Class AssetManager
- Namespace
- TAssetBundle.Extensions
- Assembly
- TAssetBundle.Extensions.dll
AssetManager provides methods to load, unload, and manage assets and prefabs using TAssetBundle for efficient asset management. It supports asynchronous loading, unloading of unused assets, and pooling of prefabs.
public static class AssetManager
- Inheritance
-
AssetManager
- Inherited Members
Properties
AssetInfos
Gets the dictionary of asset information, where the key is the asset path.
public static Dictionary<string, AssetInfo> AssetInfos { get; }
Property Value
LoadPrefabFunc
Gets or sets the delegate for loading prefabs.
public static AssetManager.LoadPrefabDelegate LoadPrefabFunc { get; set; }
Property Value
PrefabPoolRoot
Gets the root transform for prefab pools.
public static Transform PrefabPoolRoot { get; }
Property Value
- Transform
PrefabPools
Gets the dictionary of prefab pools, where the key is the prefab path.
public static Dictionary<string, AssetPrefabPool> PrefabPools { get; }
Property Value
Methods
ClearPrefabPool(string)
Clears a specific prefab pool.
public static bool ClearPrefabPool(string prefabPath)
Parameters
prefabPathstringPath to the prefab.
Returns
- bool
True if the pool was cleared, false otherwise.
ClearPrefabPools()
Clears all prefab pools.
public static void ClearPrefabPools()
GetPrefab(string, Transform, Action<GameObject>, bool, bool, object)
Asynchronously loads a prefab using an asset path and instantiates it as a game object.
public static void GetPrefab(string prefabPath, Transform parent, Action<GameObject> onComplete, bool active = true, bool pooling = true, object userData = null)
Parameters
prefabPathstringThe path to the prefab.
parentTransformThe parent transform to attach the prefab to after instancing.
onCompleteAction<GameObject>Callback to invoke when the prefab has been successfully loaded and instantiated.
activeboolIndicates whether the prefab should be active upon instancing. Default is true.
poolingboolIndicates whether pooling should be used for the prefab. Default is true.
userDataobjectOptional user data to associate with the prefab load and instancing operation.
GetPrefab(AssetRef, Transform, Action<GameObject>, bool, bool, object)
Asynchronously loads a prefab using an asset reference and instantiates it as a game object.
public static void GetPrefab(AssetRef assetRef, Transform parent, Action<GameObject> onComplete, bool active = true, bool pooling = true, object userData = null)
Parameters
assetRefAssetRefThe asset reference containing the path to the prefab.
parentTransformParent transform to attach the prefab.
onCompleteAction<GameObject>Callback to be executed when the prefab is loaded.
activeboolWhether the prefab should be active upon loading.
poolingboolWhether to use pooling for the prefab.
userDataobjectAdditional user data that can be passed along.
GetPrefabAsync(string, Transform, bool, bool, object)
Asynchronously loads a prefab using an asset reference and instantiates it as a game object.
public static AsyncOperation<GameObject> GetPrefabAsync(string prefabPath, Transform parent, bool active = true, bool pooling = true, object userData = null)
Parameters
prefabPathstringThe path to the prefab.
parentTransformParent transform for the instantiated prefab.
activeboolWhether the prefab should be active after instancing.
poolingboolWhether to use pooling for the prefab.
userDataobjectUser data that can be passed during the instancing process.
Returns
- AsyncOperation<GameObject>
Async operation for loading and instancing the prefab.
GetPrefabAsync(AssetRef, Transform, bool, bool, object)
Asynchronously loads a prefab using an asset reference and instantiates it as a game object.
public static AsyncOperation<GameObject> GetPrefabAsync(AssetRef assetRef, Transform parent, bool active = true, bool pooling = true, object userData = null)
Parameters
assetRefAssetRefThe asset reference containing the path to the prefab.
parentTransformThe parent transform to attach the loaded prefab to.
activeboolIndicates whether the prefab should be active upon loading. Default is true.
poolingboolIndicates whether pooling should be used for the prefab. Default is true.
userDataobjectOptional user data to associate with the load operation.
Returns
- AsyncOperation<GameObject>
Async operation for the prefab load process.
GetUsingAssets()
Retrieves all assets that are currently in use.
public static IEnumerable<AssetInfo> GetUsingAssets()
Returns
- IEnumerable<AssetInfo>
An enumerable collection of assets that are being used.
IsLoadedAsset(string)
Checks if a specific asset is loaded.
public static bool IsLoadedAsset(string assetPath)
Parameters
assetPathstringPath to the asset.
Returns
- bool
True if the asset is loaded, false otherwise.
IsLoading()
Checks if any assets are currently loading.
public static bool IsLoading()
Returns
- bool
True if any assets are loading, false otherwise.
LoadAssetAsync<T>(string)
Asynchronously loads an asset using its path.
public static AsyncOperation<AssetInfo<T>> LoadAssetAsync<T>(string assetPath) where T : Object
Parameters
assetPathstringThe path to the asset.
Returns
- AsyncOperation<AssetInfo<T>>
An asynchronous operation that completes with the loaded asset information.
Type Parameters
TThe type of the asset to load.
LoadAssetAsync<T>(AssetRef)
Asynchronously loads an asset using an asset reference.
public static AsyncOperation<AssetInfo<T>> LoadAssetAsync<T>(AssetRef assetRef) where T : Object
Parameters
assetRefAssetRefThe asset reference containing the asset path.
Returns
- AsyncOperation<AssetInfo<T>>
An asynchronous operation that completes with the loaded asset information.
Type Parameters
TThe type of the asset to load.
LoadAssetAsync<T>(GameObject, string)
Asynchronously loads an asset of the specified type.
public static AsyncOperation<AssetInfo<T>> LoadAssetAsync<T>(GameObject context, string assetPath) where T : Object
Parameters
contextGameObjectThe GameObject context for tracking the asset's lifecycle.
assetPathstringThe path to the asset.
Returns
- AsyncOperation<AssetInfo<T>>
An asynchronous operation that completes with the loaded asset information.
Type Parameters
TThe type of the asset to load.
LoadAssetAsync<T>(GameObject, AssetRef)
Asynchronously loads an asset using a context and an asset reference.
public static AsyncOperation<AssetInfo<T>> LoadAssetAsync<T>(GameObject context, AssetRef assetRef) where T : Object
Parameters
contextGameObjectThe GameObject context for tracking the asset's lifecycle.
assetRefAssetRefThe asset reference containing the asset path.
Returns
- AsyncOperation<AssetInfo<T>>
An asynchronous operation that completes with the loaded asset information.
Type Parameters
TThe type of the asset to load.
LoadAsset<T>(IGetProgressable, GameObject, string, bool, Action<AssetInfo<T>>)
Loads an asset asynchronously.
public static Coroutine LoadAsset<T>(IGetProgressable getProgressable, GameObject context, string assetPath, bool waitForEndOfFrame, Action<AssetInfo<T>> onComplete) where T : Object
Parameters
getProgressableIGetProgressableAn object that provides progress information during loading.
contextGameObjectThe GameObject context for tracking the asset's lifecycle.
assetPathstringThe path to the asset.
waitForEndOfFrameboolWhether to wait until the end of the frame to complete the load operation.
onCompleteAction<AssetInfo<T>>Callback invoked when the asset loading is complete.
Returns
- Coroutine
Coroutine that loads the asset asynchronously.
Type Parameters
TType of the asset.
LoadAsset<T>(IGetProgressable, GameObject, AssetRef, bool, Action<AssetInfo<T>>)
Loads an asset using an asset reference.
public static Coroutine LoadAsset<T>(IGetProgressable getProgressable, GameObject context, AssetRef assetRef, bool waitForEndOfFrame, Action<AssetInfo<T>> onComplete) where T : Object
Parameters
getProgressableIGetProgressableAn object that provides progress information during loading.
contextGameObjectThe GameObject context for tracking the asset's lifecycle.
assetRefAssetRefThe asset reference containing the asset path.
waitForEndOfFrameboolWhether to wait until the end of the frame to complete the load operation.
onCompleteAction<AssetInfo<T>>Callback invoked when the asset loading is complete.
Returns
- Coroutine
Coroutine that loads the asset asynchronously.
Type Parameters
TType of the asset.
LoadAsset<T>(GameObject, string, bool, Action<AssetInfo<T>>)
Loads an asset asynchronously.
public static Coroutine LoadAsset<T>(GameObject context, string assetPath, bool waitForEndOfFrame, Action<AssetInfo<T>> onComplete) where T : Object
Parameters
contextGameObjectThe GameObject context for tracking the asset's lifecycle.
assetPathstringThe path to the asset.
waitForEndOfFrameboolWhether to wait until the end of the frame to complete the load operation.
onCompleteAction<AssetInfo<T>>Callback invoked when the asset loading is complete.
Returns
- Coroutine
Coroutine that loads the asset asynchronously.
Type Parameters
TType of the asset.
LoadAsset<T>(GameObject, AssetRef, bool, Action<AssetInfo<T>>)
Loads an asset using an asset reference.
public static Coroutine LoadAsset<T>(GameObject context, AssetRef assetRef, bool waitForEndOfFrame, Action<AssetInfo<T>> onComplete) where T : Object
Parameters
contextGameObjectThe GameObject context for tracking the asset's lifecycle.
assetRefAssetRefThe asset reference containing the asset path.
waitForEndOfFrameboolWhether to wait until the end of the frame to complete the load operation.
onCompleteAction<AssetInfo<T>>Callback invoked when the asset loading is complete.
Returns
- Coroutine
Coroutine that loads the asset asynchronously.
Type Parameters
TType of the asset.
PreloadPrefabAsync(string, bool, object)
Asynchronously preloads a prefab.
public static AsyncOperation<GameObject> PreloadPrefabAsync(string prefabPath, bool pooling = true, object userData = null)
Parameters
prefabPathstringThe path to the prefab.
poolingboolIndicates whether to use pooling for the prefab.
userDataobjectAdditional user data to associate with the preload operation.
Returns
- AsyncOperation<GameObject>
An async operation representing the preload operation for the prefab.
PreloadPrefabAsync(GameObjectAssetRef, bool, object)
Asynchronously preloads a prefab using an asset reference.
public static AsyncOperation<GameObject> PreloadPrefabAsync(GameObjectAssetRef assetRef, bool pooling = true, object userData = null)
Parameters
assetRefGameObjectAssetRefThe asset reference containing the path to the prefab.
poolingboolIndicates whether to use pooling for the prefab.
userDataobjectAdditional user data to associate with the preload operation.
Returns
- AsyncOperation<GameObject>
An async operation representing the preload operation for the prefab.
TryGetAssetInfo(string, out AssetInfo)
Tries to get information about a specific asset.
public static bool TryGetAssetInfo(string assetPath, out AssetInfo assetInfo)
Parameters
Returns
- bool
True if the asset information was found, false otherwise.
UnloadAsset(string, bool)
Unloads the specified asset if it is loaded and removes it from the asset information.
public static bool UnloadAsset(string assetPath, bool force = false)
Parameters
assetPathstringThe path to the asset.
forceboolIf true, forces the asset to be unloaded even if it is marked as "Don't Destroy".
Returns
- bool
Returns true if the asset was successfully unloaded, otherwise false.
UnloadAsset(AssetRef, bool)
Unloads a specific asset using an asset reference.
public static bool UnloadAsset(AssetRef assetRef, bool force = false)
Parameters
assetRefAssetRefThe asset reference containing the asset path.
forceboolIf true, forces the asset to be unloaded even if marked as "Don't Destroy".
Returns
- bool
True if the asset was successfully unloaded, otherwise false.
UnloadAssetsAll()
Unloads all loaded assets.
public static void UnloadAssetsAll()
UnloadUnusedAssets()
Unloads all unused assets.
public static void UnloadUnusedAssets()
UnloadUnusedAssetsAfterLastUse(float)
Unloads assets that have not been used for a specified duration.
public static void UnloadUnusedAssetsAfterLastUse(float seconds)
Parameters
secondsfloatThe time in seconds since the last use before an asset is unloaded.
Events
OnCreated
Event triggered when a new prefab instance is created in the pool.
public static event PrefabPoolDelegate OnCreated
Event Type
OnFree
Event triggered when a prefab instance is returned to the pool for reuse.
public static event PrefabPoolDelegate OnFree
Event Type
OnUsed
Event triggered when a prefab instance is retrieved from the pool for use.
public static event PrefabPoolDelegate OnUsed