added async texture loading to standing vision torches and vision torch targets

This commit is contained in:
FreezeDriedMangoes 2022-05-25 10:40:44 -04:00
parent 714deaea05
commit ff8dfa21ad
2 changed files with 59 additions and 8 deletions

View File

@ -177,20 +177,24 @@ namespace NewHorizons.Builder.Props
var slidesCount = slides.Length;
var slideCollection = new SlideCollection(slidesCount);
var imageLoader = g.AddComponent<AsyncImageLoader>();
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<VisionTorchTarget>();
@ -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<AsyncImageLoader>();
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<SlideCollectionContainer>();

View File

@ -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<string> pathsToLoad = new List<string>();
public class ImageLoadedEvent : UnityEvent<Texture2D, int> { }
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);
}
}
}
}
}