To the best of my knowledge, Sprite components cannot be serialized, just like GameObjects etc.
However, I need to load sprites dynamically (item images, backgrounds, all depending on stored save data).
My solution was to simply store a sprite’s path and then use this path to load the appropriate sprite via Resources.Load(). This works fine in the editor. Not so in a standalone build.
You see, in order to get the sprite’s path, AssetDatabase.GetAssetPath() is required, which depends on UnityEditor namespace which cannot be included in a standalone build.
Which leaves me with a conundrum: I cannot serialize the sprite itself nor get the sprite’s path during runtime.
The only other thing I can think of is to add a direct path reference, through a Monobehvavior script variable, to every single gameobject with a Sprite component, which sounds incredibly inefficient.
Basically, I’m looking to be educated on the ‘proper’ way to dynamically load sprites in a standalone build.
I’ve read about AssetBundles, but from what I can understand AssetBundles are not exactly what I’m looking for.
Any help is greatly appreciated - thanks in advance!
AssetDatabase.GetAssetPath() shouldn’t be required. If your sprites are in a folder in your Resources folder, then Resources.Load() will find them just fine. For example, if your sprite was named Fish.png in Resources/Sprites, you would use Resources.Load(“Sprites/Fish”). This should work just as well in a build as in the editor.
You are right, of course, but your solution requires all sprites to be in the same directory, right?
After all, I can only get the sprite name during run-time, not the full path.
That is why I started using GetAssetPath() in the first place - I used your method, originally.
Since the game will have a lot of sprites, I have now organized my sprites more specifically:
So, just getting the name “backgroundX” is not really going to help me here.
Is the only way to do this without using GetAssetPath() to just dump all my sprites in one general folder sprites?
Ah, I see, you want to auto-serialize the path. In that case, yes, you do need to have a field holding a path to each sprite’s location. You can write an editor script to automagically fill in the path using GetAssetPath.
Then I suppose my idea wasn’t as stupid as I originally thought.
I will take a look into that ‘automagical editor script’ solution, don’t have much experience with that.
Thanks a lot, Dameon!