Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UdonSharp;
- using UnityEngine;
- using VRC.SDKBase;
- using VRC.Udon;
- public class SyncedTrigger : UdonSharpBehaviour
- {
- public GameObject toggleObject;
- public UdonBehaviour ud;
- [UdonSynced] private bool isEnabled;
- private VRCPlayerApi playerApi;
- private bool isInEditor;
- private bool skipStateUpdates;
- private string enableSphere;
- private string disableSphere;
- void Start()
- {
- playerApi = Networking.LocalPlayer;
- isInEditor = playerApi == null;
- enableSphere = "EnableSphere";
- disableSphere = "DisableSphere";
- // not working, isEnabled not initialized yet by vrchst at this point
- //toggleObject.SetActive(isEnabled);
- }
- public override void OnPlayerJoined(VRCPlayerApi player)
- {
- if (player == playerApi)
- toggleObject.SetActive(isEnabled);
- }
- private void LateUpdate()
- {
- // Optimization, stop updating object state every frame if button was pressed
- if(!skipStateUpdates)
- {
- // hack to force late joiners get correct object state
- toggleObject.SetActive(isEnabled);
- }
- }
- public override void Interact()
- {
- if (isInEditor)
- return;
- // not needed since we sending event to all players including owner
- //Networking.SetOwner(playerApi, this.gameObject);
- if (!toggleObject.activeSelf)
- ud.SendCustomNetworkEvent(VRC.Udon.Common.Interfaces.NetworkEventTarget.All, enableSphere);
- else
- ud.SendCustomNetworkEvent(VRC.Udon.Common.Interfaces.NetworkEventTarget.All, disableSphere);
- skipStateUpdates = true;
- }
- public void EnableSphere()
- {
- toggleObject.SetActive(true);
- isEnabled = true;
- }
- public void DisableSphere()
- {
- toggleObject.SetActive(false);
- isEnabled = false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement