新旧animator编辑问题

解决在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);
    }
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值