当前位置:网站首页>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 :
-
AnimationClip There is one SetCurve Method can set the curve ,AnimationCurve Parameter is float type ,
The animation clip records Sprite attribute , There is no curve , Need to create binding; -
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;
-
For animation clip editing, you need AnimationUtility class , use SetObjectReferenceCurve Set animation curve or ObjectReferenceKeyframe Array ;
-
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
边栏推荐
- [talkative cloud native] load balancing - the passenger flow of small restaurants has increased
- Devexpress 14.1 installation record
- The flinkcdc reports an error: but this is no longer available on the server
- 内网渗透之DOS命令
- Vscode download speed up
- GO语言开发天天生鲜项目第三天 案例-新闻发布系统二
- [latex] 5 how to quickly write out the latex formula corresponding to the formula
- 缓存淘汰算法初步认识(LRU和LFU)
- LeetCode 1346、检查整数及其两倍数是否存在
- Analysis of the relationship between generalized Bim and CAD under the current background
猜你喜欢
Latest investigation and progress of building intelligence based on sati
缓存淘汰算法初步认识(LRU和LFU)
Livego + ffmpeg + RTMP + flvjs to realize live video
Scripy tutorial - (2) write a simple crawler
Browser - learning notes
RT-1052学习笔记 - GPIO架构分析
Modeling based on catiav6
LeetCode 994、腐烂的橘子
Flex layout
DOS command of Intranet penetration
随机推荐
Devexpress 14.1 installation record
DNS cloud school | analysis of hidden tunnel attacks in the hidden corner of DNS
ArcGIS js api 4. X submergence analysis and water submergence analysis
Some basic configurations in interlij idea
Analysis of the relationship between generalized Bim and CAD under the current background
Three. Based on ply format point cloud voxel model JS upload interface writing
JDBC tool class jdbcconutil gets the connection to the database
Livego + ffmpeg + RTMP + flvjs to realize live video
Markdown < a > tag new page open link
UnhandledPromiseRejectionwarning:CastError: Cast to ObjectId failed for value
三十一. `prototype`显示原型属性和`__proto__`隐式原型属性
[talkative cloud native] load balancing - the passenger flow of small restaurants has increased
16MySQL之DCL 中 COMMIT和ROllBACK
Numpy - creation of data type and array
Resolve the eslint warning -- ignore the warning that there is no space between the method name and ()
高薪程序员&面试题精讲系列91之Limit 20000加载很慢怎么解决?如何定位慢SQL?
Elastic box model
2022dasctf APR x fat epidemic prevention challenge crypto easy_ real
What is the difference between a host and a server?
Building the tide, building the foundation and winning the future -- the successful holding of zdns Partner Conference