Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct here to familiarize yourself with the rules and how to post constructively.

  2. Unity 2022.1 is now available as the latest Tech release.
    Dismiss Notice
  3. Improve your project's performance with our new guide on profiling in Unity.
    Dismiss Notice

Help Wanted How do I incorporate Addressables into my Visual Novel application?

Discussion in 'Addressables' started by mkssanudeep, Jan 18, 2022.

  1. mkssanudeep

    mkssanudeep

    Joined:
    Dec 3, 2020
    Posts:
    14
    I have been working on this application for months and had always planned on delivery it's weekly content through addressables. However the code I have written using Resources.LoadAll() to load the character sprites from my sprite sheet, and loading Texture for my background doesn't seem to work the same way it does.

    So I have to make my text files addressable, along with my character prefab frames and sprite sheets, images, videos and audio.

    I can't seem to get my head around this. I have tried multiple ways but nothing loads up.

    I have added a screen shot of the code i use for setting up my background and also my entire Character.cs file, from which I am unable to load any sprites at all.


    Below is the info in case it helps to give you more idea:
    setLayerVideo(background,ink) - so this was how i was calling videos, ink being the video file name in my resources while background is the layer i m choosing it to play on. Foreground and cinematic as other options.

    setBackground(textureName)/setForeground(textureName)/setCinematic(textureName) does exactly what the name of the function is, setting textures on to the my raw image component.

    setBody(characterName,spriteName) - sets the body sprite onto my body renderer which is an image and the spriteName being the name of the sprite from the spriteSheet with the same name as characterName.
    setFace(characterName,spriteName) - This sets the face sprite onto my expression renderer which is an image too, similar to the setBody()
    Both these renderers are attached to my character prefab - for my main character marcus has a prefab character frame with both these renderers for my sprites to take place.
     

    Attached Files:

  2. weiping-toh

    weiping-toh

    Joined:
    Sep 8, 2015
    Posts:
    184
    You have to adapt your code into asynchronous workflows in order to use Addressables.
    At the point of GetSprite(string), the loading of sprites would not be completed hence sprites[] is empty.
    Code (CSharp):
    1. public Sprite GetSprite(string spriteName = "")
    2.     {
    3.    
    4.         AsyncOperationHandle<Sprite[]> spriteHandle = Addressables.LoadAssetAsync<Sprite[]>("Images/Characters/" + characterName);
    5.         spriteHandle.Completed += LoadSpritesWhenReady;
    6.         // At this point, sprites is empty, so the following would not work
    7.         for (int i= 0; i<sprites.Length; i++)
    8.         {
    9.             if(sprites[i].name == spriteName)
    10.             {
    11.                 return sprites[i];
    12.             }
    13.         }
    14.         return sprites.Length > 0 ? sprites[0] : null;
    15.     }
    16.  
    What you probably need to do is the change it to a async workflow
    Code (CSharp):
    1. public void GetSprite(Action<Sprite> completeAction, string spriteName = "")
    2.     {
    3.    
    4.         AsyncOperationHandle<Sprite[]> spriteHandle = Addressables.LoadAssetAsync<Sprite[]>("Images/Characters/" + characterName);
    5.         spriteHandle.Completed += (op)=>
    6.         {
    7.             sprites = op.Result;
    8.             bool found = false;
    9.             for (int i= 0; i<sprites.Length; i++)
    10.             {
    11.                 if(sprites[i].name == spriteName)
    12.                 {
    13.                     completeAction?.Invoke(sprites[i]);
    14.                     found = true;
    15.                     break;
    16.                 }
    17.             }
    18.             if(!found)
    19.                 compleAction?.Invoke(sprites.Length > 0 ? sprites[0] : null);
    20.          }
    21.     }
    22.  
    The above code is just an example and not really ideal as it is dumb to just load a whole array and take 1 element, then discard the rest.
    The point is you should not expect instant results from Addressables and should modify your code design to utilize Addressables. A good way is to preload resources for, in your case, a particular chapter/scenario and manage them in a Dictionary for instant retrieval.
     
unityunity