Code Monkey home page Code Monkey logo

unity-addressable-helper's Introduction

Addressables Helper

Helper class for Unity's Addressables Asset System.

Installation

Refer this link for installation guide: https://docs.unity3d.com/Manual/upm-ui-giturl.html

References

Static Methods

Using asset addresses

  1. Load addressable asset using address.
    private IEnumerator LoadAsset() {
        GameObject gamePrefab;
        yield return AddressablesHelper.LoadAssetByAddress<GameObject>("GamePrefabs/Game1.prefab", gameObj => gamePrefab = gameObj);
    }
  1. Load multiple addressable assets using addresses.
    private IEnumerator LoadAssets() {
        var assetAddresses = new[] {"GamePrefabs/Game1.prefab", "GamePrefabs/Game2.prefab", "GamePrefabs/Game3.prefab"};
        List<GameObject> gamePrefabs;
        yield return AddressablesHelper.LoadAssetsByAddress<GameObject>(assetAddresses, gameObjCollections => gamePrefabs = gameObjCollections.ToList());
    }

Using asset labels

  1. Load multiple addressable assets using asset label.
    private IEnumerator LoadAssets() {
        var assetLabels = "GamePrefabs";
        List<GameObject> gamePrefabs;
        yield return AddressablesHelper.LoadAssetsByLabel<GameObject>(assetLabels, gameObjCollections => gamePrefabs = gameObjCollections.ToList());
    }
  1. Load multiple addressable assets using multiple labels.
    private IEnumerator LoadAssets() {
        var assetLabels = new[] {"GamePrefabs", "Prefabs"};
        List<GameObject> gamePrefabs;
        yield return AddressablesHelper.LoadAssetsByLabels<GameObject>(assetLabels, gameObjCollections => gamePrefabs = gameObjCollections.ToList());
    }

Non-static Functions

To use the following functions you must initialize the addressables helper class with asset label & asset type as shown below. This is an async task to initialize the class on app/game start.

Initialize addressables helper

    private void InitAddressablesHelper() {
        var labelsAndTypeDict = new Dictionary<string, Type> { // {AssetLabel , TypeOfAsset}
            {"GamePrefabs", typeof(GameObject)}, 
            {"GameButtons", typeof(GameObject)}, // Multiple labels for same type of asset.
            {"Sprites", typeof(Sprite)},
            {"SpriteAtlas", typeof(SpriteAtlas)},
            {"Materials", typeof(Material)},
            {"Json", typeof(TextAsset)}
        };
        AddressablesHelper.Init(labelsAndTypeDict);
    }

Before using the following functions for first time, please ensure that the addressables helper is initialized using,

    yield return AddressablesHelper.WaitForInit;

1. Load using asset name

    private IEnumerator LoadAssets() {
        GameObject gamePrefab;
        yield return AddressablesHelper.Instance.LoadAsset<GameObject>("Game1", gameObj => gamePrefab = gameObj);
    }

Note: No need to pass the full asset address, matches the given string to existing asset addresses.

2. Load multiple assets using asset names

    private IEnumerator LoadAssets() {
        var gameObjectNames = new[] {"Game1", "Game2"};
        List<GameObject> gamePrefabs;
        yield return AddressablesHelper.Instance.LoadAssets<GameObject>(gameObjectNames, gameObjCollections => gamePrefabs = gameObjCollections.ToList());
    }

Note: No need to pass the full asset addresses, matches the given string to existing asset addresses.


Do Not Destroy On Load

All assets loaded using this class are unloaded on when a scene unloads, to prevent this pass doNotDestroyOnLoad parameter as true. This is false by default.

    private IEnumerator LoadAssets() {
        var gameObjectNames = new[] {"Game1", "Game2"};
        List<GameObject> gamePrefabs;
        yield return AddressablesHelper.Instance.LoadAssets<GameObject>(gameObjectNames, gameObjCollections => gamePrefabs = gameObjCollections.ToList(), true); // doNotDestroyOnLoad = true
    }

Demo Script

    private void Start() {
        InitAddressablesHelper();
        StartCoroutine(LoadAssets());
    }

    private void InitAddressablesHelper() {
        var labelsAndTypeDict = new Dictionary<string, Type> {
            {"GamePrefabs", typeof(GameObject)},
            {"Sprites", typeof(Sprite)},
            {"SpriteAtlas", typeof(SpriteAtlas)},
            {"Json", typeof(TextAsset)},
            {"Materials", typeof(Material)}
        };
        AddressablesHelper.Init(labelsAndTypeDict);
    }


    private IEnumerator LoadAssets() {
        yield return AddressablesHelper.WaitForInit; // Waits until addressables helper is initialized.
        var gameObjectNames = new[] {"Game1", "Game2"};
        List<GameObject> gamePrefabs;
        yield return AddressablesHelper.Instance.LoadAssets<GameObject>(gameObjectNames, gameObjCollections => gamePrefabs = gameObjCollections.ToList());
    }

unity-addressable-helper's People

Contributors

codemaker2015 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.