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

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?

Do I understand correctly that you want to get access to all Sprites imported with your PSB file? You can use AssetDatabase.LoadAllAssetsAtPath()

var psdPath = "Assets/PathToYourFile.psb";
foreach (var obj in AssetDatabase.LoadAllAssetsAtPath(psdPath))
{
    if (obj is Sprite sprite)
    {
        // here you can iterate over your sprites
    }
}
1 Like

Thank you! That’s exactly what I need!

1 Like

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)

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.

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!

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.

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 :slight_smile: 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 :slight_smile: <3

Hope you have a lovely holiday!

1 Like

Great to hear that you found a solution, and thanks for sharing!
Have a great holiday!

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.

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?

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”

AssetDatabase is part of the UnityEditor assembly, which is only available in the editor.