| | 1 | | using System; |
| | 2 | | using System.Collections; |
| | 3 | | using UnityEngine; |
| | 4 | |
|
| | 5 | | namespace DCL |
| | 6 | | { |
| | 7 | | /// <summary> |
| | 8 | | /// Player for a Gif Asset. |
| | 9 | | /// Player will stop if Gif Asset is disposed. |
| | 10 | | /// Is not this player responsibility to dispose Gif Asset. Gif Asset should be explicitly disposed. |
| | 11 | | /// </summary> |
| | 12 | | public class GifPlayer : IDisposable |
| | 13 | | { |
| | 14 | | public event Action<Texture2D> OnFrameTextureChanged; |
| | 15 | |
|
| 0 | 16 | | public bool isPlaying { get; private set; } = false; |
| | 17 | |
|
| | 18 | | private Asset_Gif gifAsset = null; |
| | 19 | | private int currentFrameIdx = 0; |
| | 20 | | private Coroutine updateRoutine = null; |
| | 21 | | private float currentTimeDelay = 0; |
| | 22 | |
|
| 6 | 23 | | public GifPlayer(Asset_Gif asset) { SetGif(asset); } |
| | 24 | |
|
| 0 | 25 | | public GifPlayer() { } |
| | 26 | |
|
| | 27 | | /// <summary> |
| | 28 | | /// Set gif asset for the player |
| | 29 | | /// </summary> |
| | 30 | | /// <param name="asset">gif asset</param> |
| | 31 | | public void SetGif(Asset_Gif asset) |
| | 32 | | { |
| 2 | 33 | | gifAsset = asset; |
| | 34 | |
|
| 2 | 35 | | if (isPlaying && IsValidAsset()) |
| | 36 | | { |
| 0 | 37 | | if (currentFrameIdx >= asset.frames.Length) |
| | 38 | | { |
| 0 | 39 | | currentFrameIdx = 0; |
| | 40 | | } |
| 0 | 41 | | SetFrame(currentFrameIdx); |
| | 42 | | } |
| 2 | 43 | | } |
| | 44 | |
|
| | 45 | | public void Play(bool reset = false) |
| | 46 | | { |
| 2 | 47 | | if (reset) |
| | 48 | | { |
| 0 | 49 | | currentFrameIdx = 0; |
| 0 | 50 | | currentTimeDelay = 0; |
| 0 | 51 | | Stop(); |
| | 52 | | } |
| | 53 | |
|
| 2 | 54 | | isPlaying = true; |
| | 55 | |
|
| 2 | 56 | | if (updateRoutine == null && gifAsset != null) |
| | 57 | | { |
| 0 | 58 | | updateRoutine = CoroutineStarter.Start(UpdateRoutine()); |
| | 59 | | } |
| 2 | 60 | | } |
| | 61 | |
|
| | 62 | | public void Stop() |
| | 63 | | { |
| 0 | 64 | | isPlaying = false; |
| | 65 | |
|
| 0 | 66 | | if (updateRoutine != null) |
| | 67 | | { |
| 0 | 68 | | CoroutineStarter.Stop(updateRoutine); |
| 0 | 69 | | updateRoutine = null; |
| | 70 | | } |
| 0 | 71 | | } |
| | 72 | |
|
| 0 | 73 | | public void Dispose() { Stop(); } |
| | 74 | |
|
| | 75 | | private IEnumerator UpdateRoutine() |
| | 76 | | { |
| 0 | 77 | | while (isPlaying) |
| | 78 | | { |
| 0 | 79 | | yield return WaitForSecondsCache.Get(currentTimeDelay); |
| 0 | 80 | | UpdateFrame(); |
| | 81 | | } |
| 0 | 82 | | } |
| | 83 | |
|
| 0 | 84 | | private bool IsValidAsset() { return gifAsset?.frames != null && gifAsset.frames.Length > 0; } |
| | 85 | |
|
| | 86 | | private void UpdateFrame() |
| | 87 | | { |
| 0 | 88 | | if (!IsValidAsset()) |
| | 89 | | { |
| 0 | 90 | | return; |
| | 91 | | } |
| | 92 | |
|
| 0 | 93 | | currentFrameIdx++; |
| | 94 | |
|
| 0 | 95 | | if (currentFrameIdx >= gifAsset.frames.Length) |
| | 96 | | { |
| 0 | 97 | | currentFrameIdx = 0; |
| | 98 | | } |
| | 99 | |
|
| 0 | 100 | | SetFrame(currentFrameIdx); |
| 0 | 101 | | } |
| | 102 | |
|
| | 103 | | private void SetFrame(int frameIdx) |
| | 104 | | { |
| 0 | 105 | | currentTimeDelay = gifAsset.frames[frameIdx].delay; |
| 0 | 106 | | OnFrameTextureChanged?.Invoke(gifAsset.frames[frameIdx].texture); |
| 0 | 107 | | } |
| | 108 | | } |
| | 109 | | } |