Skip to content

Resource Management ​

Introduction ​

The resource management component (ResourcesComponent) is a resource management system based on YooAsset, providing a unified mechanism for resource loading, unloading, and lifecycle management. Main features:

  • Based on YooAsset framework
  • Support for synchronous/asynchronous loading
  • Automatic reference counting management
  • Scene loading support
  • Coroutine lock protection

Core Functions ​

1. Component Lifecycle ​

csharp
// Default package initialization
public class ResourcesComponentAwakeSystem : AwakeSystem<ResourcesComponent>
{
    protected override void Awake(ResourcesComponent self)
    {
        self.package = YooAssets.GetPackage("DefaultPackage");
    }
}

// Custom package initialization
public class ResourcesComponentAwakeASystem : AwakeSystem<ResourcesComponent, string>
{
    protected override void Awake(ResourcesComponent self, string packageName)
    {
        self.package = YooAssets.GetPackage(packageName);
    }
}

2. Resource Loading Interfaces ​

Synchronous Loading ​

csharp
// Load a single asset
T asset = resourcesComponent.LoadAssetSync<T>(string location);

Asynchronous Loading ​

csharp
// Load a single asset
T asset = await resourcesComponent.LoadAssetAsync<T>(string location);

// Load collection of assets of the same type
Dictionary<string, T> assets = await resourcesComponent.LoadAllAssetsAsync<T>(string location);

// Load scene
await resourcesComponent.LoadSceneAsync(string location, LoadSceneMode loadSceneMode);

3. Resource Unloading ​

csharp
// Unload specific asset
resourcesComponent.UnLoadAssetSync(string location);

Feature Description ​

1. Reference Counting Management ​

  • Automatically manage the lifecycle of resource handles
  • Support for various types of resource handles:
    • AssetOperationHandle
    • AllAssetsOperationHandle
    • SubAssetsOperationHandle
    • RawFileOperationHandle
    • SceneOperationHandle

2. Automatic Resource Release ​

csharp
// Automatically release all resources when component is destroyed
public class ResourcesComponentDestroySystem : DestroySystem<ResourcesComponent>
{
    protected override void Destroy(ResourcesComponent self)
    {
        foreach (var kv in self.handlers)
        {
            switch (kv.Value)
            {
                case AssetOperationHandle handle:
                    handle.Release();
                    break;
                case AllAssetsOperationHandle handle:
                    handle.Release();
                    break;
                case SubAssetsOperationHandle handle:
                    handle.Release();
                    break;
                case RawFileOperationHandle handle:
                    handle.Release();
                    break;
                case SceneOperationHandle handle:
                    if (!handle.IsMainScene())
                        handle.UnloadAsync();
                    break;
            }
        }
    }
}

The benefit of this is:

When a CurrentScene battle scene is created with a resource component attached, and all battle resources are loaded through this component, when the battle ends and the battle scene is destroyed, this component will call the Dispose lifecycle method to release all the resources loaded at that time.

Feature explanation:

  • Automatically release all resource handles when the component is destroyed
  • Support for correct release of all types of resources
  • Prevent resource leaks

3. Coroutine Lock Protection ​

  • Use CoroutineLock to ensure thread safety of resource loading
  • Locking mechanism based on location hash value
  • Prevent duplicate loading of the same resource

4. Scene Management ​

  • Support for asynchronous scene loading
  • Automatically handle the relationship between main scenes and sub-scenes
  • Automatically handle resource release when unloading scenes

Usage Examples ​

1. Basic Resource Loading ​

csharp
// Synchronously load prefab
GameObject prefab = self.ResourcesComponent.LoadAssetSync<GameObject>("Character");

// Asynchronously load texture
Texture2D texture = await self.ResourcesComponent.LoadAssetAsync<Texture2D>("Background");

2. Batch Resource Loading ​

csharp
// Load all sprites in a directory
Dictionary<string, Sprite> sprites = await self.ResourcesComponent.LoadAllAssetsAsync<Sprite>("Icons");
foreach (var sprite in sprites.Values)
{
    // Use sprite
}

3. Scene Loading ​

csharp
// Asynchronously load new scene
await self.ResourcesComponent.LoadSceneAsync("Level1", LoadSceneMode.Single);

// Asynchronously load additional scene
await self.ResourcesComponent.LoadSceneAsync("UI", LoadSceneMode.Additive);

Best Practices ​

Usage Recommendations

  1. Resource Loading:

    • Prioritize asynchronous loading to avoid stuttering
    • Use batch loading interfaces wisely
    • Pay attention to the scope of coroutine locks
  2. Memory Management:

    • Unload resources that are no longer needed promptly
    • Use using statements to manage coroutine locks
    • Avoid loading the same resources repeatedly
  3. Scene Management:

    • Distinguish between loading methods for main scenes and sub-scenes
    • Properly handle resource release during scene transitions
    • Plan scene resource dependencies wisely

Notes ​

WARNING

  1. Asynchronous loading operations need to use await to wait for completion
  2. Resource paths (location) must be consistent with packaging configuration
  3. Pay attention to handling exceptions in case of loading failures
  4. Avoid synchronous resource loading in frequently updated loops

Technical Support ​

Get Help

  • 💬 Join QQ group for discussion (ET Framework Group) : 474643097
  • ⭐ Follow the project on GitHub for the latest updates
  • 📚 Check YooAsset Documentation for more details

Released under the MIT License.