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.

Problem When Edit AnimationClip With Script

Discussion in 'Animation' started by xianfeng, Nov 19, 2014.

  1. xianfeng

    xianfeng

    Joined:
    Nov 19, 2014
    Posts:
    1
    I got some problem when I extend Unity Editor. I got extra animation frames when I use the method SetCurve in Animation Clip. To confirm these problem is true,i try the following test:

    First,i create a fragment of animation named Test manually,and edit the animation frame,as figure 1.

    我在扩展UnityEditor的时候遇到一些问题,在调用AnimationClip的SetCurve方法时会莫名其妙的创建出许多动画帧,为了搞清楚问题所在,我做了如下测试:

    1.首先在UnityEditor中手动创建一个动画片段Test,并编辑动画帧如图1
    upload_2014-11-19_10-27-58.png
    图1

    I use the follow code to modify the animation's fragment.(In fact,the code dose nothing to modify the animation,just revoke the API that Unity supply to read the data and then write it back).The code is the following:

    2.使用如下代码对动画片段进行修改(实际上代码中没有进行任何修改,只是调用Unity提供的API将原有的动画数据读出来再写回去)代码如下:

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEditor;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. public class ChangePath : EditorWindow {
    6.     Vector2 scrollPos;
    7.     AnimationClip clip;
    8.     AnimationClip lastClip;
    9.     AnimationClipCurveData[] someData;
    10.     bool showCurves;
    11.     string before;
    12.     string after;
    13.     Stack<AnimationClipCurveData[]> backup = new Stack<AnimationClipCurveData[]>();
    14.     [MenuItem("MyPlugin/ChangePath")]
    15.     static void Init() {
    16.         ChangePath window = (ChangePath)EditorWindow.GetWindow(typeof(ChangePath));
    17.         window.Show();
    18.     }
    19.     void OnGUI () {
    20.         EditorGUILayout.BeginVertical ();
    21.         scrollPos = EditorGUILayout.BeginScrollView (scrollPos);
    22.         EditorGUILayout.LabelField("拖入要修改的动画片段:");//Drag in the animation fragment
    23.         clip=EditorGUILayout.ObjectField(clip,typeof(AnimationClip),false)as AnimationClip;
    24.         if (clip != null) {
    25.             if(clip!=lastClip){
    26.                 someData = AnimationUtility.GetAllCurves(clip,true);
    27.                 lastClip = clip;
    28.             }
    29.             if(someData!=null && someData.Length>0){
    30.                 if (GUILayout.Button("修改")) {//modify
    31.                     //清空动画曲线
    32.                     //clear the curve in animation
    33.                     clip.ClearCurves();
    34.                     foreach (AnimationClipCurveData data in someData)  {
    35.                         //根据clip中原有的AnimationClipCurveData
    36.                         //重新生成曲线
    37.                         //rebuild the curve
    38.                         clip.SetCurve(data.path, data.type,
    39.                             data.propertyName, data.curve);
    40.                     }
    41.                 }
    42.             }
    43.         }
    44.         EditorGUILayout.EndScrollView ();
    45.         EditorGUILayout.EndVertical ();
    46.     }
    47. }
    48.  
    the key code is the following,as figure 2.

    其中关键代码如图2:
    upload_2014-11-19_10-28-59.png
    图2

    I save the project after execute the code above,then refresh the animation window, the display as figure 3.

    3.执行完以上代码后保存工程刷新Amination窗口显示如图3:

    upload_2014-11-19_10-29-20.png
    图3

    There are more key frame in the rotate curve,the other curve as same as before. So , I compare the fragment with that I made before,It's 21.1k before I modify,after I modified,it's 24.5k,it says there key frame used extra resources.

    看到在旋转的曲线上莫名的出现了许多关键帧,而其他曲线和之前一样。于是我又比较了动画片段修改前后的大小,修改前是21.1k而修改后变为24.5k,也就是说这些多出来的关键帧是额外占用资源的。

    Unity 4.5
     
  2. woodlee

    woodlee

    Joined:
    Dec 1, 2014
    Posts:
    5
    why ,no body answer!