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

How to edit animation properties in C# script

Discussion in 'Animation' started by HughStones, Nov 3, 2017.

  1. HughStones

    HughStones

    Joined:
    Oct 31, 2017
    Posts:
    4
    I am attempting to change animation properties such as "loop time", "mirror" and etc in C# scripts in oder to edit a batch of animation clips. Howerver, it didn't work.

    Unity ver: 5.6.3


    Here is my test codes,
    AnimationClip newClip = AssetDatabase.LoadAssetAtPath(_file, typeof(AnimationClip)) as AnimationClip;
    AnimationClipSettings newSettings = AnimationUtility.GetAnimationClipSettings(newClip);
    //newClip.wrapMode = WrapMode.Loop;
    newSettings.loopTime = true;
    newSettings.mirror = true;
    AnimationUtility.SetAnimationClipSettings(newClip, newSettings);
    EditorUtility.SetDirty(newClip);

    AssetDatabase.SaveAssets();
    EditorApplication.SaveScene();

    Anyone could help me? Thanks!
     
  2. HughStones

    HughStones

    Joined:
    Oct 31, 2017
    Posts:
    4
    To be clear, "it didn't work" means the property of animation clip ("loop time", etc..) did not change when I saw it in editor mode.
     
  3. CaseyLee

    CaseyLee

    Joined:
    Jan 17, 2013
    Posts:
    40
    heres some code i used awhile back, if it helps you:

    Code (CSharp):
    1.   private void copyAnimSettings(AnimationClip Source, AnimationClip Targ)  ///SXAnimProperties SourceAProp
    2.     {
    3.         string ClipName = Source.name;
    4.         string SourceAsset = AssetDatabase.GetAssetPath(Targ);
    5.  
    6.         ModelImporter S_modelImporter = AssetImporter.GetAtPath(AssetDatabase.GetAssetPath(Source)) as ModelImporter;
    7.         ModelImporter T_modelImporter = AssetImporter.GetAtPath(SourceAsset) as ModelImporter;
    8.  
    9.         var clips = T_modelImporter.clipAnimations;
    10.         if (clips.Length == 0) clips = T_modelImporter.defaultClipAnimations;
    11.  
    12.         var S_clips = S_modelImporter.clipAnimations;
    13.         if (S_clips.Length == 0) S_clips = S_modelImporter.defaultClipAnimations;
    14.  
    15.         if(S_clips.Length == 0 || clips.Length == 0) {
    16.             Debug.Log("Skipped, clip was no good   -   s: " + S_clips.Length + "   t: " + clips.Length);
    17.         }
    18.  
    19.         foreach (var clip in clips)  {
    20.             if(clip.name != ClipName)   {
    21.                 Debug.Log("bad clip in list[d]" + ClipName + " " + clip.name);
    22.                 continue;
    23.             }
    24.  
    25.             foreach(var S_clip in S_clips)  {
    26.                 if (S_clip.name != ClipName)  {
    27.                     Debug.Log("bad clip in list[s] " + ClipName + " " + clip.name);
    28.                     continue;
    29.                 }
    30.  
    31.                 clip.keepOriginalOrientation = S_clip.keepOriginalOrientation;
    32.                 clip.keepOriginalPositionXZ = S_clip.keepOriginalPositionXZ;
    33.                 clip.keepOriginalPositionY = S_clip.keepOriginalPositionY;
    34.                 clip.heightFromFeet = S_clip.heightFromFeet;
    35.                 clip.heightOffset = S_clip.heightOffset;
    36.                 clip.rotationOffset = S_clip.rotationOffset;
    37.                 clip.lockRootHeightY = S_clip.lockRootHeightY;
    38.                 clip.lockRootPositionXZ = S_clip.lockRootPositionXZ;
    39.                 clip.lockRootRotation = S_clip.lockRootRotation;
    40.                 clip.loop = S_clip.loop;
    41.                 clip.loopPose = S_clip.loopPose;
    42.                 clip.loopTime = S_clip.loopTime;
    43.                 clip.mirror = S_clip.mirror;
    44.                 //clip.rotationOffset = S_clip.rotationOffset;
    45.                 clip.wrapMode = S_clip.wrapMode;
    46.                 clip.firstFrame = 1;
    47.  
    48.                 Debug.Log("finnished clip " + ClipName);
    49.  
    50.                 break;
    51.             }
    52.  
    53.             break;
    54.         }
    55.  
    56.  
    57.  
    58.         T_modelImporter.animationCompression = ModelImporterAnimationCompression.Off;
    59.         T_modelImporter.clipAnimations = clips;
    60.  
    61.         AssetDatabase.ImportAsset(SourceAsset);
    62.         AssetDatabase.SaveAssets();
    63.     }
    64.  
    65.     class AnimationClipSettings
    66.     {
    67.         SerializedProperty m_Property;
    68.  
    69.         private SerializedProperty Get(string property) { return m_Property.FindPropertyRelative(property); }
    70.  
    71.         public AnimationClipSettings(SerializedProperty prop) { m_Property = prop; }
    72.  
    73.         public float startTime { get { return Get("m_StartTime").floatValue; } set { Get("m_StartTime").floatValue = value; } }
    74.         public float stopTime { get { return Get("m_StopTime").floatValue; } set { Get("m_StopTime").floatValue = value; } }
    75.         public float orientationOffsetY { get { return Get("m_OrientationOffsetY").floatValue; } set { Get("m_OrientationOffsetY").floatValue = value; } }
    76.         public float level { get { return Get("m_Level").floatValue; } set { Get("m_Level").floatValue = value; } }
    77.         public float cycleOffset { get { return Get("m_CycleOffset").floatValue; } set { Get("m_CycleOffset").floatValue = value; } }
    78.  
    79.         public bool loopTime { get { return Get("m_LoopTime").boolValue; } set { Get("m_LoopTime").boolValue = value; } }
    80.         public bool loopBlend { get { return Get("m_LoopBlend").boolValue; } set { Get("m_LoopBlend").boolValue = value; } }
    81.         public bool loopBlendOrientation { get { return Get("m_LoopBlendOrientation").boolValue; } set { Get("m_LoopBlendOrientation").boolValue = value; } }
    82.         public bool loopBlendPositionY { get { return Get("m_LoopBlendPositionY").boolValue; } set { Get("m_LoopBlendPositionY").boolValue = value; } }
    83.         public bool loopBlendPositionXZ { get { return Get("m_LoopBlendPositionXZ").boolValue; } set { Get("m_LoopBlendPositionXZ").boolValue = value; } }
    84.  
    85.  
    86.         public bool keepOriginalOrientation { get { return Get("m_KeepOriginalOrientation").boolValue; } set { Get("m_KeepOriginalOrientation").boolValue = value; } }
    87.         public bool keepOriginalPositionY { get { return Get("m_KeepOriginalPositionY").boolValue; } set { Get("m_KeepOriginalPositionY").boolValue = value; } }
    88.         public bool keepOriginalPositionXZ { get { return Get("m_KeepOriginalPositionXZ").boolValue; } set { Get("m_KeepOriginalPositionXZ").boolValue = value; } }
    89.  
    90.  
    91.  
    92.         public bool heightFromFeet { get { return Get("m_HeightFromFeet").boolValue; } set { Get("m_HeightFromFeet").boolValue = value; } }
    93.         public bool mirror { get { return Get("m_Mirror").boolValue; } set { Get("m_Mirror").boolValue = value; } }
    94.     }
    95.  
    96.  
    97.  
    98.  
     
  4. HughStones

    HughStones

    Joined:
    Oct 31, 2017
    Posts:
    4
    Hi Lee,

    It works!

    I appreciated for your solution!