-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[BetterSceneLoader] Fix loading icon
- Loading branch information
1 parent
ff8612c
commit ba52eb5
Showing
2 changed files
with
15 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,64 +1,32 @@ | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using UnityEngine; | ||
using UnityEngine; | ||
using UnityEngine.UI; | ||
|
||
namespace BetterSceneLoader | ||
{ | ||
public class LoadingIcon : MonoBehaviour | ||
{ | ||
public static void Init(Image image, float speed) | ||
public static void Init(GameObject parent, Image image, float speed) | ||
{ | ||
var rotator = image.gameObject.AddComponent<LoadingIcon>(); | ||
rotator.image = image; | ||
rotator.speed = speed; | ||
var icon = parent.AddComponent<LoadingIcon>(); | ||
icon.image = image; | ||
icon.speed = speed; | ||
} | ||
|
||
public static readonly Dictionary<string, bool> loadingState = new Dictionary<string, bool>(); | ||
private bool rotate; | ||
private bool prevState; | ||
public static int loadingCount = 0; | ||
private Image image; | ||
private float speed; | ||
|
||
private void Start() | ||
{ | ||
image.enabled = false; | ||
StartCoroutine(LoadingIndicator()); | ||
} | ||
|
||
private IEnumerator LoadingIndicator() | ||
{ | ||
while(true) | ||
{ | ||
bool state = loadingState.Values.Contains(true); | ||
if(state != prevState) | ||
{ | ||
prevState = state; | ||
rotate = state; | ||
|
||
if(state) | ||
{ | ||
rotate = true; | ||
image.enabled = true; | ||
} | ||
else | ||
{ | ||
rotate = false; | ||
image.enabled = false; | ||
} | ||
} | ||
|
||
yield return new WaitForSeconds(0.1f); | ||
} | ||
} | ||
|
||
private void Update() | ||
{ | ||
if(rotate) | ||
if(loadingCount > 0) | ||
{ | ||
if(!image.enabled) image.enabled = true; | ||
image.rectTransform.rotation *= Quaternion.Euler(0f, 0f, speed); | ||
} | ||
else | ||
{ | ||
if(image.enabled) image.enabled = false; | ||
} | ||
} | ||
} | ||
} |