Refactors variables, functionality still working

This commit is contained in:
Momo The Monster
2023-02-15 22:38:55 -08:00
parent 225d49eca1
commit f28e5db8c8
8 changed files with 65 additions and 79 deletions

File diff suppressed because one or more lines are too long

View File

@@ -10,7 +10,7 @@ MonoBehaviour:
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: c333ccfdd0cbdbc4ca30cef2dd6e6b9b, type: 3}
m_Name: FrameDataLoader
m_Name: SlideshowFrame
m_EditorClassIdentifier:
serializedUdonProgramAsset: {fileID: 11400000, guid: 2945372477ec6354d804691fd1e55d70,
type: 2}
@@ -21,7 +21,7 @@ MonoBehaviour:
compiledVersion: 2
behaviourSyncMode: 0
hasInteractEvent: 0
scriptID: 2487633955665927238
scriptID: -7774693031578337865
serializationData:
SerializedFormat: 2
SerializedBytes:
@@ -44,19 +44,19 @@ MonoBehaviour:
Data:
- Name:
Entry: 12
Data: 11
Data: 10
- Name:
Entry: 7
Data:
- Name: $k
Entry: 1
Data: rgbUrl
Data: imageUrls
- Name: $v
Entry: 7
Data: 2|UdonSharp.Compiler.FieldDefinition, UdonSharp.Editor
- Name: <Name>k__BackingField
Entry: 1
Data: rgbUrl
Data: imageUrls
- Name: <UserType>k__BackingField
Entry: 7
Data: 3|System.RuntimeType, mscorlib
@@ -536,19 +536,19 @@ MonoBehaviour:
Data:
- Name: $k
Entry: 1
Data: _downloadsComplete
Data: _textures
- Name: $v
Entry: 7
Data: 29|UdonSharp.Compiler.FieldDefinition, UdonSharp.Editor
- Name: <Name>k__BackingField
Entry: 1
Data: _downloadsComplete
Data: _textures
- Name: <UserType>k__BackingField
Entry: 7
Data: 30|System.RuntimeType, mscorlib
- Name:
Entry: 1
Data: System.Boolean[], mscorlib
Data: UnityEngine.Texture2D[], UnityEngine.CoreModule
- Name:
Entry: 8
Data:
@@ -585,60 +585,6 @@ MonoBehaviour:
- Name:
Entry: 8
Data:
- Name:
Entry: 7
Data:
- Name: $k
Entry: 1
Data: _textures
- Name: $v
Entry: 7
Data: 32|UdonSharp.Compiler.FieldDefinition, UdonSharp.Editor
- Name: <Name>k__BackingField
Entry: 1
Data: _textures
- Name: <UserType>k__BackingField
Entry: 7
Data: 33|System.RuntimeType, mscorlib
- Name:
Entry: 1
Data: UnityEngine.Texture2D[], UnityEngine.CoreModule
- Name:
Entry: 8
Data:
- Name: <SystemType>k__BackingField
Entry: 9
Data: 33
- Name: <SyncMode>k__BackingField
Entry: 7
Data: System.Nullable`1[[UdonSharp.UdonSyncMode, UdonSharp.Runtime]], mscorlib
- Name:
Entry: 6
Data:
- Name:
Entry: 8
Data:
- Name: <IsSerialized>k__BackingField
Entry: 5
Data: false
- Name: _fieldAttributes
Entry: 7
Data: 34|System.Collections.Generic.List`1[[System.Attribute, mscorlib]], mscorlib
- Name:
Entry: 12
Data: 0
- Name:
Entry: 13
Data:
- Name:
Entry: 8
Data:
- Name:
Entry: 8
Data:
- Name:
Entry: 8
Data:
- Name:
Entry: 13
Data:

View File

