在UISpriteAnimationInspector.cs中插入
把UISpriteAnimation.cs改成
- UISpriteAnimation.AnimationMode animMode = (UISpriteAnimation.AnimationMode)EditorGUILayout.EnumPopup("AnimationMode",anim.animationMode);
- if(anim.animationMode != animMode)
- {
- NGUIEditorTools.RegisterUndo("Sprite Animation Change", anim);
- anim.animationMode = animMode;
- EditorUtility.SetDirty(anim);
- }
把UISpriteAnimation.cs改成
- //----------------------------------------------
- // NGUI: Next-Gen UI kit
- // Copyright ? 2011-2012 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.
- /// </summary>
- [ExecuteInEditMode]
- [RequireComponent(typeof(UISprite))]
- [AddComponentMenu("NGUI/UI/Sprite Animation")]
- public class UISpriteAnimation : MonoBehaviour
- {
- public enum AnimationMode
- {
- Forword = 0,
- Reverse = 1,
- PingPong = 2
- }
- [HideInInspector][SerializeField] int mFPS = 30;
- [HideInInspector][SerializeField] string mPrefix = "";
- [HideInInspector][SerializeField] bool mLoop = true;
- [HideInInspector][SerializeField] AnimationMode animMode = AnimationMode.Forword;
- UISprite mSprite;
- float mDelta = 0f;
- int mIndex = 0;
- int mFrames = 0;
- int nDelta = 1;
- bool mActive = true;
- List<string> mSpriteNames = new List<string>();
- public AnimationMode animationMode
- {
- get
- {
- return animMode;
- }
- set
- {
- animMode = value;
- }
- }
- /// <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; } }
- /// <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();
- if(animMode == AnimationMode.Reverse)
- {
- SetFrame(mFrames-1);
- nDelta = -nDelta;
- }
- else
- {
- SetFrame(0);
- }
- }
- /// <summary>
- /// Advance the sprite animation process.
- /// </summary>
- void Update ()
- {
- if(mActive && mFrames > 1 && Application.isPlaying && mFPS > 0f)
- {
- mDelta += Time.deltaTime;
- float rate = 1f/mFPS;
- if(mDelta > rate)
- {
- mDelta = (rate > 0f) ? mDelta - rate : 0f;
- if(mIndex + nDelta >= mFrames)
- {
- if(mLoop)
- {
- switch(animMode)
- {
- case AnimationMode.PingPong:
- nDelta = -nDelta;
- break;
- }
- }
- else
- {
- mActive = false;
- }
- }
- else if(mIndex + nDelta < 0)
- {
- if(mLoop)
- {
- switch(animMode)
- {
- case AnimationMode.PingPong:
- nDelta = -nDelta;
- break;
- }
- }
- else
- {
- mActive = false;
- }
- }
- SetFrame(mIndex + nDelta);
- }
- }
- }
- public void SetFrame(int _frame)
- {
- if (mActive)
- {
- int m = _frame%mFrames;
- if(m < 0) m += mFrames;
- mIndex = m;
- mSprite.spriteName = mSpriteNames[mIndex];
- //mSprite.MakePixelPerfect();
- }
- }
- /// <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();
- }
- mFrames = mSpriteNames.Count;
- }
- /// <summary>
- /// Reset the animation to frame 0 and activate it.
- /// </summary>
- public void Reset()
- {
- mActive = true;
- mIndex = 0;
- }
- }