123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172 |
- //----------------------------------------------
- // NGUI: Next-Gen UI kit
- // Copyright © 2011-2013 Tasharen Entertainment
- //----------------------------------------------
- using UnityEngine;
- using System.Collections.Generic;
- /// <summary>
- /// Very simple sprite animation. Attach to a sprite and specify a bunch of sprite names and it will cycle through them.
- /// NA: This is an updated version of UISpriteAnimation from NGUI that supports ping-pong animation
- /// </summary>
- [ExecuteInEditMode]
- [RequireComponent(typeof(UISprite))]
- [AddComponentMenu("NGUI/UI/Sprite Animation")]
- public class UISpriteAnimationWithPingPong : MonoBehaviour
- {
- [HideInInspector][SerializeField] int mFPS = 30;
- [HideInInspector][SerializeField] string mPrefix = "";
- [HideInInspector][SerializeField] bool mLoop = true;
- [HideInInspector][SerializeField] bool mPingPong = true;
- UISprite mSprite;
- float mDelta = 0f;
- int mIndex = 0;
- bool mActive = true;
- List<string> mSpriteNames = new List<string>();
- bool isInReverse;
- /// <summary>
- /// Number of frames in the animation.
- /// </summary>
- public int frames { get { return mSpriteNames.Count; } }
- /// <summary>
- /// Animation framerate.
- /// </summary>
- public int framesPerSecond { get { return mFPS; } set { mFPS = value; } }
- /// <summary>
- /// Set the name prefix used to filter sprites from the atlas.
- /// </summary>
- public string namePrefix { get { return mPrefix; } set { if (mPrefix != value) { mPrefix = value; RebuildSpriteList(); } } }
- /// <summary>
- /// Set the animation to be looping or not
- /// </summary>
- public bool loop { get { return mLoop; } set { mLoop = value; } }
- public bool pingPong { get { return mPingPong; } set { mPingPong = value; } }
- /// <summary>
- /// Returns is the animation is still playing or not
- /// </summary>
- public bool isPlaying { get { return mActive; } }
- /// <summary>
- /// Rebuild the sprite list first thing.
- /// </summary>
- void Start () { RebuildSpriteList(); }
- /// <summary>
- /// Advance the sprite animation process.
- /// </summary>
- void Update ()
- {
- if (mActive && mSpriteNames.Count > 1 && Application.isPlaying && mFPS > 0f)
- {
- mDelta += Time.deltaTime;
- float rate = 1f / mFPS;
- if (rate < mDelta)
- {
-
- mDelta = (rate > 0f) ? mDelta - rate : 0f;
- if (isInReverse)
- {
- if (--mIndex < 0)
- {
- mIndex = 0;
- isInReverse = false;
- mActive = loop;
- }
- } else
- {
- if (++mIndex >= mSpriteNames.Count)
- {
- if (mPingPong)
- {
- mIndex = mSpriteNames.Count - 1;
- isInReverse = true;
- } else
- {
- mIndex = 0;
- }
- mActive = loop;
- }
- }
- if (mActive)
- {
- mSprite.spriteName = mSpriteNames[mIndex];
- mSprite.MakePixelPerfect();
- //Nev: Since MakePixelPerfect is important for getting the right aspect ratios
- //(sprites in the animation might have different dimensions). We have to multiply
- //according to the current resolution, otherwise in an SD they would be small
- //and in SHD they would be big
- //Remember that we are designing the game in HD resolution.
- if (ResolutionSwitchController.IsSD)
- {
- transform.localScale *= 2;
- }
- if (ResolutionSwitchController.IsSHD)
- {
- transform.localScale /= 2;
- }
- }
- }
- }
- }
- /// <summary>
- /// Rebuild the sprite list after changing the sprite name.
- /// </summary>
- void RebuildSpriteList ()
- {
- if (mSprite == null) mSprite = GetComponent<UISprite>();
- mSpriteNames.Clear();
- if (mSprite != null && mSprite.atlas != null)
- {
- List<UIAtlas.Sprite> sprites = mSprite.atlas.spriteList;
- for (int i = 0, imax = sprites.Count; i < imax; ++i)
- {
- UIAtlas.Sprite sprite = sprites[i];
- if (string.IsNullOrEmpty(mPrefix) || sprite.name.StartsWith(mPrefix))
- {
- mSpriteNames.Add(sprite.name);
- }
- }
- mSpriteNames.Sort();
- }
- }
-
- /// <summary>
- /// Reset the animation to frame 0 and activate it.
- /// </summary>
-
- public void Reset()
- {
- mActive = true;
- mIndex = 0;
- isInReverse = false;
- if (mSprite != null && mSpriteNames.Count > 0)
- {
- mSprite.spriteName = mSpriteNames[mIndex];
- mSprite.MakePixelPerfect();
- }
- }
- }
|