main - remote-2023-08-20T02:24:11 now-2023-08-20T18:40:38
This commit is contained in:
commit
4cf5c80442
161 changed files with 12176 additions and 0 deletions
47
Assets/UdonSharp/UtilityScripts/Synced/MasterToggleObject.cs
Normal file
47
Assets/UdonSharp/UtilityScripts/Synced/MasterToggleObject.cs
Normal file
|
@ -0,0 +1,47 @@
|
|||
|
||||
using UnityEngine;
|
||||
using VRC.SDKBase;
|
||||
|
||||
namespace UdonSharp.Examples.Utilities
|
||||
{
|
||||
/// <summary>
|
||||
/// Allows the master and only the master to toggle a game object globally
|
||||
/// </summary>
|
||||
[UdonBehaviourSyncMode(BehaviourSyncMode.Manual)]
|
||||
public class MasterToggleObject : UdonSharpBehaviour
|
||||
{
|
||||
public GameObject toggleObject;
|
||||
|
||||
[UdonSynced]
|
||||
bool isObjectEnabled;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
isObjectEnabled = toggleObject.activeSelf;
|
||||
}
|
||||
|
||||
// Prevents people who are not the master from taking ownership
|
||||
public override bool OnOwnershipRequest(VRCPlayerApi requestingPlayer, VRCPlayerApi requestedOwner)
|
||||
{
|
||||
return requestedOwner.isMaster;
|
||||
}
|
||||
|
||||
public override void OnDeserialization()
|
||||
{
|
||||
toggleObject.SetActive(isObjectEnabled);
|
||||
}
|
||||
|
||||
public override void Interact()
|
||||
{
|
||||
if (!Networking.IsMaster)
|
||||
return;
|
||||
else if (!Networking.IsOwner(gameObject)) // The object may have transfer ownership on collision checked which would allow people to take ownership by accident
|
||||
Networking.SetOwner(Networking.LocalPlayer, gameObject);
|
||||
|
||||
isObjectEnabled = !isObjectEnabled;
|
||||
toggleObject.SetActive(isObjectEnabled);
|
||||
|
||||
RequestSerialization();
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue