123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194 |
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine.UI;
- using System;
- [RequireComponent(typeof(Image))]
- public class UGUISpriteAnimation : MonoBehaviour
- {
- private Image ImageSource;
- private int mCurFrame = 0;
- private float mDelta = 0;
- public float FPS = 16;
- //public List<Sprite> SpriteFrames;
- public bool IsPlaying = false;
- public bool Foward = true;
- public bool AutoPlay = true;
- public bool Loop = false;
- public Action OnAnimationEndEvent;
- private string _containerName;
- public int FrameCount
- {
- get
- {
- return SpriteFrames.Count;
- }
- }
- /// <summary>
- /// 资源
- /// </summary>
- private Dictionary<string, Sprite> SpriteFrames = new Dictionary<string, Sprite>();
- /// <summary>
- ///
- /// </summary>
- /// <param name="frameContainer"></param>
- /// <param name="frameCount"></param>
- public void Init(string frameContainer, Action onInitFinished)
- {
- _containerName = frameContainer;
- ImageSource = GetComponent<Image>();
- ResourceHelper.Instance.LoadAssetBundle(frameContainer, (AssetBundle bundle) =>
- {
- if (null != bundle)
- {
- GameObject go = (GameObject)bundle.LoadAsset(frameContainer);
- List<Sprite> tempList = go.GetComponent<IconContainer>().mSpriteList;
- for (int i = 0; i < tempList.Count; i++)
- {
- if (null != tempList[i])
- {
- SpriteFrames[tempList[i].name] = tempList[i];
- }
- }
- //SpriteFrames = go.GetComponent<IconContainer>().mSpriteList;
- }
- if (onInitFinished != null)
- {
- onInitFinished();
- }
- });
- }
- public void StartPlayAnimation()
- {
- if (AutoPlay)
- {
- Play();
- }
- else
- {
- IsPlaying = false;
- }
- }
- private void SetSprite(int idx)
- {
- string resname = "frame (" + (idx + 1) + ")";
- ImageSource.sprite = SpriteFrames[resname];
- ImageSource.SetNativeSize();
-
- //IconManager.Instance.GetSprite(_containerName, resname, texture =>
- //{
- // ImageSource.overrideSprite = texture;
- // ImageSource.SetNativeSize();
- //});
- }
- public void Play()
- {
- IsPlaying = true;
- Foward = true;
- }
- public void PlayReverse()
- {
- IsPlaying = true;
- Foward = false;
- }
- void Update()
- {
- if (!IsPlaying || 0 == FrameCount)
- {
- return;
- }
- mDelta += Time.deltaTime;
- if (mDelta > 1 / FPS)
- {
- mDelta = 0;
- if (Foward)
- {
- mCurFrame++;
- }
- else
- {
- mCurFrame--;
- }
- if (mCurFrame >= FrameCount)
- {
- InvokeEndEvent();
- if (Loop)
- {
- mCurFrame = 0;
- }
- else
- {
- IsPlaying = false;
- return;
- }
- }
- else if (mCurFrame < 0)
- {
- InvokeEndEvent();
- if (Loop)
- {
- mCurFrame = FrameCount - 1;
- }
- else
- {
- IsPlaying = false;
- return;
- }
- }
- SetSprite(mCurFrame);
- }
- }
- private void InvokeEndEvent()
- {
- if (OnAnimationEndEvent != null)
- {
- OnAnimationEndEvent();
- }
- }
- public void Pause()
- {
- IsPlaying = false;
- }
- public void Resume()
- {
- if (!IsPlaying)
- {
- IsPlaying = true;
- }
- }
- public void Stop()
- {
- mCurFrame = 0;
- SetSprite(mCurFrame);
- IsPlaying = false;
- }
- public void Rewind()
- {
- mCurFrame = 0;
- SetSprite(mCurFrame);
- Play();
- }
- }
|