Unity3D -- 自动生成动画(AnimationClip)

原文地址:https://blog.csdn.net/honey199396/article/details/78713318

我们一个Prefab有很多个子物体,而且当前prefab使用了大量的动画状态,假如想将该Prefab动画更改过的属性在Idle中重新更改过来,一种比较暴力方法就是直接将需要更改的属性在Idle动画中K出来,但如果动画有更改的话,我们就需要更改Idle里面的属性,操作起来比较麻烦。然后花了一天时间,查找API终于搞定代码自动生成动画,下面代码是在编辑器模式下生成动画,废话说多了,直接上代码,全套的。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
// 在Unity菜单栏设置可以点击的菜单
public class ResetAnimation : Editor {

    private Transform m_AnimatorPrefab;

    [MenuItem("Animator/ResetProperty")]
    public static void  ResetProperty()
    {
        ScriptableWizard.DisplayWizard ("ChoosePrefab", "Apply");
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System;
using System.IO;

public class ChoosePrefabWizard : ScriptableWizard {

    public Transform m_AnimatorPrefab;
    public RuntimeAnimatorController m_AnimatorController;

    private const string m_AnimationPath = "Assets/Yummy/Art/Animation";
    private static Dictionary<string, bool> propertyDict = new Dictionary<string, bool>();

    static void CreateWizard()
    {
        ChoosePrefabWizard wizard = ScriptableWizard.DisplayWizard ("choose prefab");
        wizard.minSize = new Vector2 (300, 250);
    }

    void OnWizardCreate()
    {
        AnimationClip clip = new AnimationClip ();
        clip.frameRate = 24;
        foreach (AnimationClip animationClip in m_AnimatorController.animationClips) {
            foreach (EditorCurveBinding binding in AnimationUtility.GetCurveBindings(animationClip)) {
                string pathName = binding.path + "/" + binding.propertyName;
                if (!propertyDict.ContainsKey(pathName)) {
                    string[] paths = binding.path.Split ('/');
                    string transName = paths [paths.Length - 1];
                    Transform trans = null;
                    foreach (var item in m_AnimatorPrefab.GetComponentsInChildren()) {
                        if (item.name == transName) {
                            trans = item;
                            break;
                        }
                    }
                    if (trans == null) {
                        Debug.LogError ("can not find transform:" + transName);
                    }
                    Keyframe keyFrame = new Keyframe();
                    keyFrame.time = 0;
                    keyFrame.value = GetKeyFrameValue(trans, binding.propertyName);

                    AnimationCurve curve = new AnimationCurve ();
                    curve.AddKey (keyFrame);
                    clip.SetCurve (binding.path, binding.type, binding.propertyName, curve);
                    propertyDict.Add (pathName, true);
                }
            }

            foreach (var binding in AnimationUtility.GetObjectReferenceCurveBindings(animationClip)) {
                string pathName = binding.path + "/" + binding.propertyName;

                if (!propertyDict.ContainsKey(pathName)) {
                    string[] paths = binding.path.Split ('/');
                    string transName = paths [paths.Length - 1];
                    Transform trans = null;
                    foreach (var item in m_AnimatorPrefab.GetComponentsInChildren()) {
                        if (item.name == transName) {
                            trans = item;
                            break;
                        }
                    }
                    if (trans == null) {
                        Debug.LogError ("can not find transform:" + transName);
                    }

                    ObjectReferenceKeyframe[] keyframes = new ObjectReferenceKeyframe[1];
                    keyframes [0] = new ObjectReferenceKeyframe ();
                    keyframes [0].time = 0;
                    keyframes [0].value = GetObjectRenferenceValue (trans, binding.propertyName);
                    AnimationUtility.SetObjectReferenceCurve (clip, binding, keyframes);
                    propertyDict.Add (pathName, true);
                }
            }
        }
        if (!Directory.Exists(m_AnimationPath)) {
            System.IO.Directory.CreateDirectory (m_AnimationPath);
        }
        AssetDatabase.CreateAsset (clip, m_AnimationPath + "/" + m_AnimatorPrefab.name + "Idle.anim");
        AssetDatabase.SaveAssets ();
    }

    /// 
    /// 获取组件对应的KeyFrame值
    /// 
    /// The key frame value.
    /// Trans.
    /// Property name.
    float GetKeyFrameValue(Transform trans, string propertyName)
    {
        if (propertyName == "m_LocalScale.x") {
            return trans.localScale.x;
        } else if (propertyName == "m_LocalScale.y") {
            return trans.localScale.y;
        } else if (propertyName == "m_LocalScale.z") {
            return trans.localScale.z;
        } else if (propertyName == "m_LocalPosition.x") {
            return trans.localPosition.x;
        } else if (propertyName == "m_LocalPosition.y") {
            return trans.localPosition.y;
        } else if (propertyName == "m_LocalPosition.z") {
            return trans.localPosition.z;
        } else if (propertyName == "localEulerAnglesRaw.x") {
            return trans.localEulerAngles.x;
        } else if (propertyName == "localEulerAnglesRaw.y") {
            return trans.localEulerAngles.y;
        } else if (propertyName == "localEulerAnglesRaw.z") {
            return trans.localEulerAngles.z;
        } else if (propertyName == "m_Size.x") {
            return trans.GetComponent ().size.x;
        } else if (propertyName == "m_Size.y") {
            return trans.GetComponent ().size.y;
        } else if (propertyName == "m_Color.a") {
            return trans.GetComponent ().color.a;
        } else if (propertyName == "m_Color.r") {
            return trans.GetComponent ().color.r;
        } else if (propertyName == "m_Color.g") {
            return trans.GetComponent ().color.g;
        } else if (propertyName == "m_Color.b") {
            return trans.GetComponent ().color.b;
        }  else if (propertyName == "m_Enabled") {
            if (trans.GetComponent ().enabled) {
                return 1;
            } else {
                return 0;
            }
        } else {
            Debug.LogError (propertyName + " have not doing");
            return -1;
        }
    }

    /// 
    /// 获取组件的值引用
    /// 
    /// The object renference value.
    /// Trans.
    /// Property name.
    UnityEngine.Object GetObjectRenferenceValue(Transform trans, string propertyName)
    {
        if (propertyName == "m_Sprite") {
            return trans.GetComponent ().sprite;
        } else {
            Debug.LogError (propertyName + " have not doing");
            return null;
        }
    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153

ChoosePrefabWizard 打开一个窗口,你需要设置Prefab和AnimatorController,代码会自动创建一个Idle动画将更改的属性还原。 
这里写图片描述 
Animator Prefab和Animator Controller一定要配对,不然就会报错,如果你还更改了其他属性,你需要在GetKeyFrameValue 和GetObjectRenferenceValue 将更改的属性添加进去。


你可能感兴趣的:(unity学习笔记)