我们一个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一定要配对,不然就会报错,如果你还更改了其他属性,你需要在GetKeyFrameValue
和GetObjectRenferenceValue
将更改的属性添加进去。