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. Dismiss Notice

Serializing Sprites?

Discussion in 'UGUI & TextMesh Pro' started by TSRajesh, Nov 6, 2014.

  1. TSRajesh

    TSRajesh

    Joined:
    Jun 19, 2013
    Posts:
    68
    BinaryFormatter gives error that Sprite is not serializable. How to do it?
    Or, is there a way to get the file name from the sprite and then serialize as string?

    Thanks & Regards
    Raj
     
  2. TSRajesh

    TSRajesh

    Joined:
    Jun 19, 2013
    Posts:
    68
    I got it working.. Though it may not be the best method...
    I got the path of the sprite image file using AssetDatabase.GetAssetPath(), (And retrieved the file name only using Path.GetFileNameWithoutExtension() ), and serialized it as string.
    Also, I had to put my sprite assets under the resources folder. Appears to be working fine.
     
  3. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,398
    Putting sprites in Resources is not recommended because if you do that they can't be auto-atlased (if you have Pro).

    --Eric
     
  4. TSRajesh

    TSRajesh

    Joined:
    Jun 19, 2013
    Posts:
    68
    Thanks for the response.
    - No I am with the Free Version.
    - Until i find a better solution, this works...
    - I have to animate the sprite (Nothing fancy.. Just size / Rotation), Which, is not possible in atlas if my understanding is correct.

    Thanks & Regards
     
  5. gametr4x

    gametr4x

    Joined:
    Apr 22, 2009
    Posts:
    84
    I'd like to bump this to see if anybody has found a solution to actually do this without putting it all in the resources folder, thus restricting yourself to the sprites in those folders and foregoing the huge benefits of not having to know what atlas a sprite is in in order for it to batch.

    Essentially, I just want to do what Unity already does: Find a sprite by a serialized reference at runtime. However, not from an instantiated MonoBehaviour. So, like this:

    Code (CSharp):
    1.  
    2. [System.Serializable]
    3. public class ItemData
    4. {
    5. //this doesn't serialize, sadly... and its GUID cannot be used at runtime (D'oh!)
    6. UnityEngine.Sprite sprite;
    7. }
    8.  
    I can imagine why we're not allowed to do this (it'd be next to impossible to ensure an asset is compiled to a runtime build if we're all haphazardly storing references outside of scene files), but it'd be really nice to find away to do it anyway.

    Here's how a Scene file stored the references, for instance: {fileID: 21300000, guid: 0514b8ab7013b4d90ad589edb6f17993, type: 3}

    EDIT: Created a generic workaround for this: http://forum.unity3d.com/threads/manually-update-serialized-asset-references-for-build.297651/ (might be even more generic by turning DataContainer into a template class, but don't know if that would still work)
     
    Last edited: Feb 12, 2015
  6. BionicWombatGames

    BionicWombatGames

    Joined:
    Oct 5, 2020
    Posts:
    33
    7 Years Later..:

    You can avoid AssetDatabase.GetAssetPath() if your sprites (for this instance) are all in the same (Resources) folder. Then you can cheat a bit with something like this:

    Code (CSharp):
    1. [Serializable]
    2. public struct ItemData{
    3.   [SerializeField] private string spritePath;
    4.   public Sprite portrait => Resources.Load<Sprite>(spritePath);
    5.  
    6.   public ItemData(Sprite sprite) {
    7.     spritePath = sprite.name;
    8.   }
    9. }
    10.  
    This is fully serializable and is an opaque facade on just using the Sprite directly: in other words, you should be able to sub this in without any other changes to your code.