最近在开发一款功夫猫游戏,本来使用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. html5 app图片预加载

    function Laimgload(){} //图片预加载JS Laimgload.prototype.winHeight = function(){ //计算页面高度 var winHeight ...

  2. css中的列表样式

    在网页设计中,我们经常将某些具有相似功能的标签放在同一组中,这时我们经常会用到列表标签(无序列表ul,有序列表ol),在列表标签中对列表样式的设计可以使我们的页面得到一定程度的美化. 在css中对列表 ...

  3. AngularJs学习笔记2——四大特性之MVC

    angularJs的四大特性 ①.采用MVC的设计模式 ②.双向数据绑定 ③.依赖注入 ④.模块化设计 现在细说一下MVC的设计模式: MVC: Model(模型)--项目中的数据 View(视图)- ...

  4. 升级IOS9,提示滑动升级,卡在password锁屏界面,无反应了

    注:升级之前一定要把锁屏password取消掉 若遇上述问题.可通过进入DFU 模式解决 进入DFU具体步骤.(进入成功后,屏幕为全黑) 注:在进入DFU操作时务必与电脑连接好数据线. 1.按住pow ...

  5. first day for new job

    第一天上班,做个总结. 总得来说,感觉非常不错,一个结论~保持头脑清醒,好好加油. 今天主要办一些入职手续,拿到了代码,后面几天主要就是熟悉应用的功能.源代码.想好好制定个计划,定日目标. 1.功能结 ...

  6. Android中Service类onStartCommand

    Android开发的过程中,每次调用startService(Intent)的时候,都会调用该Service对象的onStartCommand(Intent,int,int)方法,然后在onStart ...

  7. 先装Net Framework 后 装 IIS的处理办法

    先装IIS话,后面装Net Framework时候会自动注册 处理aspx和ashx等的处理扩展程序 先装Net Framework 后 装 IIS.扩展程序注册在命令:aspnet_regiis - ...

  8. Tomcat 默认应用

    在部署应用时需要更改默认的端口号及应用,以免让别人知道使用的服务器类型而进行攻击.tomca的部署有多种方式,这里简单谈一下.目前想到有三种方式:一.添加 Context在Tomcat的配置文件中,一 ...

  9. HTML解析利器 - HtmlAgilityPack

    HtmlAgilityPack 是CodePlex 上的一个开源项目.它提供了标准的DOM API 和XPath 导航--即使 HTML 不是适当的格式! 使用HtmlAgilityPack操作HTM ...

  10. CODEVS 1066/洛谷 P1514引水入城

    1066 引水入城 2010年NOIP全国联赛提高组  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond   题目描述 Description 在一个遥远的国 ...