UGUI 帧动画插件
最近在开发一款功夫猫游戏,本来使用Unity Sprite制作,但是发现Sprite对各种分辨率不支持. 看着游戏很简单就使用UGUI制作,在中途发现有很多帧动画播放,使用了Animation调整使用多了的确很不方便.
于是改成脚本来控制Sprite帧动画切换,慢慢开始形成了写一个插件来调整. 写了两个通宵终于搞定了. O(∩_∩)O~
效果图:
代码:
组件类:
using UnityEngine; using System.Collections; using System.Collections.Generic; using UnityEngine.UI; using System;/// <summary> /// 帧动画组件 /// </summary> [System.Serializable] public class ImageAnimation : MonoBehaviour {private float animationDeltaTime;public float animationDeltaTimer;public List<AnimationInfoEntity> animationInfo;public int type;public Image visualize;public int index;public string animationTypeList;public string tempAnimationTypeList;public string[] animationTypeProp;public void Awake(){visualize = this.transform.GetComponent<Image>();}public void Update(){animationDeltaTime += Time.deltaTime;#region List的用法if (animationInfo != null && animationInfo.Count > 0 && animationDeltaTime > animationInfo[type].deltaTime){if (animationInfo[type].animationSprite != null && animationInfo[type].animationSprite.Count != 0){index++;index = index % animationInfo[type].animationSprite.Count;visualize.sprite = animationInfo[type].animationSprite[index];animationDeltaTime = 0;visualize.SetNativeSize();}}#endregion}/// <summary>/// 切换动画状态/// </summary>/// <param name="index">输入动画状态下标值</param>public void ChangeAnimationState(int index){if (animationTypeProp != null) {if (index < animationTypeProp.Length) {type = index;}}}/// <summary>/// 切换动画状态/// </summary>/// <param name="animationStateName">输入动画状态的名称</param>public void ChangeAnimationState(string animationStateName){if (animationTypeProp != null){for (int i = 0; i < animationTypeProp.Length; i++){if (animationTypeProp[i].Equals(animationStateName)) {type = i;return;}}}}}[System.Serializable] public class AnimationInfoEntity {/// <summary>/// 动画状态/// </summary>public int type; /// <summary>/// 播放当前帧需要的时间/// </summary>public float deltaTime; /// <summary>/// 动画状态所需要的图片集合/// </summary>public List<Sprite> animationSprite;public AnimationInfoEntity() { }public AnimationInfoEntity(int type, float deltaTime, int spriteNum = 1){this.type = type;this.deltaTime = deltaTime;animationSprite = new List<Sprite>();} }
编辑器类:
using UnityEngine; using System.Collections; using UnityEditor; using System.Collections.Generic; using System; using System.Reflection; using System.Reflection.Emit;[CustomEditor(typeof(ImageAnimation))] public class AnimationEditor : Editor {public void OnEnable() {ImageAnimation model = target as ImageAnimation;if (model.tempAnimationTypeList == null) {model.tempAnimationTypeList = string.Empty;}if (model.animationInfo == null){model.animationInfo = new List<AnimationInfoEntity>();}}public override void OnInspectorGUI(){ImageAnimation model = target as ImageAnimation;if (!string.IsNullOrEmpty(model.animationTypeList)) {model.animationTypeProp = model.animationTypeList.Split (';');}#region 动画分割GUILayout.BeginHorizontal();GUILayout.Label("所有图片每帧时间: ", new GUILayoutOption[] { GUILayout.Width(120) });model.animationDeltaTimer = EditorGUILayout.FloatField(model.animationDeltaTimer);if (GUILayout.Button("统一时间")) {for (int j = 0; j < model.animationInfo.Count; j++){model.animationInfo[j].deltaTime = model.animationDeltaTimer;}}GUILayout.EndHorizontal();GUILayout.BeginHorizontal ();GUILayout.Label("动画类型分隔符: ",new GUILayoutOption[]{ GUILayout.Width(120)});model.tempAnimationTypeList = GUILayout.TextField(model.tempAnimationTypeList, 50);if (GUILayout.Button ("重新定义动画类型")) {model.animationInfo = new List<AnimationInfoEntity>();model.animationTypeList = model.tempAnimationTypeList;model.animationTypeProp = model.animationTypeList.Split (';');//初始化动画类型集合for (int j = 0; j < model.animationTypeProp.Length; j++){model.animationInfo.Add(new AnimationInfoEntity(j, model.animationDeltaTimer));}}GUILayout.EndHorizontal ();#endregion#region 绘制各个动画属性if (model.animationTypeProp != null && !string.IsNullOrEmpty(model.animationTypeProp[0])){for (int i = 0; i < model.animationTypeProp.Length; i++) {//draw animation typea GUILayout.BeginHorizontal();GUILayout.Label("动画类型: ", new GUILayoutOption[] { GUILayout.Width(60) });int index = EditorGUILayout.Popup(i, model.animationTypeProp, new GUILayoutOption[] { GUILayout.Width(150) });if (GUILayout.Button("+")){model.animationInfo[i].animationSprite.Add(new Sprite());}if (GUILayout.Button("-")){if (model.animationInfo[i].animationSprite.Count > 0){model.animationInfo[i].animationSprite.RemoveAt(model.animationInfo[i].animationSprite.Count - 1);}}GUILayout.EndHorizontal();//draw image list GUILayout.BeginVertical();if (model.animationInfo != null && model.animationInfo.Count > 0){for (int k = 0; k < model.animationInfo[i].animationSprite.Count; k++){GUILayout.BeginHorizontal();GUILayout.Label("动画帧数: ", new GUILayoutOption[] { GUILayout.Width(60) });EditorGUILayout.FloatField(model.animationInfo[i].deltaTime, new GUILayoutOption[] { GUILayout.Width(60) });model.animationInfo[i].animationSprite[k] = EditorGUILayout.ObjectField("增加一个贴图", model.animationInfo[i].animationSprite[k], typeof(Sprite)) as Sprite;GUILayout.EndHorizontal();}}GUILayout.EndVertical();}}#endregionserializedObject.ApplyModifiedProperties();DrawAnimationButton();}/// <summary>/// 绘制动画切换按钮,方便用户切换动画,查看动画是否正确/// </summary>private void DrawAnimationButton() {ImageAnimation model = target as ImageAnimation;if (model.animationTypeProp != null) {GUILayout.BeginHorizontal();GUILayout.Label("切换动画状态: ",GUILayout.Width(80));for (int i = 0; i < model.animationTypeProp.Length; i++){if (GUILayout.Button(model.animationTypeProp[i],GUILayout.Width(50))){model.ChangeAnimationState(i);}}GUILayout.EndHorizontal();}} }
下载地址: http://yunpan.cn/cFRfdgXhK6ff2 访问密码 3aed
转载于:https://www.cnblogs.com/plateFace/p/4899728.html
UGUI 帧动画插件相关推荐
- 前端逐帧动画性能探究和比较
什么是逐帧动画? 首先看一下维基百科中的定义: 定格动画,又名逐帧动画,是一种动画技术,其原理即将每帧不同的图像连续播放,从而产生动画效果. 简单的来说就是: 以一定的速度切换几张连续图像,让它动起来 ...
- android动画不占cpu如何实现,【实战总结】帧动画调优实践
原标题:[实战总结]帧动画调优实践 原文链接:https://www.zybuluo.com/avenwu/note/876161 APP架构师整理发布,转载请联系作者获得授权. 1.背景 在做动画的 ...
- html逐帧动画,谈谈网页中实现逐帧动画
前言 我们在网页中经常需要一些动画效果,假如你的动画效果需要人为控制停止或者播放,我们一般就不会用gif来实现了!这里,就引出了我们今天讲到的逐帧动画!今天主要讲解几种方式来实现逐帧动画!大家可以根据 ...
- web loding 自定义加载动画插件
官网 指南 介绍 Web 中实现 loading 的方式有很多种,例如使用css动画.js操作元素.gif图片.svg动画.ui框架中自带loading等等,各有所优,操作元素可能更方便,但会影响性能 ...
- 鼠标滚轮仿星球大战文字帧动画的实现
最近研究了一下利用鼠标滚轮控制文字帧动画的效果,在此记录一下实现过程. 不多说,先看一下效果图: 滚动鼠标滚轮之后文字会渐渐显示再渐渐消失,当菜单下的progressbar满了的时候,就会触发换页动画 ...
- 探讨3DSMAX 中的CS骨骼动画插件
3D 图形引擎中角色动画是一个重要的组成部分,它在虚拟现实.电子游戏,甚至是传统的动画制作中均扮演着极其重要的角色.如何实现一个良好的角色动画以引起越来越多的研究者的关注.目前的许多建模软件都可以快速 ...
- CSS和HTML帧动画
1.定义关键帧 @keyframes 动画名称(英文) { 0% { /* 动画的开始 */ } 25% { ...
- Android动画之帧动画和补间动画
Android系统提供三种动画:帧动画.补间动画和属性动画.这里先分析总结帧动画和补间动画. FrameAnimation 帧动画,通俗来说就是按照图片动作顺序依次播放来形成动画,创建帧动画可以用 x ...
- Android 逐帧动画(Frame)
Android 逐帧动画(Frame) 很好理解就是将多张图片放到一个容器里面通过控制这些图片一帧一张图片从而形成动画 使用的使用通过AnimationDrawable 加载放好的图片 然后通过调用 ...
最新文章
- 人脸检测--Recurrent Scale Approximation for Object Detection in CNN
- ***微信 该连接无法访问问题解决办法
- Android开发究竟该如何学习,含泪整理面经
- SpringBoot2 整合 AXIS 服务端和客户端
- 【Win 10应用开发】提供建议列表的输入控件(AutoSuggestBox)
- 非计算机专业计算机教学考试,论非计算机专业的计算机教学与等级考试
- 【GitHub通过ssh方法下载详细配置过程】
- Laravel源码解析【转】
- Spring Boot 定制个性 banner
- 计算机关机慢怎么解决方法,电脑关机很慢,详细教您win7电脑关机很慢的解决方法...
- 入手评测 联想小新PRO14,华硕灵耀14s和联想YOGA14s选哪个
- 华为OD机试 - 按身高和体重排队(Java JS Python)
- tushare更新,get_k_data支持分时k线数据,可替代以前的get_hist_data
- 【排序】详细聊聊归并排序(含非递归)
- php redis 挂掉,redis中的宕机什么意思
- 区块链学习名词详解-Part1
- 2023王道数据结构考研习题汇总
- python 学习笔记1
- 微营销好标题之喜闻乐见(微营销标题吸引粉丝篇-6)
- Unity 3D俄罗斯方块
热门文章
- Adobe Dreamweaver CS3中文版
- FCKeditor 2.6 精简版
- jQuery1.6以上attr改用prop
- 结对-贪吃蛇-项目进度
- Sql查询一个列对应多个列
- 另类的 高版本数据库 转换到 低版本数据库
- wchar_t*,wchar_t,wchat_t数组,char,char*,char数组,std::string,std::wstring,CString 以及system(command)...
- Direct2D (25) : 将画笔线条所占用的区域转换为路径 - ID2D1Geometry.Widen()
- Java反射机制大神必学系列之 ,高级与低级的差别在哪里?
- rancher的微服务运维