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

How to get access to a specific sprite of imported PSB file?

Discussion in '2D' started by Dinochrome, May 26, 2022.

  1. Dinochrome

    Dinochrome

    Joined:
    Apr 23, 2018
    Posts:
    19
    Hello everyone! I'm trying to get a sprite from imported PSB file. This can be easily done in Editor, but I need to get access from code. And I can't figure out how to do this. Can anyone give me an advice?

     

    Attached Files:

  2. MarekUnity

    MarekUnity

    Unity Technologies

    Joined:
    Jan 6, 2017
    Posts:
    179
    Do I understand correctly that you want to get access to all Sprites imported with your PSB file? You can use AssetDatabase.LoadAllAssetsAtPath()
    Code (CSharp):
    1. var psdPath = "Assets/PathToYourFile.psb";
    2. foreach (var obj in AssetDatabase.LoadAllAssetsAtPath(psdPath))
    3. {
    4.     if (obj is Sprite sprite)
    5.     {
    6.         // here you can iterate over your sprites
    7.     }
    8. }
     
    Dinochrome likes this.
  3. Dinochrome

    Dinochrome

    Joined:
    Apr 23, 2018
    Posts:
    19
    Thank you! That's exactly what I need!
     
    MarekUnity likes this.
  4. Dinochrome

    Dinochrome

    Joined:
    Apr 23, 2018
    Posts:
    19
    I just realized this works only in Editor. I can't make an Android build for example. Is there any other way? (Except System.IO.File of course)
     
  5. MarekUnity

    MarekUnity

    Unity Technologies

    Joined:
    Jan 6, 2017
    Posts:
    179
    That's because AssetDatabase.LoadAllAssetsAtPath uses editor code that's unavailable in runtime. Put your script in the Editor folder and you should be able to build your project without errors.
     
  6. HuldaGnodima

    HuldaGnodima

    Joined:
    Oct 10, 2016
    Posts:
    106
    Hi!

    This is the first hit I got from Google when I searched for "How to access specific sprite inside a psb-file", so I thought I could ask that here as a follow up question (and maybe others will find it helpful too who end up here).

    Could you help me with how to get a specific sprite inside a psb file (not all files)? I'm worried that iterating through all the sprites each time might be performance heavy, so I was wondering if there's a better way to get just one specifc sprite via its name?

    I would like to use it to toggle different facial expressions of a character.

    Thanks in advance!
     
  7. Ted_Wikman

    Ted_Wikman

    Unity Technologies

    Joined:
    Oct 7, 2019
    Posts:
    883
    We do not have an API to fetch an individual Sprite from an imported .psd/.psb. What Marek showed is the appropriate way to find Sprites generated on import by the PSD Importer.

    Before trying to optimize this step, I would suggest you to first profile your implementation, to see if it really has the impact you believe it does.
     
  8. HuldaGnodima

    HuldaGnodima

    Joined:
    Oct 10, 2016
    Posts:
    106
    Thank you so much for your reply!
    Yes, I for sure shouldn't optimize before it even turns out to be a problem. It would probably not effect performance Thanks for the heads up!

    I ended up using the PSB-files in tandem with 2DAnimation with the Sprite Library and Sprite Resolver. That way I could just call each sprite in the PSB by sprite-name in the Sprite Resolver. It works wonderfully :) So I got the function I was looking for like that. But otherwise I would give your proposed solution above a go!

    Thank you for your reply and quick help :) <3

    Hope you have a lovely holiday!
     
    Last edited: Dec 21, 2022
    Ted_Wikman likes this.
  9. Ted_Wikman

    Ted_Wikman

    Unity Technologies

    Joined:
    Oct 7, 2019
    Posts:
    883
    Great to hear that you found a solution, and thanks for sharing!
    Have a great holiday!
     
  10. EVISharp

    EVISharp

    Joined:
    Jun 19, 2019
    Posts:
    2
    Hi, looks like the "AssetDatabase" solution doesn't work when you need to build your game outside of editor. Do we have any options beside SpriteLibrary + Sprite Resolver? I would like to gather a script that will automatically load sprites from PSD file, so no drag & drop in UI required.
     
  11. Ted_Wikman

    Ted_Wikman

    Unity Technologies

    Joined:
    Oct 7, 2019
    Posts:
    883
    I'm not sure I understand your limitations. What do you mean by "build your game outside of editor"? Are you loading in a .psd at runtime?
     
  12. EVISharp

    EVISharp

    Joined:
    Jun 19, 2019
    Posts:
    2
    I mean project build. If you use "Build and run", AssetDatabase will not be available and it will cause an error "The name 'AssetDatabase' does not exist in the current context"
     
  13. Ted_Wikman

    Ted_Wikman

    Unity Technologies

    Joined:
    Oct 7, 2019
    Posts:
    883
    AssetDatabase is part of the UnityEditor assembly, which is only available in the editor.