8000 improvements to faulty off logic · coderDarren/UnityCore@a571361 · GitHub
[go: up one dir, main page]

Skip to content

Commit a571361

Browse files
committed
improvements to faulty off logic
1 parent 22f37b1 commit a571361
< 8000 /div>

File tree

2 files changed

+27
-17
lines changed

2 files changed

+27
-17
lines changed

Menu/Page.cs

+23-11
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System.Threading.Tasks;
1+
using System.Collections;
22
using UnityEngine;
33

44
namespace UnityCore {
@@ -32,25 +32,37 @@ private void OnEnable() {
3232
/// <summary>
3333
/// Call this to turn the page on or off by setting the control '_on'
3434
/// </summary>
35-
public async Task<bool> Animate(bool _on) {
35+
public void Animate(bool _on) {
3636
if (useAnimation) {
3737
m_Animator.SetBool("on", _on);
38-
39-
// wait for animation to finish
40-
isAnimating = true;
41-
while (m_Animator.GetCurrentAnimatorStateInfo(0).normalizedTime < 1) {
42-
await Task.Delay(50);
43-
}
44-
isAnimating = false;
38+
39+
StopCoroutine("AwaitAnimation");
40+
StartCoroutine("AwaitAnimation", _on);
4541

4642
Log("Page ["+type+"] finished transitioning to "+(_on ? "<color=#0f0>on</color>." : "<color=#f00>off</color>."));
4743
}
48-
49-
return true;
5044
}
5145
#endregion
5246

5347
#region Private Functions
48+
private IEnumerator AwaitAnimation(bool _on) {
49+
string _targetState = _on ? "On" : "Off";
50+
isAnimating = true;
51+
52+
while (!m_Animator.GetCurrentAnimatorStateInfo(0).IsName(_targetState)) {
53+
yield return null;
54+
}
55+
while (m_Animator.GetCurrentAnimatorStateInfo(0).normalizedTime < 1) {
56+
yield return null;
57+
}
58+
59+
isAnimating = false;
60+
61+
if (!_on) {
62+
gameObject.SetActive(false);
63+
}
64+
}
65+
5466
private void CheckAnimatorIntegrity() {
5567
if (useAnimation) {
5668
// try to get animator

Menu/PageController.cs

+4-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System.Collections.Generic;
22
using System.Collections;
3-
using System.Threading.Tasks;
43
using UnityEngine;
54

65
namespace UnityCore {
@@ -42,26 +41,25 @@ private void Update() {
4241
#endregion
4342

4443
#region Public Functions
45-
public async void TurnPageOn(PageType _type) {
44+
public void TurnPageOn(PageType _type) {
4645
if (!PageExists(_type)) {
4746
LogWarning("You are trying to turn a page on ["+_type+"] that has not been registered.");
4847
return;
4948
}
5049

5150
Page _page = GetPage(_type);
5251
_page.gameObject.SetActive(true);
53-
await _page.Animate(true);
52+
_page.Animate(true);
5453
}
5554

56-
public async void TurnPageOff(PageType _type) {
55+
public void TurnPageOff(PageType _type) {
5756
if (!PageExists(_type)) {
5857
LogWarning("You are trying to turn a page off ["+_type+"] that has not been registered.");
5958
return;
6059
}
6160

6261
Page _page = GetPage(_type);
63-
await _page.Animate(false);
64-
//_page.gameObject.SetActive(false);
62+
_page.Animate(false);
6563
}
6664
#endregion
6765

0 commit comments

Comments
 (0)
0