Advertisement
Guest User

Untitled

a guest
Apr 4th, 2020
747
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.93 KB | None | 0 0
  1. using UdonSharp;
  2. using UnityEngine;
  3. using VRC.SDKBase;
  4. using VRC.Udon;
  5.  
  6. public class SyncedTrigger : UdonSharpBehaviour
  7. {
  8.     public GameObject toggleObject;
  9.     public UdonBehaviour ud;
  10.  
  11.     [UdonSynced] private bool isEnabled;
  12.  
  13.     private VRCPlayerApi playerApi;
  14.     private bool isInEditor;
  15.     private bool skipStateUpdates;
  16.  
  17.     private string enableSphere;
  18.     private string disableSphere;
  19.  
  20.     void Start()
  21.     {
  22.         playerApi = Networking.LocalPlayer;
  23.         isInEditor = playerApi == null;
  24.         enableSphere = "EnableSphere";
  25.         disableSphere = "DisableSphere";
  26.  
  27.         // not working, isEnabled not initialized yet by vrchst at this point
  28.         //toggleObject.SetActive(isEnabled);
  29.     }
  30.  
  31.     public override void OnPlayerJoined(VRCPlayerApi player)
  32.     {
  33.         if (player == playerApi)
  34.             toggleObject.SetActive(isEnabled);
  35.     }
  36.  
  37.     private void LateUpdate()
  38.     {
  39.         // Optimization, stop updating object state every frame if button was pressed
  40.         if(!skipStateUpdates)
  41.         {
  42.             // hack to force late joiners get correct object state
  43.             toggleObject.SetActive(isEnabled);
  44.         }
  45.     }
  46.  
  47.     public override void Interact()
  48.     {
  49.         if (isInEditor)
  50.             return;
  51.  
  52.         // not needed since we sending event to all players including owner
  53.         //Networking.SetOwner(playerApi, this.gameObject);
  54.  
  55.         if (!toggleObject.activeSelf)
  56.             ud.SendCustomNetworkEvent(VRC.Udon.Common.Interfaces.NetworkEventTarget.All, enableSphere);
  57.         else
  58.             ud.SendCustomNetworkEvent(VRC.Udon.Common.Interfaces.NetworkEventTarget.All, disableSphere);
  59.  
  60.         skipStateUpdates = true;
  61.     }
  62.  
  63.     public void EnableSphere()
  64.     {
  65.         toggleObject.SetActive(true);
  66.         isEnabled = true;
  67.     }
  68.  
  69.     public void DisableSphere()
  70.     {
  71.         toggleObject.SetActive(false);
  72.         isEnabled = false;
  73.     }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement