diff --git a/NewHorizons/Builder/Props/ProjectionBuilder.cs b/NewHorizons/Builder/Props/ProjectionBuilder.cs index 7745ee0e..b1228f90 100644 --- a/NewHorizons/Builder/Props/ProjectionBuilder.cs +++ b/NewHorizons/Builder/Props/ProjectionBuilder.cs @@ -177,20 +177,24 @@ namespace NewHorizons.Builder.Props var slidesCount = slides.Length; var slideCollection = new SlideCollection(slidesCount); - + + var imageLoader = g.AddComponent(); for (int i = 0; i < slidesCount; i++) { var slide = new Slide(); var slideInfo = slides[i]; // TODO: do this part asynchronously so that you can load all the slides you want without stalling the game out for 5 days - var texture = ImageUtilities.GetTexture(mod, slideInfo.imagePath); - slide.textureOverride = texture; //ImageUtilities.Invert(texture); + //var texture = ImageUtilities.GetTexture(mod, slideInfo.imagePath); + //slide.textureOverride = texture; //ImageUtilities.Invert(texture); + imageLoader.pathsToLoad.Add(mod.ModHelper.Manifest.ModFolderPath + slideInfo.imagePath); AddModules(slideInfo, ref slide); slideCollection.slides[i] = slide; - } + } + imageLoader.imageLoadedEvent.AddListener((Texture2D tex, int index) => { slideCollection.slides[index].textureOverride = tex; }); + // attatch a component to store all the data for the slides that play when a vision torch scans this target var target = g.AddComponent(); @@ -236,20 +240,23 @@ namespace NewHorizons.Builder.Props var slides = info.slides; var slidesCount = slides.Length; var slideCollection = new SlideCollection(slidesCount); - + + var imageLoader = standingTorch.AddComponent(); for (int i = 0; i < slidesCount; i++) { var slide = new Slide(); var slideInfo = slides[i]; // TODO: do this part asynchronously so that you can load all the slides you want without stalling the game out for 5 days - var texture = ImageUtilities.GetTexture(mod, slideInfo.imagePath); - slide.textureOverride = texture; //ImageUtilities.Invert(texture); + //var texture = ImageUtilities.GetTexture(mod, slideInfo.imagePath); + //slide.textureOverride = texture; //ImageUtilities.Invert(texture); + imageLoader.pathsToLoad.Add(mod.ModHelper.Manifest.ModFolderPath + slideInfo.imagePath); AddModules(slideInfo, ref slide); slideCollection.slides[i] = slide; - } + } + imageLoader.imageLoadedEvent.AddListener((Texture2D tex, int index) => { slideCollection.slides[index].textureOverride = tex; }); // set up the containers for the slides var slideCollectionContainer = standingTorch.AddComponent(); diff --git a/NewHorizons/Utility/ImageUtilities.cs b/NewHorizons/Utility/ImageUtilities.cs index 3a89b7ba..4de79971 100644 --- a/NewHorizons/Utility/ImageUtilities.cs +++ b/NewHorizons/Utility/ImageUtilities.cs @@ -1,8 +1,12 @@ using OWML.Common; using System; +using System.Collections; using System.Collections.Generic; using System.IO; using UnityEngine; +using UnityEngine.Events; +using UnityEngine.Networking; + namespace NewHorizons.Utility { public static class ImageUtilities @@ -313,5 +317,45 @@ 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(); + + void Start() + { + for (int i = 0; i < pathsToLoad.Count; i++) + { + StartCoroutine(DownloadTexture(pathsToLoad[i], i)); + } + } + + IEnumerator DownloadTexture(string url, int index) + { + Logger.Log("loading img " + url); + 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); + Logger.Log("Finished loading image " + url); + imageLoadedEvent.Invoke(texture, index); + } + } + } } }