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
prefabPath
stringPath 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
prefabPath
stringThe path to the prefab.
parent
TransformThe parent transform to attach the prefab to after instancing.
onComplete
Action<GameObject>Callback to invoke when the prefab has been successfully loaded and instantiated.
active
boolIndicates whether the prefab should be active upon instancing. Default is true.
pooling
boolIndicates whether pooling should be used for the prefab. Default is true.
userData
objectOptional 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
assetRef
AssetRefThe asset reference containing the path to the prefab.
parent
TransformParent transform to attach the prefab.
onComplete
Action<GameObject>Callback to be executed when the prefab is loaded.
active
boolWhether the prefab should be active upon loading.
pooling
boolWhether to use pooling for the prefab.
userData
objectAdditional 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
prefabPath
stringThe path to the prefab.
parent
TransformParent transform for the instantiated prefab.
active
boolWhether the prefab should be active after instancing.
pooling
boolWhether to use pooling for the prefab.
userData
objectUser 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
assetRef
AssetRefThe asset reference containing the path to the prefab.
parent
TransformThe parent transform to attach the loaded prefab to.
active
boolIndicates whether the prefab should be active upon loading. Default is true.
pooling
boolIndicates whether pooling should be used for the prefab. Default is true.
userData
objectOptional 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
assetPath
stringPath 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
assetPath
stringThe path to the asset.
Returns
- AsyncOperation<AssetInfo<T>>
An asynchronous operation that completes with the loaded asset information.
Type Parameters
T
The 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
assetRef
AssetRefThe asset reference containing the asset path.
Returns
- AsyncOperation<AssetInfo<T>>
An asynchronous operation that completes with the loaded asset information.
Type Parameters
T
The 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
context
GameObjectThe GameObject context for tracking the asset's lifecycle.
assetPath
stringThe path to the asset.
Returns
- AsyncOperation<AssetInfo<T>>
An asynchronous operation that completes with the loaded asset information.
Type Parameters
T
The 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
context
GameObjectThe GameObject context for tracking the asset's lifecycle.
assetRef
AssetRefThe asset reference containing the asset path.
Returns
- AsyncOperation<AssetInfo<T>>
An asynchronous operation that completes with the loaded asset information.
Type Parameters
T
The 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
getProgressable
IGetProgressableAn object that provides progress information during loading.
context
GameObjectThe GameObject context for tracking the asset's lifecycle.
assetPath
stringThe path to the asset.
waitForEndOfFrame
boolWhether to wait until the end of the frame to complete the load operation.
onComplete
Action<AssetInfo<T>>Callback invoked when the asset loading is complete.
Returns
- Coroutine
Coroutine that loads the asset asynchronously.
Type Parameters
T
Type 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
getProgressable
IGetProgressableAn object that provides progress information during loading.
context
GameObjectThe GameObject context for tracking the asset's lifecycle.
assetRef
AssetRefThe asset reference containing the asset path.
waitForEndOfFrame
boolWhether to wait until the end of the frame to complete the load operation.
onComplete
Action<AssetInfo<T>>Callback invoked when the asset loading is complete.
Returns
- Coroutine
Coroutine that loads the asset asynchronously.
Type Parameters
T
Type 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
context
GameObjectThe GameObject context for tracking the asset's lifecycle.
assetPath
stringThe path to the asset.
waitForEndOfFrame
boolWhether to wait until the end of the frame to complete the load operation.
onComplete
Action<AssetInfo<T>>Callback invoked when the asset loading is complete.
Returns
- Coroutine
Coroutine that loads the asset asynchronously.
Type Parameters
T
Type 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
context
GameObjectThe GameObject context for tracking the asset's lifecycle.
assetRef
AssetRefThe asset reference containing the asset path.
waitForEndOfFrame
boolWhether to wait until the end of the frame to complete the load operation.
onComplete
Action<AssetInfo<T>>Callback invoked when the asset loading is complete.
Returns
- Coroutine
Coroutine that loads the asset asynchronously.
Type Parameters
T
Type 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
prefabPath
stringThe path to the prefab.
pooling
boolIndicates whether to use pooling for the prefab.
userData
objectAdditional 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
assetRef
GameObjectAssetRefThe asset reference containing the path to the prefab.
pooling
boolIndicates whether to use pooling for the prefab.
userData
objectAdditional 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
assetPath
stringThe path to the asset.
force
boolIf 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
assetRef
AssetRefThe asset reference containing the asset path.
force
boolIf 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
seconds
floatThe 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