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

Serialization of AnimationCurves

Discussion in 'Scripting' started by woistjadefox, Jun 22, 2016.

  1. woistjadefox

    woistjadefox

    Joined:
    Oct 24, 2014
    Posts:
    11
    Hi Unity Community!

    I'm actually working on a save game system and I'm not so happy with the existing solutions on the asset store, since they handle things too much, or not enough ;-)

    I just have a couple questions.

    When I started using the BinaryFormatter to save custom classes I recognized that for every Unity specific type I need to create an ISerializationSurrogate selector, which is kind of a pain in the ass. Is there already a project who did map most Unity types or is there library of these selectors probably?

    When I did researches with the official JsonUtility of Unity I found it pretty neat. Then I realized that not all types are serializable which normally are in the Unity inspector. The JsonUtility is not serializing AnimationCurves for example. And I do change some AnimationCurves during runtime, that's the reason I need to serialize and to save them. Any idea how to achieve that?

    Thanks for your answers in advance.
     
  2. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,376
    I don't know of any public libraries that specifically cover all the surrogates for unity.

    I usually write them as I come across the need for them. It's usually not a whole lot of work.

    I haven't added AnimationCurve to my serialization library yet... probably going to do that when I get home tonight. I'll post it here when I'm done.
     
  3. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
    Looks pretty easy to serialize. A couple float values, a couple enumerations and an indexer. It's really just a collection of keyframes with some additional properties.
     
  4. woistjadefox

    woistjadefox

    Joined:
    Oct 24, 2014
    Posts:
    11
    Would appreciate to see your example. I'm just wondering why the JsonUtility isn't able to do it, since I thought it can serialize all the properties that also show up on the inspector. Does anybody know if there is a list with the supported Unity types for the JsonUtility?
     
  5. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
  6. jipsen

    jipsen

    Joined:
    May 22, 2018
    Posts:
    37
    Can you help me on this one? I really need to serialize it but I'm quite lost with your comment.
     
  7. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,181
    You'd serialize the value of everything in the AnimationCurve - so the keys and wrapmode. The keys are just a set of floats and an enum member, so you could serialize them like that.

    Then on deserialization, you'd create a new animation curve and fill the data back in.

    So this is a classic "your question is hard to answer, what have you tried, what are you unable to do?"
     
    dbdenny and jipsen like this.
  8. dbdenny

    dbdenny

    Joined:
    Mar 13, 2020
    Posts:
    12
    I have got an idea but not tried yet. As we known, Unity can serialize AnimationCurve as a field of ScriptableObject to .asset file.
    So we can preserve an empty field of a class (let's call it CurveDB) which is inherited from ScriptableObject, load the asset of CurveDB in runtime, and create the AnimationCurve from the data we want to serialize, and set CurveDB dirty, then AssetDatabase.SaveAssets(curveDB); (As I know, Unity will report an error or warning when we try to save runtime data, but the data can be saved indeed...)

    Here is the presudo code (should be completed):
    Code (CSharp):
    1.     public class CurveDB : ScriptableObject
    2.     {
    3.         public List<AnimationCurve> curves = new List<AnimationCurve>();
    4.         private static CurveDB m_Instance;
    5.  
    6.         public static CurveDB Instance
    7.         {
    8.             get
    9.             {
    10.                 if (m_Instance == null)
    11.                 {
    12.                     m_Instance = AssetDatabase.LoadAssetAtPath<CurveDB>("<path of curveDB related to Assets>");
    13.                 }
    14.  
    15.                 return m_Instance;
    16.             }
    17.         }
    18.  
    19.         // use this to save AnimationCurve
    20.         public static void SaveCurve(AnimationCurve runtimeCurve)
    21.         {
    22.             var savedCurveData = new AnimationCurve(runtimeCurve.keys);
    23.             savedCurveData.preWrapMode = runtimeCurve.preWrapMode;
    24.             // assign other fields which you want to save here
    25.             // ...
    26.             Instance.curves.Add(new AnimationCurve());
    27.             EditorUtility.SetDirty(Instance);
    28.             AssetDatabase.SaveAssetIfDirty(Instance);
    29.         }
    30.     }