Unity AnimationClip 动画事件 修改

1、修改动画事件属性。 

    private static void Clone(AnimationEvent l, AnimationEvent r,float length)
    {
        r.functionName = l.functionName;
        r.floatParameter = l.floatParameter;
        r.intParameter = l.intParameter;
        r.objectReferenceParameter = l.objectReferenceParameter;
        r.stringParameter = l.stringParameter;
        r.time = l.time / length;
    }

    [MenuItem("Kit/Asset/修改AnimationEvent ObjParam的链接")]
    static void SetAnimClipEventObjectPath()
    {

        UnityEngine.Object[] selObjs = Selection.GetFiltered(typeof(UnityEngine.Object), SelectionMode.DeepAssets);
        if (selObjs == null || selObjs.Length == 0)
        {
            Debug.LogError("请选择需要添加帧事件的动画!");
            return;
        }

        foreach (UnityEngine.Object obj in selObjs)
        {
            if (obj.GetType() != typeof(GameObject))
                continue;
            GameObject fbx = (GameObject)obj;
            string fbxPath = AssetDatabase.GetAssetPath(fbx);
            UnityEngine.Object[] assets = AssetDatabase.LoadAllAssetsAtPath(fbxPath);
            foreach (UnityEngine.Object objGo in assets)
            {
                if (objGo.GetType() != typeof(AnimationClip))
                    continue;

                AnimationClip clipGo = (AnimationClip)objGo;

                AnimationEvent[] events = AnimationUtility.GetAnimationEvents(clipGo);
                if (events != null && events.Length != 0)
                {
                    List<AnimationEvent> events_clone = new List<AnimationEvent>();
                    for (int i = 0; i < events.Length;++i)
                    {
                        AnimationEvent newEvent = new AnimationEvent();
                          Clone(events[i], newEvent, clipGo.length); //传入长度。事件time 设置的长度的比例值

                        // 我这里实现 将object 链接,打断并把路径设置到stringparam内;
                        if (newEvent.objectReferenceParameter != null)
                        {
                            string str = newEvent.stringParameter;
                            string path = AssetDatabase.GetAssetPath(newEvent.objectReferenceParameter);
                            int idx = path.LastIndexOf('.');
                            string assets_path = "Assets/Resources/";
                            path = path.Substring(0, idx);
                            path = path.Substring(assets_path.Length);

                            int index = str.LastIndexOf('}');
                            if (index < 0)
                            {
                                newEvent.stringParameter = "{\"Prefab\":\"" + path + "\"}";
                            }
                            else
                            {
                                newEvent.stringParameter = newEvent.stringParameter.Substring(0, index);
                                newEvent.stringParameter += ",\"Prefab\":\"" + path + "\"}";
                            }
                            newEvent.objectReferenceParameter = null;
                        }

                        events_clone.Add(newEvent);
                    }

                    ModelImporter modelImporter = AssetImporter.GetAtPath(AssetDatabase.GetAssetPath(clipGo)) as ModelImporter;
                    if (modelImporter == null)
                        return;

                    SerializedObject serializedObject = new SerializedObject(modelImporter);
                    SerializedProperty clipAnimations = serializedObject.FindProperty("m_ClipAnimations");
                    for (int i = 0; i < modelImporter.clipAnimations.Length; i++)
                    {

                       if (modelImporter.clipAnimations[i].name == clipGo.name)
                        {
                            SerializedProperty m_clip = clipAnimations.GetArrayElementAtIndex(i);
                            SerializedProperty m_events = m_clip.FindPropertyRelative("events");

                            m_events.ClearArray();
                            foreach (AnimationEvent evt in events_clone)
                            {
                                m_events.InsertArrayElementAtIndex(m_events.arraySize);
                                SetEvent(m_events, m_events.arraySize - 1, evt);
                            }
                            break;
                        }

                    }

                    serializedObject.ApplyModifiedProperties();
                    AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(clipGo));
                    AssetDatabase.Refresh();
                }
            }
        }
    }

    static void SetEvent(SerializedProperty events, int index, AnimationEvent animationEvent)
    {
        //Debug.Log("set event");
        if (events != null && events.isArray)
        {
            if (index < events.arraySize)
            {
                events.GetArrayElementAtIndex(index).FindPropertyRelative("floatParameter").floatValue = animationEvent.floatParameter;
                events.GetArrayElementAtIndex(index).FindPropertyRelative("functionName").stringValue = animationEvent.functionName;
                events.GetArrayElementAtIndex(index).FindPropertyRelative("intParameter").intValue = animationEvent.intParameter;
                events.GetArrayElementAtIndex(index).FindPropertyRelative("objectReferenceParameter").objectReferenceValue = animationEvent.objectReferenceParameter;
                events.GetArrayElementAtIndex(index).FindPropertyRelative("data").stringValue = animationEvent.stringParameter;
                events.GetArrayElementAtIndex(index).FindPropertyRelative("time").floatValue = animationEvent.time;
            }
            else
            {
                Debug.LogWarning("Invalid Event Index");
            }
        }
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

虾米神探

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值