Search Unity

How convert Legacy animations to the new System

Discussion in 'Editor & General Support' started by chechoggomez, Sep 18, 2014.

  1. chechoggomez

    chechoggomez

    Unity Technologies

    Joined:
    Feb 25, 2013
    Posts:
    91
    If you need to convert Legacy animations to the new system, specifically to work around this problem with Materials and be able to edit the animations in the new Animation Window :

    http://issuetracker.unity3d.com/issues/cant-animate-color-value-of-materials?page=6#comments

    You can use this code to covert those animations:

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using UnityEditor;
    4. using System.Collections;
    5.  
    6. public class AnimationConverter : EditorWindow {
    7.  
    8.  
    9.     AnimationClip target;
    10.     SerializedProperty animationType;
    11.  
    12.     [MenuItem("Window/AnimationConverter")]
    13.     static void OpenWindow()
    14.     {
    15.         // Get existing open window or if none, make a new one:
    16.         EditorWindow.GetWindow(typeof(AnimationConverter));
    17.  
    18.     }
    19.  
    20.     void OnGUI()
    21.     {
    22.         target = EditorGUILayout.ObjectField("Target", target, typeof(AnimationClip), true) as AnimationClip;
    23.  
    24.         if (GUILayout.Button("Convert"))
    25.         {
    26.             SerializedObject serializedObject = new SerializedObject(target);
    27.             animationType = serializedObject.FindProperty("m_AnimationType");
    28.             animationType.intValue = 2;
    29.             serializedObject.ApplyModifiedProperties();
    30.      
    31.  
    32.             EditorCurveBinding[]  allCurves = AnimationUtility.GetCurveBindings (target);
    33.  
    34.             foreach(var data in allCurves)
    35.             {
    36.                 Debug.Log ("path" + data.path);
    37.                 Debug.Log ("propertyName" + data.propertyName);
    38.                 Debug.Log ("type" + data.type);
    39.  
    40.                 if(data.type == typeof(UnityEngine.Material))
    41.                 {
    42.                     AnimationCurve curve = AnimationUtility.GetEditorCurve(target, data);
    43.  
    44.                     EditorCurveBinding newBindings = new EditorCurveBinding();
    45.                     newBindings.path = data.path;
    46.                     newBindings.propertyName = "material." + data.propertyName;
    47.                     newBindings.type = typeof(UnityEngine.MeshRenderer);
    48.  
    49.                     AnimationUtility.SetEditorCurve(target, newBindings, curve);
    50.                 }
    51.  
    52.                 AnimationUtility.SetEditorCurve(target, data, null);
    53.  
    54.             }
    55.         }
    56.     }
    57. }
    58.  
     
  2. steinbitglis

    steinbitglis

    Joined:
    Sep 22, 2011
    Posts:
    254
    These days you need to do:
    Code (csharp):
    1. animationType = serializedObject.FindProperty("m_Legacy");
    2. animationType.boolValue = false;
     
  3. defaxer

    defaxer

    Joined:
    Nov 15, 2010
    Posts:
    140
    In fact you dont need any scripting, just switch inspector to "Debug" mode and change AnimationType to 2 (1 - legacy animation, 2 - mecanim). Inspector's "Debug" mode has a lot of hidden gems :)
     
    DarkDeivel, jlnorris and theANMATOR2b like this.