Unity—NGUI—鼠标悬停播放 Sprite Animation

来源:互联网 发布:网络玄幻小说合集目录 编辑:程序博客网 时间:2024/05/18 01:53

NGUI插件中的源码非常强大,只要你看过并且理解了它的源码,你就可以做出你想要的UI界面和效果,这篇文章就是我根据NGUI中UIButton组件改编的,当然喜欢的朋友可以用它来改成键盘控制播放动画(2D 的角色动画),希望能对新学NGUI的朋友有所帮助。

先用NGUI 添加一个sprite,然后把下面的代码拖拽到 sprite 身上就可以看到结果了。

     Framerate 是帧速率,可以自由调整到你满意为止。

下面贴上源码:

using UnityEngine;using System.Collections;/// <summary>/// 创建精灵的时候记得要给它添加上一个 BoxCollider/// </summary>public class testte : MonoBehaviour {// 这两个图集 也可以是一个,手动拖拽进来任意两个图集 两图集的 sprite 要尽量一致public UIAtlas atlas;public UIAtlas atlasNormal;// 需要添加的精灵动画组件,你可以在 Componen 中的 NGUI 的下级 UI里面找到它UISpriteAnimation animation;public UISprite sprite;// Use this for initializationvoid Start () {sprite = GetComponent<UISprite>();// 如果是按钮,可以在 他的孩子中找到 UISprite 或者你还可以直接拖拽你想要改变的任一个 UISprite 到 sprite(你要保持它为 public)//if(sprite == null)//sprite = GetComponentInChildren<UISprite>();animation = GetComponent<UISpriteAnimation>();animation.enabled = false;string name = sprite.atlas.spriteList[2].name;sprite.spriteName = name;// 使精灵 sprite 规模化 也可以自己调整它的 localScale 使符合要播放的图集动画大小sprite.MakePixelPerfect();//sprite.transform.localScale = new Vector3(108f, 107f, 0);}// Update is called once per framevoid Update () {}void OnHover(bool isOver){if(isEnabled || sprite != null){//base.OnHover(isOver);//sprite.spriteName = isOver ? hoverSprite : normalSprite;if(isOver){// 当鼠标悬停的时候,改变 sprite 的精灵图集sprite.atlas = atlas;// 名字可以手动写,或者像下面一样自动获取sprite.spriteName = "223124814_04";animation.enabled = true;sprite.MakePixelPerfect();}else{animation.enabled = false;sprite.atlas = atlasNormal;sprite.spriteName = sprite.atlas.spriteList[2].name;sprite.MakePixelPerfect();//sprite.transform.localScale = new Vector3(108f, 107f, 0);}}}public bool isEnabled{get{Collider col = collider;return col && col.enabled;}set{Collider col = collider;if(!col)return;if(col.enabled != value){col.enabled = value;//UpdateColor(value, false);}}}}

发现了个问题,上面那个事先给背景做好 SpriteAnimation 之后,当鼠标第二次停留在它上面的时候,它不会从第一张图片开始播放,所以改了一下,当鼠标每次停留在 sprite 上的时候,动态的给 sprite 添加 SpriteAnimation 类,当鼠标走开时,自动销毁这个类。这样就可以从图集的第一帧开始播放了

using UnityEngine;using System.Collections;public class MySpriteAnimation : MonoBehaviour{// 这两个图集 也可以是一个,手动拖拽进来任意两个图集 两图集的 sprite 要尽量一致//public UIAtlas atlas;//public UIAtlas atlasNormal;// 图片第一帧所在的位置public int spriteCount;public string theNamePrefix;// 需要添加的精灵动画组件,你可以在 Componen 中的 NGUI 的下级 UI里面找到它//UISpriteAnimation animation;public UISprite sprite;// Use this for initializationvoid Start () {//sprite = GetComponent<UISprite>();// 如果是按钮,可以在 他的孩子中找到 UISprite 或者你还可以直接拖拽你想要改变的任一个 UISprite 到 sprite(你要保持它为 public)if(sprite == null)sprite = GetComponentInChildren<UISprite>();//animation = GetComponentInChildren<UISpriteAnimation>();string name = sprite.atlas.spriteList[spriteCount].name;sprite.spriteName = name;// 使精灵 sprite 规模化 也可以自己调整它的 localScale 使符合要播放的图集动画大小//sprite.MakePixelPerfect();//sprite.transform.localScale = new Vector3(108f, 107f, 0);}// Update is called once per framevoid Update () {}void OnHover(bool isOver){if(isEnabled || sprite != null){//base.OnHover(isOver);//sprite.spriteName = isOver ? hoverSprite : normalSprite;if(isOver){UISpriteAnimation animation = sprite.gameObject.AddComponent<UISpriteAnimation>();animation.framesPerSecond = 25;// 当鼠标悬停的时候,改变 sprite 的精灵图集//sprite.atlas = atlasNormal;// 名字可以手动写,或者像下面一样自动获取sprite.spriteName = sprite.atlas.spriteList[spriteCount].name;animation.namePrefix = theNamePrefix;animation.enabled = true;//sprite.MakePixelPerfect();}else{Destroy(sprite.transform.GetComponent<UISpriteAnimation>());//animation.enabled = false;//sprite.atlas = atlasNormal;sprite.spriteName = sprite.atlas.spriteList[spriteCount].name;//sprite.MakePixelPerfect();//sprite.transform.localScale = new Vector3(108f, 107f, 0);}}}public bool isEnabled{get{Collider col = collider;return col && col.enabled;}set{Collider col = collider;if(!col)return;if(col.enabled != value){col.enabled = value;//UpdateColor(value, false);}}}}

0 0
原创粉丝点击