Skip to content

Commit c383c2e

Browse files
committed
CoroutineUtil Instance fix
1 parent 6c91d9b commit c383c2e

3 files changed

Lines changed: 23 additions & 15 deletions

File tree

Runtime/Dom/Dom.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -522,4 +522,4 @@ struct TickBasedCallTracker {
522522
public int count;
523523
}
524524
}
525-
}
525+
}

Runtime/Dom/Elements/Flipbook.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ public class Flipbook : Image {
1010
public object src {
1111
get { return _src; }
1212
set {
13-
if (value is Texture2D) {
14-
_texture = (Texture2D)value;
13+
if (value is Texture2D tex) {
14+
_texture = tex;
1515
this.image = _texture;
1616
Reset();
1717
}
@@ -67,6 +67,8 @@ public Flipbook() {
6767
}
6868

6969
void Reset() {
70+
if (_texture == null)
71+
return;
7072
_index = 0;
7173
_cellWidth = _texture.width / _numPerRow;
7274
_cellHeight = _texture.height / Math.Max(1, _count / _numPerRow);
@@ -76,7 +78,8 @@ void Reset() {
7678
}
7779

7880
void Animate() {
79-
this.sourceRect = new Rect((_index % _numPerRow) * _cellWidth, (_index / _numPerRow) * _cellHeight,
81+
this.sourceRect = new Rect((_index % _numPerRow) * _cellWidth,
82+
(_index / _numPerRow) * _cellHeight,
8083
_cellWidth, _cellHeight);
8184
if (_randomRotation && _index == 0) {
8285
this.style.rotate = new StyleRotate(new Rotate(new Angle(Random.Range(0, 360f))));
@@ -87,4 +90,4 @@ void Animate() {
8790
CoroutineUtil.Start(_currentCo);
8891
}
8992
}
90-
}
93+
}

Runtime/Utils/CoroutineUtil.cs

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,28 @@
44

55
namespace OneJS.Utils {
66
public class CoroutineUtil : MonoBehaviour {
7-
public static CoroutineUtil Instance => instance;
8-
static CoroutineUtil instance;
9-
10-
void Awake() {
11-
instance = this;
7+
public static CoroutineUtil Instance {
8+
get {
9+
if (instance == null) {
10+
var go = new GameObject("CoroutineUtil");
11+
DontDestroyOnLoad(go);
12+
instance = go.AddComponent<CoroutineUtil>();
13+
}
14+
return instance;
15+
}
1216
}
17+
static CoroutineUtil instance;
1318

1419
public static void Start(IEnumerator routine) {
15-
instance.StartCoroutine(routine);
20+
Instance.StartCoroutine(routine);
1621
}
1722

1823
public static void Stop(IEnumerator routine) {
19-
instance.StopCoroutine(routine);
24+
Instance.StopCoroutine(routine);
2025
}
2126

2227
public static void StopAll() {
23-
instance.StopAllCoroutines();
28+
Instance.StopAllCoroutines();
2429
}
2530

2631
/**
@@ -34,7 +39,7 @@ public static void StopAll() {
3439
*/
3540
public static IEnumerator Chain(params IEnumerator[] actions) {
3641
foreach (IEnumerator action in actions) { // <- this foreach may be source of mem leaks
37-
yield return instance.StartCoroutine(action);
42+
yield return Instance.StartCoroutine(action);
3843
}
3944
}
4045

@@ -82,4 +87,4 @@ public static IEnumerator Do(Action action) {
8287
yield return 0;
8388
}
8489
}
85-
}
90+
}

0 commit comments

Comments
 (0)