@@ -1,15 +1,15 @@
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;
public class FrameDataLoader : UdonSharpBehaviour
public class SlideshowFrame : UdonSharpBehaviour
{
public VRCUrl[] rgbUrl;
public VRCUrl[] imageUrls;
public VRCUrl stringUrl;
public new Renderer renderer;
public Text field;
@@ -20,7 +20,6 @@ public class FrameDataLoader : UdonSharpBehaviour
private VRCImageDownloader _imageDownloader;
private IUdonEventReceiver udonEventReceiver;
private string[] _captions = new string[0];
private bool[] _downloadsComplete;
private Texture2D[] _textures;
void Start()
@@ -29,8 +28,8 @@ public class FrameDataLoader : UdonSharpBehaviour
udonEventReceiver = (IUdonEventReceiver)this;
// Track which downloads have been completed already
_downloadsComplete = new bool[rgbUrl.Length];
_textures = new Texture2D[rgbUrl.Length];
// _downloadsComplete = new bool[rgbUrl.Length];
_textures = new Texture2D[imageUrls.Length];
// Construct Image Downloader to reuse
_imageDownloader = new VRCImageDownloader();
@@ -49,17 +48,19 @@ public class FrameDataLoader : UdonSharpBehaviour
private void LoadNext()
{
_loadedIndex = (int)(Networking.GetServerTimeInMilliseconds() / 1000f / slideDurationSeconds) % rgbUrl.Length;
_loadedIndex = (int)(Networking.GetServerTimeInMilliseconds() / 1000f / slideDurationSeconds) % imageUrls.Length;
if (_downloadsComplete[_loadedIndex])
var nextTexture = _textures[_loadedIndex];
if (nextTexture != null)
{
renderer.sharedMaterial.mainTexture = _textures[_loadedIndex];
renderer.sharedMaterial.mainTexture = nextTexture;
}
else
{
var rgbInfo = new TextureInfo();
rgbInfo.GenerateMipMaps = true;
_imageDownloader.DownloadImage(rgbUrl[_loadedIndex], renderer.material, udonEventReceiver, rgbInfo);
_imageDownloader.DownloadImage(imageUrls[_loadedIndex], renderer.material, udonEventReceiver, rgbInfo);
}
// Set caption if one is provided
@@ -87,18 +88,16 @@ public class FrameDataLoader : UdonSharpBehaviour
{
Debug.Log($"Image loaded: {result.SizeInMemoryBytes} bytes.");
_downloadsComplete[_loadedIndex] = true;
_textures[_loadedIndex] = result.Result;
}
public override void OnImageLoadError(IVRCImageDownload result)
{
Debug.Log($"Image not loaded: {result.Error.ToString()}: {result.ErrorMessage}.");
_downloadsComplete[_loadedIndex] = false;
}
private void OnDestroy()
{
_imageDownloader.Dispose();
}
}
}

View File

@@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:a29fc0b7ad1c79d167df2c286714c2efa3c7b4418fe2ff1369c9cce78deb1c3b
size 52736
oid sha256:4797e2ba68bf4823b5e73544017535b317e80407da67c133199aa6a548d5ec09
size 52731

View File

@@ -28,7 +28,10 @@ GraphicsSettings:
m_LensFlare:
m_Mode: 1
m_Shader: {fileID: 102, guid: 0000000000000000f000000000000000, type: 0}
m_AlwaysIncludedShaders: []
m_AlwaysIncludedShaders:
- {fileID: 16000, guid: 0000000000000000f000000000000000, type: 0}
- {fileID: 16001, guid: 0000000000000000f000000000000000, type: 0}
- {fileID: 17000, guid: 0000000000000000f000000000000000, type: 0}
m_PreloadedShaders: []
m_SpritesDefaultMaterial: {fileID: 10754, guid: 0000000000000000f000000000000000,
type: 0}

38
Readme.md Normal file
View File

@@ -0,0 +1,38 @@
# VRChat Image Downloding Examples
This is a WIP Unity Example Project that demonstrates some ways that remote images can be downloaded using Udon in VRChat Worlds.
## Getting Started
Clone or Download this project and open it up in Unity 2019.4.31f1.
## Gallery Example
Scene File: `Assets/_Project/Gallery`
This scene demonstrates a RemoteImageFrame which loads images from a web server at runtime.
### TheFrame
TheFrame is a GameObject with a couple of important pieces:
* SlideshowFrame UdonBehaviour to load the images and captions from a web server.
* Mesh object to render the frame.
* Picture mesh to render the downloaded textures.
* UI world-space canvas to render the caption.
#### SlideshowFrame
This UdonBehaviour has all of the logic to download the images and captions from a web server.
It has these public variables:
* Rgb Url - an array of all the `VRCUrls` for the images to download.
* String Url - a single `VRCUrl` where the caption text can be downloaded.
* Renderer - this renderer's sharedMaterial will have its texture set from the downloaded textures.
* Field - this UI element's `text` property will be set from the downloaded caption for the matching texture.
* Slide Duration Seconds - how long to show each image.
The basic logic flow of the script is this:
* On Start, construct a VRCImageDownloader to reuse for downloading all the images. It's important to keep this around so the textures will persist. Download the string from the `String Url`.
* If the String downloads successfully, split it up line-by-line into separate strings, and save those to a `_captions` array. If it doesn't download, log the error message.
* Next, try to Load the next Image. Increment the `_loadedIndex` to keep track of our place, then call `DownloadImage()` on the downloader we saved earlier.
* If the Image downloads successfully, save a reference to it.