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

我们一个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<ChoosePrefabWizard> ("ChoosePrefab", "Apply");
    }
}
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<ChoosePrefabWizard> ("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<Transform>()) {
                        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<Transform>()) {
                        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 ();
    }

    /// <summary>
    /// 获取组件对应的KeyFrame值
    /// </summary>
    /// <returns>The key frame value.</returns>
    /// <param name="trans">Trans.</param>
    /// <param name="propertyName">Property name.</param>
    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<SpriteRenderer> ().size.x;
        } else if (propertyName == "m_Size.y") {
            return trans.GetComponent<SpriteRenderer> ().size.y;
        } else if (propertyName == "m_Color.a") {
            return trans.GetComponent<SpriteRenderer> ().color.a;
        } else if (propertyName == "m_Color.r") {
            return trans.GetComponent<SpriteRenderer> ().color.r;
        } else if (propertyName == "m_Color.g") {
            return trans.GetComponent<SpriteRenderer> ().color.g;
        } else if (propertyName == "m_Color.b") {
            return trans.GetComponent<SpriteRenderer> ().color.b;
        }  else if (propertyName == "m_Enabled") {
            if (trans.GetComponent<SpriteRenderer> ().enabled) {
                return 1;
            } else {
                return 0;
            }
        } else {
            Debug.LogError (propertyName + " have not doing");
            return -1;
        }
    }

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

}

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

  • 3
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值