UIFrameAnimation.cs 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. [RequireComponent(typeof(UI2DSprite), typeof(UI2DSpriteAnimation))]
  5. public class UIFrameAnimation : MonoBehaviour
  6. {
  7. private void Awake()
  8. {
  9. this._animation = base.GetComponent<UI2DSpriteAnimation>();
  10. this._2DSprite = base.GetComponent<UI2DSprite>();
  11. }
  12. public void Preload(string name)
  13. {
  14. this._spritesesCache.Add(name, Resources.LoadAll<Sprite>("UI/Frames/" + name));
  15. }
  16. public void Play(string name)
  17. {
  18. Sprite[] array;
  19. if (this._spritesesCache.ContainsKey(name))
  20. {
  21. array = this._spritesesCache[name];
  22. }
  23. else
  24. {
  25. array = Resources.LoadAll<Sprite>("UI/Frames/" + name);
  26. this._spritesesCache.Add(name, array);
  27. }
  28. this._2DSprite.sprite2D = null;
  29. this._animation.frames = array;
  30. if (array.Length != 0)
  31. {
  32. this._animation.ResetToBeginning();
  33. this._animation.Play();
  34. }
  35. }
  36. public void Pause()
  37. {
  38. this._animation.Pause();
  39. }
  40. public void ResetToBeginning()
  41. {
  42. this._animation.ResetToBeginning();
  43. }
  44. private Dictionary<string, Sprite[]> _spritesesCache = new Dictionary<string, Sprite[]>();
  45. private const string Path = "UI/Frames/";
  46. private UI2DSpriteAnimation _animation;
  47. private UI2DSprite _2DSprite;
  48. }