From 0955bda3f6748a4cff23d9e4e798c20d80f15195 Mon Sep 17 00:00:00 2001 From: FreezeDriedMangoes Date: Wed, 25 May 2022 17:44:05 -0400 Subject: [PATCH] added comment for future enhancement --- NewHorizons/Utility/ImageUtilities.cs | 84 ++++++++++++++------------- 1 file changed, 44 insertions(+), 40 deletions(-) diff --git a/NewHorizons/Utility/ImageUtilities.cs b/NewHorizons/Utility/ImageUtilities.cs index 018abdd8..7eb0e869 100644 --- a/NewHorizons/Utility/ImageUtilities.cs +++ b/NewHorizons/Utility/ImageUtilities.cs @@ -1,12 +1,12 @@ using OWML.Common; using System; -using System.Collections; +using System.Collections; using System.Collections.Generic; using System.IO; using UnityEngine; -using UnityEngine.Events; -using UnityEngine.Networking; - +using UnityEngine.Events; +using UnityEngine.Networking; + namespace NewHorizons.Utility { public static class ImageUtilities @@ -317,43 +317,47 @@ namespace NewHorizons.Utility newTexture.Apply(); return newTexture; } - } - - // Modified from https://stackoverflow.com/a/69141085/9643841 - public class AsyncImageLoader : MonoBehaviour - { - public List pathsToLoad = new List(); + } - public class ImageLoadedEvent : UnityEvent { } - public ImageLoadedEvent imageLoadedEvent = new ImageLoadedEvent(); + // Modified from https://stackoverflow.com/a/69141085/9643841 + public class AsyncImageLoader : MonoBehaviour + { + public List pathsToLoad = new List(); + + public class ImageLoadedEvent : UnityEvent { } + public ImageLoadedEvent imageLoadedEvent = new ImageLoadedEvent(); - void Start() - { - for (int i = 0; i < pathsToLoad.Count; i++) - { - StartCoroutine(DownloadTexture(pathsToLoad[i], i)); - } - } - - IEnumerator DownloadTexture(string url, int index) - { - using (UnityWebRequest uwr = UnityWebRequestTexture.GetTexture(url)) - { - yield return uwr.SendWebRequest(); - - var hasError = uwr.error != null && uwr.error != ""; - - if (hasError) // (uwr.result != UnityWebRequest.Result.Success) - { - Debug.Log(uwr.error); - } - else - { - // Get downloaded asset bundle - var texture = DownloadHandlerTexture.GetContent(uwr); - imageLoadedEvent.Invoke(texture, index); - } - } - } + // TODO: set up an optional “StartLoading” and “StartUnloading” condition on AsyncTextureLoader, + // and make use of that for at least for projector stuff (require player to be in the same sector as the slides + // for them to start loading, and unload when the player leaves) + + void Start() + { + for (int i = 0; i < pathsToLoad.Count; i++) + { + StartCoroutine(DownloadTexture(pathsToLoad[i], i)); + } + } + + IEnumerator DownloadTexture(string url, int index) + { + using (UnityWebRequest uwr = UnityWebRequestTexture.GetTexture(url)) + { + yield return uwr.SendWebRequest(); + + var hasError = uwr.error != null && uwr.error != ""; + + if (hasError) // (uwr.result != UnityWebRequest.Result.Success) + { + Debug.Log(uwr.error); + } + else + { + // Get downloaded asset bundle + var texture = DownloadHandlerTexture.GetContent(uwr); + imageLoadedEvent.Invoke(texture, index); + } + } + } } }