Code Monkey home page Code Monkey logo

addressablesmaster's Introduction

About

Manage Addressables using Sync, Async(Built-in/UniTask), Coroutine, Lifetime Managing systems.

This solution will provide you with simple and convenient operations for Addressables assets management with results caching. You have a synchronous, asynchronous, coroutine use-case at your disposal. Also, if you are concerned about preventing memory leaks, you can use lifetime management tools to release an unused asset in time.

If you find this project useful, star it, I will be grateful!

Table of Contents

Roadmap

Status Milestone
๐Ÿš€ Comment out all code
๐Ÿš€ Reduce memory allocation
๐Ÿ“Œ Completely docs

Installation

Install via OpenUPM

The package is available on the openupm registry. It's recommended to install it via openupm-cli.

openupm add com.inc8877.addressables-master

Install via Git URL

Open Packages/manifest.json with your favorite text editor. Add the following line to the dependencies block.

{
  "dependencies": {
    "com.inc8877.addressables-master": "https://github.com/inc8877/AddressablesMaster.git",
   }
}

How to use

Intro

Plug in namespace first.

using AddressablesMaster;

All basic features for asset management are available along the following path:

ManageAddressables.[SOME_COMMAND];

Below is a list of operations that are available in each control model:

  • Initialize
  • LoadLocations
  • LoadAsset
  • LoadScene
  • UnloadScene
  • Instantiate
  • InstantiateWithAutoRelease

If you are using the UniTask in a project and want to use it in an asynchronous control model, then connect it by following the path Tools > Addressables Master > UniTask > On. .NET asynchronous operating model is used by default.

Carefully! If you switch the asynchronous model, all existing code will be invalidated as each model has its own implementation.

Short examples

For examples we will take some data:

// Some data for examples
public string startupSound;
public AssetReferenceGameObject props;
public AssetReferenceMaterial material;
public AudioSource audioSource;

[Serializable]
public class AssetReferenceMaterial : AssetReferenceT<Material>
{
  public AssetReferenceMaterial(string guid) : base(guid) { }
}

Sync

audioSource.PlayOneShot(ManageAddressables.LoadAssetSync<AudioClip>(startupSound));
            
var _material = ManageAddressables.LoadAssetSync(material);
            
ManageAddressables.InstantiateSync(props).GetComponent<MeshRenderer>().material = _material;

Async .NET

ManageAddressables.LoadAssetAsync(material, _material => ManageAddressables.InstantiateAsync(props,
                onCompletion: _go => _go.GetComponent<MeshRenderer>().material = _material));

Async UniTask

ManageAddressables.LoadAssetAsync(material).ContinueWith(result =>
            ManageAddressables.InstantiateAsync(props).ContinueWith(x =>
            x.Value.GetComponent<MeshRenderer>().material = result));

Coroutine

StartCoroutine(ManageAddressables.LoadAssetCoroutine(material, (key1, result1) =>
                StartCoroutine(ManageAddressables.InstantiateCoroutine(props,
                    onSucceeded: (key2, result2) => result2.GetComponent<MeshRenderer>().material = result1))));

Lifetime managment

When you load an addressable asset, you should release it as soon as you don't need it anymore, forgetting to do this can lead to many bad processes at runtime. Using the Addressables Master you can bind a release to the GameoObject that will do it for you automatically as soon as it is destroyed.

The Addressables Master has two ways to manage the release of objects, the first is to use methods of extending the basic operations of the Addressables, the second is to use separate methods of life management.

Addressables Extensions

If you need to use standard operations for working with addressables assets, then you can use the extensions.

public static async Task<AsyncOperationHandle<T>> AddAutoRelease<T>(this AsyncOperationHandle<T> operationHandle, GameObject targetGO)
public static async Task<AsyncOperationHandle<T>> AddAutoRelease<T>(this AsyncOperationHandle<T> operationHandle, GameObject targetGO, Action onCompletion)
public static async Task<AsyncOperationHandle<GameObject>> AddReleaseOnDestroy(this AsyncOperationHandle<GameObject> operationHandle)

Examples:

GameObject go = new GameObject("Temp");

assetReferenceMaterial.LoadAssetAsync().AddAutoRelease(go);

figureAssetRefGO.InstantiateAsync().AddReleaseOnDestroy();

Management operations

Sync

public static GameObject InstantiateSyncWithAutoRelease(string key, Transform parent = null,
            bool inWorldSpace = false)
public static GameObject InstantiateSyncWithAutoRelease(AssetReference assetReference, Transform parent = null,
            bool inWorldSpace = false)

Async .NET

public static async Task<GameObject> InstantiateAsyncWithAutoRelease(string key, Transform parent = null,
            bool inWorldSpace = false, Action<GameObject> onCompletion = null)
public static async Task<GameObject> InstantiateAsyncWithAutoRelease(AssetReference assetReference, Transform parent = null, bool inWorldSpace = false)

Async UniTask

public static async UniTask<GameObject> InstantiateAsyncWithAutoRelease(string key, Transform parent = null,
            bool inWorldSpace = false, Action<GameObject> onCompletion = null)
public static async UniTask<GameObject> InstantiateAsyncWithAutoRelease(AssetReference assetReference,
            Transform parent = null, bool inWorldSpace = false, Action<GameObject> onCompletion = null)

Coroutine

public static IEnumerator InstantiateWithAutoReleaseCoroutine(string key, Transform parent = null,
            bool inWorldSpace = false, bool trackHandle = true, Action<string, GameObject> onSucceeded = null,
            Action<string> onFailed = null)
public static IEnumerator InstantiateWithAutoReleaseCoroutine(AssetReference reference, Transform parent = null,
            bool inWorldSpace = false, Action<string, GameObject> onSucceeded = null, Action<string> onFailed = null)

Methods not tied to a specific management model

public static void AddAutoReleaseAssetTrigger(string key, GameObject targetGO)
public static void AddAutoReleaseAssetTrigger(AssetReference assetReference, GameObject targetGO)
public static void AddAutoReleaseInstanceTrigger(string key, GameObject targetGO)
public static void AddAutoReleaseInstanceTrigger(AssetReference assetReference, GameObject targetGO)

Short examples

ManageAddressables.InstantiateSyncWithAutoRelease(figureAssetRefGO);
ManageAddressables.InstantiateAsyncWithAutoRelease(figureAssetRefGO, onCompletion: x => x.transform.position = Vector3.up);

// or

var material = ManageAddressables.LoadAssetAsync(assetReferenceMaterial).Result.Value;
GameObject tempGO = new GameObject("Temp");
ManageAddressables.AddAutoReleaseAssetTrigger(assetReferenceMaterial, tempGO); // assetReferenceMaterial will be released as soon as tempGO is destroyed

Credits

The project is based on a Laicasaane solution named Unity Addressables Manager.

Logo background by founder of Kvistholt Photography.

addressablesmaster's People

Contributors

inc8877 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.