AssetLoader
class SoftKitty.AssetLoader
AssetLoader is a base class responsible for handling asset loading, including prefabs, textures, audio clips, and more.
Inherit from this class to create your own loader script, override the load methods as needed (eg: Asset Bundles, Addressable), then attach the script to an empty prefab and assign it in SGD_Settings via:
Project Settings > SoftKitty > Data Settings > General > Custom Loader
Methods
public virtual T Load<T>(string _path) where T : UnityEngine.Object
Load an Asset from AssetBundle or your custom loading method. Loaded bundle will be add to a Dictionary.
Example code for load from Addressables
public override T Load<T>(string _path) where T : UnityEngine.Object{
var op = Addressables.LoadAssetAsync<T>(_path);
T go = op.WaitForCompletion();
return go;
}
Example code for load from AssetBundle
/// In this example, the bundle path and the object path are separated using "#".
/// For example, an object with the path "icons/food" stored in a bundle named "IconData"
/// would be accessed using the path: "IconData#icons/food".
public override T Load<T>(string _path) where T : UnityEngine.Object{
string[] _args = _path.Split("#");
if (_args.Length >= 2)
{
AssetBundle myLoadedAssetBundle=null;
if (LoadedBundle.ContainsKey(_args[0]))
{
myLoadedAssetBundle = LoadedBundle[_args[0]];
}
else
{
myLoadedAssetBundle = AssetBundle.LoadFromFile(Path.Combine(Application.streamingAssetsPath, _args[0]));
if (myLoadedAssetBundle == null)
{
Debug.Log("Failed to load AssetBundle: " + _args[0]);
return null;
}
else
{
LoadedBundle.Add(_args[0], myLoadedAssetBundle);
}
}
var go = myLoadedAssetBundle.LoadAsset<T>(_args[1]);
return go;
}
else
{
Debug.Log("The path must be in this format: BundleName#ObjectPath, for example: IconData#icons/food" + _args[0]);
return null;
}
}
public virtual T LoadAndInstantiate<T>(string _path) where T : UnityEngine.Object
Load and Instantiate an Asset from AssetBundle or your custom loading method. Loaded bundle will be add to a Dictionary.
Example code for load from Addressables
public override T LoadAndInstantiate<T>(string _path) where T : UnityEngine.Object{
var _handle = Addressables.LoadAssetAsync<T>(_path);
T _ref = _handle.WaitForCompletion();
if (_ref != null)
{
var op = Addressables.InstantiateAsync(_path);
T newObj = op.Result;
Addressables.Release(_handle);
//Make sure to call Addressables.Release(newObj); when this object is about to destroied!!
return newObj;
}
else
{
return null;
}
}
Example code for load from AssetBundle
/// In this example, the bundle path and the object path are separated using "#".
/// For example, an object with the path "icons/food" stored in a bundle named "IconData"
/// would be accessed using the path: "IconData#icons/food".
public override T LoadAndInstantiate<T>(string _path) where T : UnityEngine.Object{
string[] _args = _path.Split("#");
if (_args.Length >= 2)
{
AssetBundle myLoadedAssetBundle = null;
if (LoadedBundle.ContainsKey(_args[0]))
{
myLoadedAssetBundle = LoadedBundle[_args[0]];
}
else
{
myLoadedAssetBundle = AssetBundle.LoadFromFile(Path.Combine(Application.streamingAssetsPath, _args[0]));
if (myLoadedAssetBundle == null)
{
Debug.Log("Failed to load AssetBundle: " + _args[0]);
return null;
}
else
{
LoadedBundle.Add(_args[0], myLoadedAssetBundle);
}
}
var go = myLoadedAssetBundle.LoadAsset<T>(_args[1]);
return Instantiate(go);
}
else
{
Debug.Log("The path must be in this format: BundleName#ObjectPath, for example: IconData#icons/food" + _args[0]);
return null;
}
}
public virtual void ReleaseAllBundles(bool _unloadAllLoadedObjects)
Release all loaded AssetBundle.
public virtual void ReleaseAllLoadedAsset()
Release all loaded icons and custom assets from the memory.
public virtual void ReleaseAsset(string _path)
When 'ReleaseLoadedAsset()' is called from an item, this method will be called with the unloading asset. You can override this method to handle it.