美文网首页
2019-07-11 【UnityTips】代码添加动画事件

2019-07-11 【UnityTips】代码添加动画事件

作者: 持刀的要迟到了 | 来源:发表于2019-07-11 11:28 被阅读0次

    1.外部导入得带骨骼(.FBX)得动画事件不能保存,因为是只读得。

    image.png
    https://answers.unity.com/questions/187907/how-to-add-keyframes-on-imported-read-only-animati.html

    2.非只读动画可以在编辑器模式下保存。代码如下:

    using UnityEditor;
    using UnityEngine;
    
    
    [ExecuteInEditMode]
    public class AnimEventGenerator : MonoBehaviour
    {
        public AnimEventGeneratorAsset asset;
        public bool Generate = false;
    
        private void Update()
        {
            if(Generate)
            {
                Generate = false;
                foreach (var x in asset.clips)
                {
                    ResetEvent(x);
                }
            }
        }
    
        void ResetEvent(AnimClipEvents data)
        {
            int eventCount = data.events.Length;
            AnimationEvent[] eventList = new AnimationEvent[eventCount];
    
            AnimationClip clip = data.clip;
    
            for(int i = 0;i < eventCount; i ++)
            {
                AnimationEvent e = new AnimationEvent();
                e.functionName = data.events[i].funcName;
                e.time = data.events[i].frame / clip.frameRate;
                e.intParameter = data.events[i].intParam;
    
                Debug.Log(e.functionName + " " + e.time + " " + e.intParameter);
    
                eventList[i] = e;
            }
    
            Debug.Log(clip.name);
    
    
            AnimationUtility.SetAnimationEvents(clip, eventList);
    
            //EditorUtility.SetDirty(clip);
        }
    
    
    }
    
    using UnityEngine;
    
    
    [CreateAssetMenu(menuName = "AnimHelper/AnimEventGeneratorAsset")]
    public class AnimEventGeneratorAsset : ScriptableObject
    {
    
        [SerializeField]
        public AnimClipEvents[] clips;
    
    }
    
    
    using System;
    using UnityEngine;
    
    [CreateAssetMenu(menuName = "AnimHelper/AnimClipEvents")]
    public class AnimClipEvents : ScriptableObject
    {
        public AnimationClip clip;
        public int frameCount;
        public AnimEvent[] events;
    }
    
    [Serializable]
    public class AnimEvent
    {
        public string funcName;
        public int frame;
        public int intParam;
    }
    

    相关文章

      网友评论

          本文标题:2019-07-11 【UnityTips】代码添加动画事件

          本文链接:https://www.haomeiwen.com/subject/ztyrkctx.html