Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/inc8877/AddressablesMaster
Manage Addressables using Sync, Async(.NET / UniTask), Coroutine, Lifetime Managing systems with results caching
https://github.com/inc8877/AddressablesMaster
addressables async coroutines lifetime management manager sync unity
Last synced: 2 months ago
JSON representation
Manage Addressables using Sync, Async(.NET / UniTask), Coroutine, Lifetime Managing systems with results caching
- Host: GitHub
- URL: https://github.com/inc8877/AddressablesMaster
- Owner: inc8877
- License: apache-2.0
- Created: 2021-08-29T16:49:50.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2024-08-28T13:41:59.000Z (5 months ago)
- Last Synced: 2024-08-28T15:05:18.333Z (5 months ago)
- Topics: addressables, async, coroutines, lifetime, management, manager, sync, unity
- Language: C#
- Homepage:
- Size: 89.8 KB
- Stars: 36
- Watchers: 2
- Forks: 3
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
## 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
- [About](#about)
- [Table of Contents](#table-of-contents)
- [Roadmap](#roadmap)
- [Installation](#installation)
- [Install via OpenUPM](#install-via-openupm)
- [Install via Git URL](#install-via-git-url)
- [How to use](#how-to-use)
- [Intro](#intro)
- [Short examples](#short-examples)
- [Lifetime managment](#lifetime-managment)
- [Addressables Extensions](#addressables-extensions)
- [Management operations](#management-operations)
- [Short examples](#short-examples-1)
- [Credits](#credits)## Roadmap
| Status | Milestone |
| :-------: | :----------------------- |
| :rocket: | Comment out all code |
| :rocket: | Reduce memory allocation |
| :pushpin: | Completely docs |## Installation
### Install via OpenUPM
The package is available on the [openupm](https://openupm.com) registry. It's recommended to install it via [openupm-cli](https://github.com/openupm/openupm-cli).
```bash
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.
```json
{
"dependencies": {
"com.inc8877.addressables-master": "https://github.com/inc8877/AddressablesMaster.git",
}
}
```## How to use
### Intro
Plug in namespace first.
```c#
using AddressablesMaster;
```All basic features for asset management are available along the following path:
```c#
ManageAddressables.[SOME_COMMAND];
```Below is a list of operations that are available in each control model:
- Initialize
- LoadLocations
- LoadAsset
- LoadScene
- UnloadScene
- Instantiate
- InstantiateWithAutoReleaseIf 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:
```c#
// Some data for examples
public string startupSound;
public AssetReferenceGameObject props;
public AssetReferenceMaterial material;
public AudioSource audioSource;[Serializable]
public class AssetReferenceMaterial : AssetReferenceT
{
public AssetReferenceMaterial(string guid) : base(guid) { }
}
````Sync`
```c#
audioSource.PlayOneShot(ManageAddressables.LoadAssetSync(startupSound));
var _material = ManageAddressables.LoadAssetSync(material);
ManageAddressables.InstantiateSync(props).GetComponent().material = _material;
````Async .NET`
```c#
ManageAddressables.LoadAssetAsync(material, _material => ManageAddressables.InstantiateAsync(props,
onCompletion: _go => _go.GetComponent().material = _material));
````Async UniTask`
```c#
ManageAddressables.LoadAssetAsync(material).ContinueWith(result =>
ManageAddressables.InstantiateAsync(props).ContinueWith(x =>
x.Value.GetComponent().material = result));
````Coroutine`
```c#
StartCoroutine(ManageAddressables.LoadAssetCoroutine(material, (key1, result1) =>
StartCoroutine(ManageAddressables.InstantiateCoroutine(props,
onSucceeded: (key2, result2) => result2.GetComponent().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](#addressables-extensions) 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.
```c#
public static async Task> AddAutoRelease(this AsyncOperationHandle operationHandle, GameObject targetGO)
``````c#
public static async Task> AddAutoRelease(this AsyncOperationHandle operationHandle, GameObject targetGO, Action onCompletion)
``````c#
public static async Task> AddReleaseOnDestroy(this AsyncOperationHandle operationHandle)
```Examples:
```c#
GameObject go = new GameObject("Temp");assetReferenceMaterial.LoadAssetAsync().AddAutoRelease(go);
figureAssetRefGO.InstantiateAsync().AddReleaseOnDestroy();
```#### Management operations
`Sync`
```c#
public static GameObject InstantiateSyncWithAutoRelease(string key, Transform parent = null,
bool inWorldSpace = false)
``````c#
public static GameObject InstantiateSyncWithAutoRelease(AssetReference assetReference, Transform parent = null,
bool inWorldSpace = false)
````Async .NET`
```c#
public static async Task InstantiateAsyncWithAutoRelease(string key, Transform parent = null,
bool inWorldSpace = false, Action onCompletion = null)
``````c#
public static async Task InstantiateAsyncWithAutoRelease(AssetReference assetReference, Transform parent = null, bool inWorldSpace = false)
````Async UniTask`
```c#
public static async UniTask InstantiateAsyncWithAutoRelease(string key, Transform parent = null,
bool inWorldSpace = false, Action onCompletion = null)
``````c#
public static async UniTask InstantiateAsyncWithAutoRelease(AssetReference assetReference,
Transform parent = null, bool inWorldSpace = false, Action onCompletion = null)
````Coroutine`
```c#
public static IEnumerator InstantiateWithAutoReleaseCoroutine(string key, Transform parent = null,
bool inWorldSpace = false, bool trackHandle = true, Action onSucceeded = null,
Action onFailed = null)
``````c#
public static IEnumerator InstantiateWithAutoReleaseCoroutine(AssetReference reference, Transform parent = null,
bool inWorldSpace = false, Action onSucceeded = null, Action onFailed = null)
````Methods not tied to a specific management model`
```c#
public static void AddAutoReleaseAssetTrigger(string key, GameObject targetGO)
``````c#
public static void AddAutoReleaseAssetTrigger(AssetReference assetReference, GameObject targetGO)
``````c#
public static void AddAutoReleaseInstanceTrigger(string key, GameObject targetGO)
``````c#
public static void AddAutoReleaseInstanceTrigger(AssetReference assetReference, GameObject targetGO)
```#### Short examples
```c#
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](https://github.com/laicasaane) solution named [Unity Addressables Manager](https://github.com/laicasaane/unity-addressables-manager).
Logo background by founder of [Kvistholt Photography](https://unsplash.com/@freeche).