Skip to content

Commit

Permalink
[BetterSceneLoader] Fix loading icon
Browse files Browse the repository at this point in the history
  • Loading branch information
Keelhauled committed Jul 14, 2024
1 parent ff8612c commit ba52eb5
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 49 deletions.
8 changes: 3 additions & 5 deletions src/BetterSceneLoader.Core/ImageGrid.cs
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ public void CreateUI(string name, int sortingOrder, string titleText)
loadingIcon.transform.SetRect(0.1f, 0.1f, 0.9f, 0.9f);
var loadiconTex = PngAssist.ChangeTextureFromByte(Resource.GetResourceAsBytes(typeof(ImageGrid).Assembly, "Resources.loadicon"));
loadingIcon.sprite = Sprite.Create(loadiconTex, new Rect(0, 0, loadiconTex.width, loadiconTex.height), new Vector2(0.5f, 0.5f));
LoadingIcon.Init(loadingIcon, -5f);
LoadingIcon.Init(loadingPanel.gameObject, loadingIcon, -5f);

imagelist = UIUtility.CreateScrollView("Imagelist", mainPanel.transform);
imagelist.transform.SetRect(0f, 0f, 1f, 1f, marginSize, marginSize, -marginSize, -headerSize - marginSize / 2f);
Expand Down Expand Up @@ -306,11 +306,10 @@ private void PopulateGrid(bool forceUpdate = false)

private IEnumerator LoadButtonsAsync(Transform parent, List<KeyValuePair<DateTime, string>> scenefiles)
{
LoadingIcon.loadingCount++;
foreach(var scene in scenefiles)
{
LoadingIcon.loadingState[currentCategoryFolder] = true;
var uri = "file:///" + EncodePath(scene.Value);

#if KKS
using(var uwr = UnityWebRequestTexture.GetTexture(uri, true))
{
Expand All @@ -334,8 +333,7 @@ private IEnumerator LoadButtonsAsync(Transform parent, List<KeyValuePair<DateTim
}
#endif
}

LoadingIcon.loadingState[currentCategoryFolder] = false;
LoadingIcon.loadingCount--;
}

private Button CreateSceneButton(Transform parent, Texture2D texture, string path)
Expand Down
56 changes: 12 additions & 44 deletions src/BetterSceneLoader.Core/LoadingIcon.cs
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;
}
}
}
}

0 comments on commit ba52eb5

Please sign in to comment.