Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice
  2. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  3. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Bug on AnimationCurve when stripping is enabled

Discussion in '2017.3 Beta' started by Inter-Illusion, Oct 18, 2017.

  1. Inter-Illusion

    Inter-Illusion

    Joined:
    Jan 5, 2014
    Posts:
    598
    I just found that in Unity 2017.3 when in WebGL or Android with Stripping enabled, the AnimationCurve Keyframe properties are not been serialized correctly (at least when using C# xml serializer).
    The following code works in Editor, and Android (with stripping disabled)
    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using System.Xml.Serialization;
    6. using System.Xml;
    7. using System.Xml.Schema;
    8. public class Data
    9. {
    10.  public AnimationCurve curve;
    11. }
    12.  
    13. public class BugExample : MonoBehaviour
    14. {
    15.  void Start ()
    16.  {
    17.   string debugLog = "";
    18.   var data = new Data ();
    19.   data.curve = new AnimationCurve (new Keyframe(0, 0), new Keyframe(1, 1));
    20.  
    21.   var serializer = new System.Xml.Serialization.XmlSerializer(typeof(Data));
    22.  
    23.  
    24.   // Save to XML
    25.   var writer = new System.IO.StringWriter(System.Globalization.CultureInfo.InvariantCulture);
    26.   serializer.Serialize(writer, data);
    27.   var xmlString = writer.ToString ();
    28.  
    29.  
    30.   debugLog += xmlString;
    31.  
    32.  
    33.   // Read from Xml
    34.   var reader = new System.IO.StringReader(xmlString);
    35.   var data1 = serializer.Deserialize(reader) as Data;
    36.  
    37.  
    38.   debugLog += string.Format("\nNum Keys: {0}", data1.curve.keys.Length);
    39.   foreach (var key in data1.curve.keys)
    40.   {
    41.    debugLog += string.Format ("\n  Keys: {0} - {1}", key.time, key.value);
    42.   }
    43.  
    44.   Debug.Log (debugLog);
    45.   GetComponent<UnityEngine.UI.Text> ().text = debugLog;
    46.  }
    47. }
    48.  
    Producing the following output:
    Code (csharp):
    1.  
    2. <?xml version="1.0" encoding="utf-16"?>
    3. <Data xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    4.   <curve>
    5.     <keys>
    6.       <Keyframe>
    7.         <time>0</time>
    8.         <value>0</value>
    9.         <inTangent>0</inTangent>
    10.         <outTangent>0</outTangent>
    11.         <tangentMode>0</tangentMode>
    12.       </Keyframe>
    13.       <Keyframe>
    14.         <time>1</time>
    15.         <value>1</value>
    16.         <inTangent>0</inTangent>
    17.         <outTangent>0</outTangent>
    18.         <tangentMode>0</tangentMode>
    19.       </Keyframe>
    20.     </keys>
    21.     <preWrapMode>ClampForever</preWrapMode>
    22.     <postWrapMode>ClampForever</postWrapMode>
    23.   </curve>
    24. </Data>
    25. Num Keys: 2
    26.   Keys: 0 - 0
    27.   Keys: 1 - 1
    28.  
    But when in WebGL or Android with Stripping enabled, it fails to deserialize the Keyframe struct, and produces this output:
    Code (csharp):
    1.  
    2. <?xml version="1.0" encoding="utf-16"?>
    3. <Data xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    4.   <curve>
    5.     <keys>
    6.       <Keyframe />
    7.       <Keyframe />
    8.     </keys>
    9.   </curve>
    10. </Data>
    11. Num Keys: 2
    12.   Keys: 0 - 0
    13.   Keys: 0 - 0
    14.  
    To make it work in my own project, I had to catch the event for unknown xml elements, parse the xml and then use reflection to set the value in the private keyframe fields. But that's far from ideal!
    Hope that helps,
    Frank
     
  2. LeonhardP

    LeonhardP

    Unity Technologies

    Joined:
    Jul 4, 2016
    Posts:
    3,132
    Hi Inter-Illusion,
    Thanks for reporting this issue. We're investigating!
     
  3. Inter-Illusion

    Inter-Illusion

    Joined:
    Jan 5, 2014
    Posts:
    598
    I just got an email from Unity with a proper solution using link.xml to avoid stripping the Keyframe properties.
    Here it is in case someone else faces the same problem
    Thanks Unity!!
     
    piersb likes this.