最近在开发一款功夫猫游戏,本来使用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();
}
}
#endregion serializedObject.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

UGUI 帧动画插件的更多相关文章

  1. UGUI 过渡动画插件,模仿NGUI的Tween (转载)

    最近在相亲,后来好朋友跟我说他写了一个好插件,于是我就把女朋友甩了,看看他的插件,可以在UGUI制作简单过渡动画. 我看了下是模仿NGUI的Tween, 我在筱程的基础上稍微改到人性化, 简单支持的让 ...

  2. [UGUI]帧动画

    ImageFrameAnimation.cs using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; [R ...

  3. Unity CCTween UGUI 动画插件

    在这简单的介绍一下 CCTween 动画插件的使用 因为GIF 制作软件不太好(网上随便下载的)所以导致效果不太好,有时间我重新制作一下 这是一下简单的效果 下面介绍怎么使用 首先 先下载 CCTwe ...

  4. 让网站动起来!12款优秀的 jQuery 动画插件推荐

    如今,大多数设计师和开发人员被要客户要求开发动态的网站.创造视觉震撼和醒目的动态网站是艰巨的任务,因为它需要大量的努力和创造力.在网络上有大量的工具和插件可用于创建网站动画.许多开发人员正在使用 HT ...

  5. 程序猿必备的10款web前端动画插件一

    1.动画SVG框架幻灯片 在幻灯片之间切换时显示动画SVG帧的实验性幻灯片.不同的形状可以用来创建各种风格. 我们想和大家分享一个实验幻灯片.我们的想法是在从一个幻灯片转换到另一张幻灯片时,使SVG帧 ...

  6. Lottie在手,动画我有:ios/Android/Web三端复杂帧动画解决方案

      为什么需要Lottie 在相对复杂的移动端应用中,我们可能会需要使用到复杂的帧动画.例如: 刚进入APP时候可能会看到的入场小动画,带来愉悦的视觉享受 许多Icon的互动变化比较复杂多变的时候,研 ...

  7. 深入理解CSS3 Animation 帧动画

    CSS3我在5年之前就有用了,包括公司项目都一直在很前沿的技术. 最近在写慕课网的七夕主题,用了大量的CSS3动画,但是真的沉淀下来仔细的去深入CSS3动画的各个属性发现还是很深的,这里就写下关于帧动 ...

  8. Android动画效果之Frame Animation(逐帧动画)

    前言: 上一篇介绍了Android的Tween Animation(补间动画) Android动画效果之Tween Animation(补间动画),今天来总结下Android的另外一种动画Frame ...

  9. android 帧动画,补间动画,属性动画的简单总结

      帧动画——FrameAnimation 将一系列图片有序播放,形成动画的效果.其本质是一个Drawable,是一系列图片的集合,本身可以当做一个图片一样使用 在Drawable文件夹下,创建ani ...

随机推荐

  1. MySQL精粹

    关于Mysql整理的需要记忆和熟练掌握的内容 1.查询数据表的信息(比如有多少行数据): show table status like 'tab_User' -- 数据表中的数量   2. 使用 ex ...

  2. python使用一个集合代替列表

    """说明:对于一个指定的序列,如果需要获得一个只包含该序列中不重复的序列时,使用以下算法:"""seq=['a','a','b','c', ...

  3. [Oracle] 使用触发器实现IP限制用户登录

    在Oracle里,不像MySQL那样方便,可以直接在用户上进行IP限制,Oracle要实现用户级别的IP限制,可以使用触发器来迂回实现,下面是一个触发器的例子: create or replace t ...

  4. Java学习笔记——JDBC之与数据库MySQL的连接以及增删改查等操作

    必须的准备工作 一.MySQL的安装.可以参考博文: http://blog.csdn.net/jueblog/article/details/9499245 二.下载 jdbc 驱动.可以从在官网上 ...

  5. 多路复用I/O epoll()

    epoll 是Linux内核中的一种可扩展IO事件处理机制,最早在 Linux 2.5.44内核中引入,可被用于代替POSIX select 和 poll 系统调用,并且在具有大量应用程序请求时能够获 ...

  6. (转)[老老实实学WCF] 第四篇 初探通信--ChannelFactory

    第四篇 初探通信--ChannelFactory 通过前几篇的学习,我们简单了解了WCF的服务端-客户端模型,可以建立一个简单的WCF通信程序,并且可以把我们的服务寄宿在IIS中了.我们不禁感叹WCF ...

  7. get请求与post请求

    1. get是从服务器上获取数据,post是向服务器传送数据.2. get是把参数数据队列加到提交表单的ACTION属性所指的URL中,值和表单内各个字段一一对应,在URL中可以看到.post是通过H ...

  8. Geodatabase - 打开数据库(工作空间)

    //使用IName方式打开数据库(工作空间). public void GetWorkspace_IName(string workspacePath) { ESRI.ArcGIS.Geodataba ...

  9. sql 合并列

    1.合并一列用“ ,”号隔开. 如下图: 这样的一列我想直接在sql里面合并最后变成:586,444,444,444,444这样的效果,平常的做法是直接把这列数据取出来,在前端循环加上逗号,但其实是可 ...

  10. (二)CodeMirror - 配置项

    theme: string theme:'monokai' 引入对应的css, <link rel="stylesheet" href="../theme/mono ...