Advertisement
Dieton

VRCExampleDemo

May 17th, 2025
359
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.26 KB | Gaming | 0 0
  1. using UdonSharp;
  2. using UnityEngine;
  3. using TMPro;
  4. using VRC.SDKBase; // Core SDK functionality
  5. using VRC.SDK3; // Main SDK features
  6. using VRC.SDK3.Components; // VRCPickup, VRCUrlInputField
  7. using VRC.SDK3.Data; // DataToken/DataDictionary
  8. using VRC.SDK3.StringLoading; // String downloads
  9. using VRC.SDK3.Video.Components.Base; // Base video player
  10. using VRC.SDK3.ImageLoading; // Texture downloads
  11. using VRC.Udon.Common.Interfaces; // Network events
  12. using VRC.SDK3.Networking; // RPCs
  13. using VRC.SDK3.Validation; // Validation attributes
  14.  
  15. // Note: Some namespaces like VRC.SDK3.Experimental are intentionally omitted
  16. // as they're unstable or editor-only
  17.  
  18. [UdonBehaviourSyncMode(BehaviourSyncMode.Manual)] // From VRC.SDKBase
  19. [RequireComponent(typeof(VRCPickup))] // From VRC.SDK3.Components
  20. public class VRCNamespaceDemo : UdonSharpBehaviour
  21. {
  22.     // ========== VRC.SDK3.Components ==========
  23.     [SerializeField] private VRCPickup _pickup;
  24.     [SerializeField] private VRCUrlInputField _urlInputField;
  25.  
  26.     // ========== VRC.SDKBase ==========
  27.     [UdonSynced] private int _syncedValue;
  28.     private VRCPlayerApi _localPlayer;
  29.  
  30.     // ========== VRC.SDK3.Data ==========
  31.     private DataDictionary _dataDictionary = new DataDictionary();
  32.  
  33.     // ========== VRC.SDK3.StringLoading ==========
  34.     [SerializeField] private VRCUrl _jsonUrl;
  35.     private string _loadedJson;
  36.  
  37.     // ========== VRC.SDK3.Video.Components.Base ==========
  38.     [SerializeField] private UnityEngine.Video.VideoPlayer _videoPlayer;
  39.  
  40.     // ========== VRC.SDK3.ImageLoading ==========
  41.     private Texture _downloadedTexture;
  42.     private VRCImageDownloader _imageDownloader;
  43.  
  44.     // ========== UI Elements ==========
  45.     [SerializeField] private TextMeshProUGUI _statusText;
  46.     [SerializeField] private Renderer _displayRenderer;
  47.  
  48.     // ========== VRC.SDK3.Validation ==========
  49.     [RecursiveMethod] // Validation attribute
  50.     private void ValidateData()
  51.     {
  52.         // Method content would go here
  53.     }
  54.  
  55.     void Start()
  56.     {
  57.         _localPlayer = Networking.LocalPlayer; // From VRC.SDKBase
  58.         InitializeDataStructure();
  59.     }
  60.  
  61.     // ========== VRC.SDK3.Data Demo ==========
  62.     private void InitializeDataStructure()
  63.     {
  64.         _dataDictionary.Add("playerCount", new DataToken(0));
  65.         _dataDictionary.Add("isActive", new DataToken(false));
  66.  
  67.         // WARNING: Complex nested structures impact performance
  68.         DataList colors = new DataList();
  69.         colors.Add(new DataToken("red"));
  70.         colors.Add(new DataToken("blue"));
  71.         _dataDictionary.Add("colors", new DataToken(colors));
  72.     }
  73.  
  74.     // ========== VRC.SDK3.StringLoading Demo ==========
  75.     public void LoadJsonData()
  76.     {
  77.         VRCStringDownloader.LoadUrl(_jsonUrl, (IUdonEventReceiver)this);
  78.     }
  79.  
  80.     public override void OnStringLoadSuccess(IVRCStringDownload result)
  81.     {
  82.         _loadedJson = result.Result;
  83.         _statusText.text = "JSON loaded!";
  84.        
  85.         // WARNING: Large JSON files may cause lag
  86.         if (VRCJson.TryDeserializeFromJson(_loadedJson, out DataToken token))
  87.         {
  88.             ProcessJsonData(token.DataDictionary);
  89.         }
  90.     }
  91.  
  92.     // ========== VRC.SDK3.Networking Demo ==========
  93.     [VRCSDK3.Networking.UdonSynced]
  94.     private int _networkedValue;
  95.  
  96.     public void IncrementNetworkedValue()
  97.     {
  98.         if (!Networking.IsOwner(gameObject))
  99.             Networking.SetOwner(_localPlayer, gameObject);
  100.  
  101.         _networkedValue++;
  102.         RequestSerialization();
  103.     }
  104.  
  105.     public override void OnDeserialization()
  106.     {
  107.         _statusText.text = $"Synced value: {_networkedValue}";
  108.     }
  109.  
  110.     // ========== VRC.SDK3.ImageLoading Demo ==========
  111.     public void DownloadImage(VRCUrl imageUrl)
  112.     {
  113.         _imageDownloader = new VRCImageDownloader();
  114.         _imageDownloader.DownloadImage(imageUrl, _displayRenderer.material, _localPlayer);
  115.     }
  116.  
  117.     // ========== VRC.SDK3.Video.Components.Base Demo ==========
  118.     public void PlayVideo(VRCUrl videoUrl)
  119.     {
  120.         _videoPlayer.url = videoUrl.ToString();
  121.         _videoPlayer.Play();
  122.     }
  123.  
  124.     // ========== Cleanup ==========
  125.     void OnDisable()
  126.     {
  127.         if (_imageDownloader != null)
  128.         {
  129.             _imageDownloader.Dispose();
  130.         }
  131.     }
  132. }
Tags: UDON
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement