Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

Addressable Textures

Discussion in 'Addressables' started by GreedyVox, Nov 7, 2019.

  1. GreedyVox

    GreedyVox

    Joined:
    Nov 18, 2015
    Posts:
    14
    Hello, trying out unity’s addressable and so far everything is working and loving this package, but am having issues with a design that I am trying to implement, will try my best to explain.

    Screen01.png

    I have a Tilemap that needs to reference a main Texture Atlas so that tiles are in the correct clipping order, each tile has been set-up inside of the unity editor with the child sprites of the main Atlas, currently there is a lot of types of tiles with animation and rules, please ignore the buildings in the screenshots as they are not apart of the main Atlas yet, still working on this issue.

    Screen02.png

    My goal is to have 4 x Addressable Texture Atlas merge into the main large Addressable Texture Atlas that is to be used with the Tilemap, at runtime. Might sound a bit strange but those 4 x Texture Atlas are swappable Tile skins, each one of the four sub atlas has many tiles but the example images below is just a template for testing, two sub Textures and one large main Texture Atlas.

    Example01.png Example02.png

    Everything is working fine except for the Tile sprites are not updating and still referencing the main Texture Atlas from the game build, was hoping the Addressable Texture shared the same memory reference, and once updated it would atomically update all Tiles sprites when the game scene loads in, my test shows that the Addressables textures have been updated even when the scenes have changed, however doesn't seems to be sharing memory for the texture with game build and Addressable content, I am aware this can be done by coding a loading script for each tile, but that is a lot of work and I am lazy, however it’s looking like it’s my only solution.

    Example03.png

    Cheers for any help or ideas you care to share.

    Code (CSharp):
    1.  
    2. private IEnumerator Loading (string key) {
    3.             var list = new List<Texture2D> ();
    4.             yield return AddressableUtility.Loading<Texture2D> (m_Text, m_Progress, key, list);
    5.             if (list.Count > 0) {
    6.                 yield return AddressableUtility.Loading<Texture2D> (m_Text, m_Progress, "Textures/GameObjects.png", atlas => {
    7.                     atlas.PackTextures (list.OrderBy (o => o.name).ToArray (), 0, m_AtlasSize);
    8.                     atlas.Apply ();
    9.       });
    10.    }
    11. }
    12.  

    Code (CSharp):
    1.  
    2. public class AddressableUtility {
    3.         #region Methods for loading asset
    4.         public static IEnumerator Loading<T> (string key, System.Action<T> action)
    5.         where T : Object {
    6.             var op = Addressables.LoadAssetAsync<T> (key);
    7.             op.Completed += (obj) => {
    8.                 if (op.Status == AsyncOperationStatus.Succeeded) {
    9.                     action (obj.Result);
    10.                 }
    11.             };
    12.             yield return op;
    13.         }
    14.         public static IEnumerator Loading<T> (Slider slider, string key, System.Action<T> action)
    15.         where T : Object {
    16.             var op = Addressables.LoadAssetAsync<T> (key);
    17.             op.Completed += (obj) => {
    18.                 if (op.Status == AsyncOperationStatus.Succeeded) {
    19.                     action (obj.Result);
    20.                 }
    21.             };
    22.             slider.value = 0;
    23.             while (!op.IsDone) {
    24.                 slider.value = op.PercentComplete;
    25.                 yield return null;
    26.             }
    27.             slider.value = op.PercentComplete;
    28.         }
    29.         public static IEnumerator Loading<T> (TextMeshProUGUI label, Slider slider, string key, System.Action<T> action)
    30.         where T : Object {
    31.             var op = Addressables.LoadAssetAsync<T> (key);
    32.             op.Completed += (obj) => {
    33.                 label.text = string.Format ("Loading:{0}", obj.Result.name).ToUpper ();
    34.                 if (op.Status == AsyncOperationStatus.Succeeded) {
    35.                     action (obj.Result);
    36.                 }
    37.             };
    38.             slider.value = 0;
    39.             while (!op.IsDone) {
    40.                 slider.value = op.PercentComplete;
    41.                 yield return null;
    42.             }
    43.             slider.value = op.PercentComplete;
    44.         }
    45.         #endregion
    46.         #region Methods for loading assets
    47.         public static IEnumerator Loading<T> (string key, List<T> assets)
    48.         where T : Object {
    49.             yield return Addressables.LoadAssetsAsync<T> (key, obj => {
    50.                 assets.Add (obj);
    51.             });
    52.         }
    53.         public static IEnumerator Loading<T> (Slider slider, string key, List<T> assets)
    54.         where T : Object {
    55.             var op = Addressables.LoadAssetsAsync<T> (key, obj => {
    56.                 assets.Add (obj);
    57.             });
    58.             slider.value = 0;
    59.             while (!op.IsDone) {
    60.                 slider.value = op.PercentComplete;
    61.                 yield return null;
    62.             }
    63.             slider.value = op.PercentComplete;
    64.         }
    65.         public static IEnumerator Loading<T> (TextMeshProUGUI label, Slider slider, string key, List<T> assets)
    66.         where T : Object {
    67.             var op = Addressables.LoadAssetsAsync<T> (key, obj => {
    68.                 assets.Add (obj);
    69.                 label.text = string.Format ("Loading:{0}", obj.name).ToUpper ();
    70.             });
    71.             slider.value = 0;
    72.             while (!op.IsDone) {
    73.                 slider.value = op.PercentComplete;
    74.                 yield return null;
    75.       }
    76.    slider.value = op.PercentComplete;
    77. }
    78. #endregion
    79.  
     
  2. GreedyVox

    GreedyVox

    Joined:
    Nov 18, 2015
    Posts:
    14
    After researching more about Addressables, I now understand that this packages wasn’t designed to do what I was trying to implement, it loads all assets content into cache and shares the cache reference of its contents, was hoping that the cache was up-datable, would be ideal for procedural coding.



    In a nutshell will have to do it the old fashion way, creating loading scripts for the main Texture Atlas, load all Tile sprites using texture arrays, but at lease I can still use the sub Texture Atlas Addressable, in the creation and loading of the main Texture Atlas.