当前位置:网站首页>Unity animation creates sequence frame code and generates animationclip

Unity animation creates sequence frame code and generates animationclip

2022-04-23 20:34:00 One Mr rabbit one

use Odin Create a window , Drag in Sprite Generate AnimationClip

A few pits :

  1. AnimationClip There is one SetCurve Method can set the curve ,AnimationCurve Parameter is float type , Insert picture description here
    The animation clip records Sprite attribute , There is no curve , Need to create binding;

  2. establish EditorCurveBinding Type of binding You can set value yes Object Type value :

    • type: Components on animated objects ;
    • propertyName: Parameters recorded on component , For example Image.Sprite; Get private members "m_Sprite";
    • Get public member "sprite" Meeting Missing;  Insert picture description here
  3. For animation clip editing, you need AnimationUtility class , use SetObjectReferenceCurve Set animation curve or ObjectReferenceKeyframe Array ;

  4. Animation clip settings AnimationClipSettings class ,AnimationUtility.GetAnimationClipSettings obtain ,AnimationUtility.SetObjectReferenceCurve Set up ;

Complete code :

#if UNITY_EDITOR
using System;
using System.Collections.Generic;
using System.IO;
using DG.DemiEditor;
using UnityEditor;
using UnityEngine;
using Sirenix.OdinInspector.Editor;
using Sirenix.OdinInspector;
using Sirenix.Utilities.Editor;
using Sirenix.Utilities;
using UnityEngine.UI;

namespace Editor.Tools
{
    
    public class TextureTool : OdinEditorWindow
    {
    
        [ReadOnly] [LabelText(" role ")] public string avatarName = "";
        [LabelText(" A picture every few frames ")] public int frame = 5;
        [LabelText(" The name of the animation ")] public string animName = "New anim";
        
        //todo  Frame rate setting 
        // private float _frameRate = Common.FrameRate;
        private float _frameRate = 30;
        [MenuItem("Tools/Sprite2Anim")]
        private static void OpenWindow()
        {
    
            var window = GetWindow<TextureTool>();
            window.position = GUIHelper.GetEditorWindowRect().AlignCenter(600, 600);
            window.titleContent = new GUIContent("Create Sprite Anim");
        }
        [InlineEditor(InlineEditorModes.LargePreview)]
        [ListDrawerSettings(OnTitleBarGUI = "DrawRefreshButton")]
        [OnValueChanged("SetDefaultName")]
        public List<Sprite> sprites;

        public void SetDefaultName(List<Sprite> sprites)
        {
    
            if (sprites.Count == 0)
            {
    
                avatarName = "";
                animName = "";
                return;
            }
            string[] tmpStrs = sprites[0].name.Split(new[] {
    "_"}, StringSplitOptions.RemoveEmptyEntries);
            if (tmpStrs.Length <= 1) animName = tmpStrs[0];
            else
            {
    
                avatarName = tmpStrs[0];
                animName = tmpStrs[0] + "_" + tmpStrs[1];
            }
        }
        public void DrawRefreshButton()
        {
    
            if(SirenixEditorGUI.ToolbarButton(EditorIcons.Refresh))
                sprites.Clear();
        }
        
        [HorizontalGroup("Button")][Button(ButtonSizes.Gigantic, Name = " Create animation "), GUIColor(0, 1, 0)]
        public void CreateAnimationClip()
        {
    
            if (sprites.Count == 0)
            {
    
                Debug.LogError(" No map selected ");
                return;
            }

            string assetLocation = AssetUtilities.GetAssetLocation(sprites[0]);

            AnimationClip clip = new AnimationClip();
            EditorCurveBinding curveBinding = new EditorCurveBinding();
            curveBinding.type = typeof(Image);
            curveBinding.path = "";
            curveBinding.propertyName = "m_Sprite";
            ObjectReferenceKeyframe[] keyframes = new ObjectReferenceKeyframe[sprites.Count];
            for (int i = 0; i < sprites.Count; i++)
            {
    
                keyframes[i] = new ObjectReferenceKeyframe();
                keyframes[i].time = i * frame / _frameRate;
                keyframes[i].value = sprites[i];
            }
            
            clip.frameRate = _frameRate;
            AnimationClipSettings clipSettings = AnimationUtility.GetAnimationClipSettings(clip);
            clipSettings.loopTime = true;
            AnimationUtility.SetAnimationClipSettings(clip, clipSettings);

            AnimationUtility.SetObjectReferenceCurve(clip, curveBinding, keyframes);
            
            if (!avatarName.IsNullOrEmpty())
            {
    
                assetLocation += "/" + avatarName + "_anim";
                Directory.CreateDirectory(assetLocation);
            }
            
            AssetDatabase.CreateAsset(clip, assetLocation + "/" + animName + ".anim");
            AssetDatabase.SaveAssets();
        }
    }
}
#endif

版权声明
本文为[One Mr rabbit one]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204210548406417.html