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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Serializing Sprites in a Standalone Build

Discussion in 'Scripting' started by turbosheep, Jan 15, 2017.

  1. turbosheep

    turbosheep

    Joined:
    Jul 19, 2013
    Posts:
    25
    Hi, forum!

    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!
     
  2. Dameon_

    Dameon_

    Joined:
    Apr 11, 2014
    Posts:
    542
    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.
     
  3. turbosheep

    turbosheep

    Joined:
    Jul 19, 2013
    Posts:
    25
    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:

    Resources/art/chapter1/backgrounds/area1/backgroundX.png
    Resources/art/chapter3/backgrounds/area3/backgroundY.png
    Resources/art/chapter8/backgrounds/area7/backgroundZ.png
    Resources/art/chapter9/items/itemX.png

    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?
     
  4. Dameon_

    Dameon_

    Joined:
    Apr 11, 2014
    Posts:
    542
    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.
     
    turbosheep likes this.
  5. turbosheep

    turbosheep

    Joined:
    Jul 19, 2013
    Posts:
    25
    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!