解决在animator子物体下无法调节animation问题
//放在editor下
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
public class ChangeAnimationInspector : Editor
{
public override void OnInspectorGUI()
{
base.DrawDefaultInspector();
if (GUILayout.Button("我要修改子动画!"))
{
Animation thisTransform = (Animation)base.target;
SaveChangeAnimatorInfo saInfo = thisTransform.transform.GetComponentInChildren<SaveChangeAnimatorInfo>();
GameObject gobjParent;
if (saInfo == null)
{
gobjParent = new GameObject("gobjParent!");
saInfo = gobjParent.AddComponent<SaveChangeAnimatorInfo>();
gobjParent.transform.SetParent(thisTransform.transform);
}
else
{
gobjParent = saInfo.gameObject;
}
Animator[] animList = thisTransform.GetComponentsInParent<Animator>();
for (int i = 0; i < animList.Length; i++)
{
GameObject gobj = new GameObject("gobj");
gobj.transform.SetParent(gobjParent.transform);
UnityEditorInternal.ComponentUtility.CopyComponent(animList[i]);
UnityEditorInternal.ComponentUtility.PasteComponentAsNew(gobj);
ChangeAnimatorInfomation caiInfo = gobj.AddComponent<ChangeAnimatorInfomation>();
caiInfo.belongTo = animList[i].gameObject;
DestroyImmediate(animList[i]);
}
}
if (GUILayout.Button("我要还原!"))
{
Animation thisTransform = (Animation)base.target;
SaveChangeAnimatorInfo[] saInfoList = thisTransform.transform.GetComponentsInChildren<SaveChangeAnimatorInfo>();
for (int i = 0; i < saInfoList.Length; i++)
{
saInfoList[i].ReturnBelongTo();
}
}
//if (GUI.changed)
//{
// EditorUtility.SetDirty(target);
//}
//if (caInfo.type == 1)
//{
// caInfo.gobjParent = new GameObject("gobjParent!");
// caInfo.gobjParent.transform.SetParent(thisTransform.transform);
// Animator[] animList = thisTransform.GetComponentsInParent<Animator>();
// for (int i = 0; i < animList.Length; i++)
// {
// GameObject gobj = new GameObject("gobj" + i.ToString());
// gobj.transform.SetParent(caInfo.gobjParent.transform);
// UnityEditorInternal.ComponentUtility.CopyComponent(animList[i]);
// UnityEditorInternal.ComponentUtility.PasteComponentAsNew(gobj);
// ChangeAnimatorInfomation mation = gobj.AddComponent<ChangeAnimatorInfomation>();
// mation.belongTo = animList[i].gameObject;
// caInfo.animatorDict.Add(gobj, animList[i].gameObject);
// DestroyImmediate(animList[i]);
// }
// caInfo.type = 2;
//}
//else if (caInfo.type == 2)
//{
// foreach (GameObject item in caInfo.animatorDict.Keys)
// {
// UnityEditorInternal.ComponentUtility.CopyComponent(item.GetComponent<Animator>());
// Animator componentOld = caInfo.animatorDict[item].GetComponent<Animator>();
// if (!componentOld)
// {
// UnityEditorInternal.ComponentUtility.PasteComponentAsNew(caInfo.animatorDict[item]);
// }
// else
// {
// UnityEditorInternal.ComponentUtility.PasteComponentValues(componentOld);
// }
// }
// caInfo.animatorDict.Clear();
// DestroyImmediate(caInfo.gobjParent);
// caInfo.gobjParent = null;
// caInfo.type = 1;
//}
//if (GUILayout.Button("我要添加控制子动画脚本!"))
//{
// Animation thisTransform = (Animation)base.target;
// UIAnimator[] uiaList = thisTransform.GetComponents<UIAnimator>();
// for (int i = 0; i < uiaList.Length; i++)
// {
// DestroyImmediate(uiaList[i]);
// }
// foreach (AnimationState state in thisTransform)
// {
// UIAnimator uia = thisTransform.gameObject.AddComponent<UIAnimator>();
// uia.ani = thisTransform;
// uia.animaName = state.name;
// }
//}
}
}
[CanEditMultipleObjects()]
[CustomEditor(typeof(Animation), true)]
public class ChangeAnimationInspectorP : ChangeAnimationInspector
{
}
//以下为管理的,直接放在script下即可
using UnityEngine;
using System.Collections;
public class ChangeAnimatorInfomation : MonoBehaviour
{
public GameObject belongTo;
public void ReturnBelong()
{
if (belongTo != null)
{
UnityEditorInternal.ComponentUtility.CopyComponent(this.transform.GetComponent<Animator>());
Animator componentOld = belongTo.GetComponent<Animator>();
if (!componentOld)
{
UnityEditorInternal.ComponentUtility.PasteComponentAsNew(belongTo);
}
else
{
UnityEditorInternal.ComponentUtility.PasteComponentValues(componentOld);
}
}
DestroyImmediate(gameObject);
}
}
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class SaveChangeAnimatorInfo : MonoBehaviour
{
public void ReturnBelongTo()
{
ChangeAnimatorInfomation[] mationList = transform.GetComponentsInChildren<ChangeAnimatorInfomation>();
for (int i = 0; i < mationList.Length; i++)
{
mationList[i].ReturnBelong();
}
DestroyImmediate(gameObject);
}
}