mirror of
https://github.com/MrUnknownDE/examples-image-loading.git
synced 2026-04-21 07:43:45 +02:00
Reorganizes scene
Adds comments, renamed methods in `SlideshowFrame.cs` Moved materials to `Materials\` folder Simplifies shaders (Standard Lite -> Diffuse, Alpha Blended Particle-> Unlit
This commit is contained in:
@@ -1,42 +1,50 @@
|
||||
using UdonSharp;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Serialization;
|
||||
using UnityEngine.UI;
|
||||
using VRC.SDK3.Image;
|
||||
using VRC.SDK3.StringLoading;
|
||||
using VRC.SDKBase;
|
||||
using VRC.Udon.Common.Interfaces;
|
||||
|
||||
[UdonBehaviourSyncMode(BehaviourSyncMode.None)]
|
||||
public class SlideshowFrame : UdonSharpBehaviour
|
||||
{
|
||||
public VRCUrl[] imageUrls;
|
||||
public VRCUrl stringUrl;
|
||||
public new Renderer renderer;
|
||||
public Text field;
|
||||
public float slideDurationSeconds = 10f;
|
||||
[SerializeField, Tooltip("URLs of images to load")]
|
||||
private VRCUrl[] imageUrls;
|
||||
|
||||
[SerializeField, Tooltip("URL of text file containing captions for images, one caption per line.")]
|
||||
private VRCUrl stringUrl;
|
||||
|
||||
[SerializeField, Tooltip("Renderer to show downloaded images on.")]
|
||||
private new Renderer renderer;
|
||||
|
||||
[SerializeField, Tooltip("Text field for captions.")]
|
||||
private Text field;
|
||||
|
||||
[SerializeField, Tooltip("Duration in seconds until the next image is shown.")]
|
||||
private float slideDurationSeconds = 10f;
|
||||
|
||||
// Private Variables
|
||||
private int _loadedIndex = -1;
|
||||
private VRCImageDownloader _imageDownloader;
|
||||
private IUdonEventReceiver udonEventReceiver;
|
||||
private IUdonEventReceiver _udonEventReceiver;
|
||||
private string[] _captions = new string[0];
|
||||
private Texture2D[] _textures;
|
||||
private Texture2D[] _downloadedTextures;
|
||||
|
||||
void Start()
|
||||
private void Start()
|
||||
{
|
||||
// Cast self to the type needed for Image and String Loading methods
|
||||
udonEventReceiver = (IUdonEventReceiver)this;
|
||||
// Downloaded textures will be cached in a texture array.
|
||||
_downloadedTextures = new Texture2D[imageUrls.Length];
|
||||
|
||||
// Track which downloads have been completed already
|
||||
// _downloadsComplete = new bool[rgbUrl.Length];
|
||||
_textures = new Texture2D[imageUrls.Length];
|
||||
|
||||
// Construct Image Downloader to reuse
|
||||
// It's important to store the VRCImageDownloader as a variable, to stop it from being garbage collected!
|
||||
_imageDownloader = new VRCImageDownloader();
|
||||
|
||||
// Load Captions
|
||||
VRCStringDownloader.LoadUrl(stringUrl, udonEventReceiver);
|
||||
// To receive Image and String loading events, 'this' is casted to the type needed
|
||||
_udonEventReceiver = (IUdonEventReceiver)this;
|
||||
|
||||
// Captions are downloaded once. On success, OnImageLoadSuccess() will be called.
|
||||
VRCStringDownloader.LoadUrl(stringUrl, _udonEventReceiver);
|
||||
|
||||
// Load the next image. Then do it again, and again, and...
|
||||
LoadNextRecursive();
|
||||
}
|
||||
|
||||
@@ -48,22 +56,28 @@ public class SlideshowFrame : UdonSharpBehaviour
|
||||
|
||||
private void LoadNext()
|
||||
{
|
||||
// All clients share the same server time. That's used to sync the currently displayed image.
|
||||
_loadedIndex = (int)(Networking.GetServerTimeInMilliseconds() / 1000f / slideDurationSeconds) % imageUrls.Length;
|
||||
|
||||
var nextTexture = _textures[_loadedIndex];
|
||||
var nextTexture = _downloadedTextures[_loadedIndex];
|
||||
|
||||
if (nextTexture != null)
|
||||
{
|
||||
// Image already downloaded! No need to download it again.
|
||||
renderer.sharedMaterial.mainTexture = nextTexture;
|
||||
}
|
||||
else
|
||||
{
|
||||
var rgbInfo = new TextureInfo();
|
||||
rgbInfo.GenerateMipMaps = true;
|
||||
_imageDownloader.DownloadImage(imageUrls[_loadedIndex], renderer.material, udonEventReceiver, rgbInfo);
|
||||
_imageDownloader.DownloadImage(imageUrls[_loadedIndex], renderer.material, _udonEventReceiver, rgbInfo);
|
||||
}
|
||||
|
||||
UpdateCaptionText();
|
||||
}
|
||||
|
||||
// Set caption if one is provided
|
||||
private void UpdateCaptionText()
|
||||
{
|
||||
if (_loadedIndex < _captions.Length)
|
||||
{
|
||||
field.text = _captions[_loadedIndex];
|
||||
@@ -77,6 +91,7 @@ public class SlideshowFrame : UdonSharpBehaviour
|
||||
public override void OnStringLoadSuccess(IVRCStringDownload result)
|
||||
{
|
||||
_captions = result.Result.Split('\n');
|
||||
UpdateCaptionText();
|
||||
}
|
||||
|
||||
public override void OnStringLoadError(IVRCStringDownload result)
|
||||
@@ -88,7 +103,7 @@ public class SlideshowFrame : UdonSharpBehaviour
|
||||
{
|
||||
Debug.Log($"Image loaded: {result.SizeInMemoryBytes} bytes.");
|
||||
|
||||
_textures[_loadedIndex] = result.Result;
|
||||
_downloadedTextures[_loadedIndex] = result.Result;
|
||||
}
|
||||
|
||||
public override void OnImageLoadError(IVRCImageDownload result)
|
||||
|
||||
Reference in New Issue
Block a user