Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question SpriteAtlas --> late binding

Discussion in '2D' started by Leesh_NeoSonyx, Jul 1, 2022.

  1. Leesh_NeoSonyx

    Leesh_NeoSonyx

    Joined:
    Aug 12, 2020
    Posts:
    10
    Why are the results screens different for the two sources Code?

    1. Source Code

    public void RequestAtlas(string _Key, System.Action<SpriteAtlas> callback)
    {

    if(false == true)
    {
    var result = Resources.Load<SpriteAtlas>("SpriteAtlas/UILogin/"+_Key);

    callback(result);
    }
    }

    2. Source Code

    public void Awake()
    {
    RequestAtlas("SpriteAtlas/UILogin/UILogin", (result) =>
    {
    if (result != null)
    Debug.Log($"[SpriteAtlas] : {result.name}");
    });
    }


    public void RequestAtlas(string _Key, System.Action<SpriteAtlas> callback)
    {

    if(false == true)
    {
    var result = Resources.Load<SpriteAtlas>(_Key);

    callback(result);
    }
    }


    1 Source Code Result
    upload_2022-7-1_17-52-51.png

    2 Source Code Result




    SpiteAtlas path: SpriteAtlas/UILogin/UILogin.SpriteAtlas

    Additional questions:
    When is the packed texture data in Spriteatlas saved to a file and where is the path?


    plz........
    Please check the issue
     

    Attached Files:

    Last edited: Jul 18, 2022
  2. Leesh_NeoSonyx

    Leesh_NeoSonyx

    Joined:
    Aug 12, 2020
    Posts:
    10
    I'm testing it because I want to put the SpriteAtlas in a specific subfolder of the Resources folder

    The two source codes explicitly specify the folder path, but the results are different.
     
  3. Venkify

    Venkify

    Unity Technologies

    Joined:
    Apr 7, 2015
    Posts:
    619
    Could you please post a simple repro project ? That will help us investigate the issue better.
     
  4. Leesh_NeoSonyx

    Leesh_NeoSonyx

    Joined:
    Aug 12, 2020
    Posts:
    10
    A sample project has been uploaded.
    Unity 2021.3.4f1 version


    Just open the sample project, change to the Android platform, and run Login.unity in the Scenes folder. Then the image called TestSample is normally displayed on the screen.
     

    Attached Files:

  5. Leesh_NeoSonyx

    Leesh_NeoSonyx

    Joined:
    Aug 12, 2020
    Posts:
    10
    Reproduce the situation when image output fails

    1.
    Uncomment the RequestAtlas() function of the Awake() function and make an explicit call.

    2. In the Request Atlas callback function var result = Resources.Load<Sprite Atlas>("Sprite Atlas/UI Login/" + _Key); ---> var result = Resources.Load<SpriteAtlas>(_Key);
    change to


    3. When debugging, the Awake function calls RequestAtlas forcibly.
    Then, RequestAtlas is called once more to find the rs_ui_title image in the object registered in Hierarchy. In conclusion, it is called twice, but in the first forced call, Atlas is considered to be registered, but the image output fails.
     
  6. Leesh_NeoSonyx

    Leesh_NeoSonyx

    Joined:
    Aug 12, 2020
    Posts:
    10
    Is there any way to call the Atlas image to the folder I specified?
     
  7. Leesh_NeoSonyx

    Leesh_NeoSonyx

    Joined:
    Aug 12, 2020
    Posts:
    10
    Please check the issue
     
  8. Venkify

    Venkify

    Unity Technologies

    Joined:
    Apr 7, 2015
    Posts:
    619
    From the attached project, it is still unclear what exactly is the issue. I see this code :

    ....
    if (test != null)
    {
    GetComponent<Image>().sprite = test;
    }
    The above line in SpriteAtlasManager.cs will obviously fail as it does not contain an UI Image component. Could you please provide more info or explain the issue ?

    Also regarding
    When is the packed texture data in Spriteatlas saved to a file and where is the path? - This is not accessible as its managed internally by SpriteAtlas.
    Thanks.
     
  9. Leesh_NeoSonyx

    Leesh_NeoSonyx

    Joined:
    Aug 12, 2020
    Posts:
    10
    You can delete the source. This is a source that has not been erased while testing.
     
  10. Leesh_NeoSonyx

    Leesh_NeoSonyx

    Joined:
    Aug 12, 2020
    Posts:
    10
    I understand that the atlas texture is accessed through the registered callback function when the texture is needed. However, the final conclusion is that I want the path of the atlas texture to be a subfolder of Resources/SpriteAtlas in the Resources folder.
     
  11. Leesh_NeoSonyx

    Leesh_NeoSonyx

    Joined:
    Aug 12, 2020
    Posts:
    10
    Looking at the second source code, RequestAtlas is forcibly called in the Awake() function. Then, the atlas is registered in memory in advance, and the necessary textures can be retrieved from the registered atlas. In the second source, RequestAtlas is called once more. And the result is that the texture is not output normally. I may not understand it, but the conclusion is that I want to create a random subfolder of Resource to collect and refer to atlas textures.
     
  12. Leesh_NeoSonyx

    Leesh_NeoSonyx

    Joined:
    Aug 12, 2020
    Posts:
    10
    I am using a translator, so it may be difficult to understand the comments.