forked from gilzoide/unity-lottie-player
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImageLottiePlayer.cs
198 lines (175 loc) · 5.85 KB
/
ImageLottiePlayer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
using System.Collections;
using Unity.Jobs;
using UnityEngine;
using UnityEngine.UI;
namespace Gilzoide.LottiePlayer
{
[RequireComponent(typeof(CanvasRenderer))]
public class ImageLottiePlayer : MaskableGraphic
{
[Header("Animation Options")]
[SerializeField] protected LottieAnimationAsset _animationAsset;
[SerializeField] protected AutoPlayEvent _autoPlay = AutoPlayEvent.OnStart;
[SerializeField] protected bool _loop = true;
[Header("Texture Options")]
[SerializeField, Min(2)] protected int _width = 128;
[SerializeField, Min(2)] protected int _height = 128;
[SerializeField] protected bool _keepAspect = true;
protected Texture2D _texture;
protected NativeLottieAnimation _animation;
protected float _time = 0;
protected uint _currentFrame = 0;
protected uint _lastRenderedFrame = 0;
protected JobHandle _renderJobHandle;
protected Coroutine _playCoroutine;
private string _lastAnimationAssetCacheKey;
public override Texture mainTexture => _texture;
public bool IsPlaying => _playCoroutine != null;
protected override void OnEnable()
{
base.OnEnable();
RecreateAnimationIfNeeded();
if (_autoPlay == AutoPlayEvent.OnEnable && Application.isPlaying)
{
Play();
}
}
protected override void Start()
{
base.Start();
if (_autoPlay == AutoPlayEvent.OnStart && Application.isPlaying)
{
Play();
}
}
protected override void OnDisable()
{
Pause();
base.OnDisable();
}
protected override void OnDestroy()
{
DestroyImmediate(_texture);
_animation.Dispose();
base.OnDestroy();
}
protected override void OnPopulateMesh(VertexHelper vh)
{
vh.Clear();
if (!_animation.IsValid())
{
return;
}
Rect pixelAdjustedRect = GetPixelAdjustedRect();
if (_keepAspect)
{
pixelAdjustedRect = pixelAdjustedRect.AspectFit(_animation.GetSize().GetAspect());
}
Color32 color = this.color;
vh.AddVert(new Vector3(pixelAdjustedRect.xMin, pixelAdjustedRect.yMin), color, new Vector2(0f, 1f));
vh.AddVert(new Vector3(pixelAdjustedRect.xMin, pixelAdjustedRect.yMax), color, new Vector2(0f, 0f));
vh.AddVert(new Vector3(pixelAdjustedRect.xMax, pixelAdjustedRect.yMax), color, new Vector2(1f, 0f));
vh.AddVert(new Vector3(pixelAdjustedRect.xMax, pixelAdjustedRect.yMin), color, new Vector2(1f, 1f));
vh.AddTriangle(0, 1, 2);
vh.AddTriangle(2, 3, 0);
}
[ContextMenu("Play")]
public void Play(float startTime = 0)
{
Pause();
_time = startTime;
Unpause();
}
[ContextMenu("Pause")]
public void Pause()
{
if (_playCoroutine != null)
{
StopCoroutine(_playCoroutine);
_playCoroutine = null;
}
}
[ContextMenu("Unpause")]
public void Unpause()
{
if (!IsPlaying && _animation.IsValid())
{
_playCoroutine = StartCoroutine(PlayRoutine());
}
}
protected IEnumerator PlayRoutine()
{
// force render first frame
_lastRenderedFrame = uint.MaxValue;
float duration = (float) _animation.GetDuration();
while (_loop || _time < duration)
{
_currentFrame = _animation.GetFrameAtTime(_time, _loop);
if (_currentFrame != _lastRenderedFrame)
{
ScheduleRenderJob(_currentFrame);
}
yield return null;
_time += Time.deltaTime;
if (_currentFrame != _lastRenderedFrame)
{
CompleteRenderJob();
}
}
CompleteRenderJob();
_playCoroutine = null;
}
protected void RecreateAnimationIfNeeded()
{
if (!_animationAsset)
{
return;
}
else if (_animationAsset.CacheKey != _lastAnimationAssetCacheKey)
{
_animation.Dispose();
_lastAnimationAssetCacheKey = _animationAsset.CacheKey;
}
if (!_animation.IsValid())
{
_animation = _animationAsset.CreateNativeAnimation();
}
if (_texture == null
|| _width != _texture.width
|| _height != _texture.height)
{
DestroyImmediate(_texture);
_texture = _animation.CreateTexture(_width, _height, true);
}
if (!Application.isPlaying)
{
RenderNow();
}
}
protected void RenderNow()
{
_animation.Render(_currentFrame, _texture, keepAspectRatio: false);
_texture.Apply(true);
}
protected void ScheduleRenderJob(uint frame)
{
_renderJobHandle = _animation.CreateRenderJob(frame, _texture, keepAspectRatio: false).Schedule();
}
protected void CompleteRenderJob()
{
_lastRenderedFrame = _currentFrame;
_renderJobHandle.Complete();
_texture.Apply(true);
}
#if UNITY_EDITOR
protected override void OnValidate()
{
base.OnValidate();
if (IsActive())
{
RecreateAnimationIfNeeded();
}
}
#endif
}
}