Init
This commit is contained in:
@@ -0,0 +1,15 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace Mirror.Examples.AssignAuthority
|
||||
{
|
||||
[AddComponentMenu("")]
|
||||
public class ObserverBehaviour : NetworkBehaviour
|
||||
{
|
||||
[SyncVar(hook = nameof(OnColorChanged))]
|
||||
public Color Color;
|
||||
|
||||
public MeshRenderer MeshRenderer;
|
||||
|
||||
void OnColorChanged(Color _, Color newColor) => MeshRenderer.material.color = newColor;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 63dc059051754fb4297a90f8a292cf37
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,58 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
using Random = UnityEngine.Random;
|
||||
|
||||
namespace Mirror.Examples.AssignAuthority
|
||||
{
|
||||
[AddComponentMenu("")]
|
||||
public class OwnerBehaviour : NetworkBehaviour
|
||||
{
|
||||
[SyncVar(hook = nameof(OnSecretCodeChanged))]
|
||||
int secretCode;
|
||||
public float NewCodeInterval = 5;
|
||||
float newCodeElapsed;
|
||||
|
||||
public TextMesh CodeDisplay;
|
||||
|
||||
void Awake() =>
|
||||
// Hide the display to start with
|
||||
SetDisplay(false);
|
||||
|
||||
public override void OnStartAuthority() => SetDisplay(true);
|
||||
|
||||
public override void OnStopAuthority() =>
|
||||
// Hide the display when we don't have authority anymore
|
||||
SetDisplay(false);
|
||||
|
||||
public override void OnStartServer() => NewCode();
|
||||
|
||||
public override void OnStartClient()
|
||||
{
|
||||
// host mode: hide display
|
||||
if (isServer) SetDisplay(false);
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
newCodeElapsed += Time.deltaTime;
|
||||
if (newCodeElapsed >= NewCodeInterval)
|
||||
{
|
||||
NewCode();
|
||||
newCodeElapsed = 0;
|
||||
}
|
||||
}
|
||||
|
||||
protected override void OnValidate() => syncMode = SyncMode.Owner;
|
||||
|
||||
[Server]
|
||||
public void NewCode() => secretCode = Random.Range(0, 9999);
|
||||
|
||||
void SetDisplay(bool show) =>
|
||||
CodeDisplay.gameObject.SetActive(show);
|
||||
|
||||
void OnSecretCodeChanged(int _, int newCode)
|
||||
{
|
||||
CodeDisplay.text = newCode.ToString("0000");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 5aa27fb87ab9fd044bdc23ee3c20ccf1
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
16
Assets/Mirror/Examples/AssignAuthority/Scripts/Player.cs
Normal file
16
Assets/Mirror/Examples/AssignAuthority/Scripts/Player.cs
Normal file
@@ -0,0 +1,16 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace Mirror.Examples.AssignAuthority
|
||||
{
|
||||
[AddComponentMenu("")]
|
||||
public class Player : NetworkBehaviour
|
||||
{
|
||||
[SyncVar]
|
||||
public Color Color;
|
||||
|
||||
public override void OnStartServer()
|
||||
{
|
||||
Color = Random.ColorHSV(0, 1, 1, 1, 1, 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d463043958d54f10aa72b7cb7f8da2cd
|
||||
timeCreated: 1748521217
|
||||
48
Assets/Mirror/Examples/AssignAuthority/Scripts/UI.cs
Normal file
48
Assets/Mirror/Examples/AssignAuthority/Scripts/UI.cs
Normal file
@@ -0,0 +1,48 @@
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
namespace Mirror.Examples.AssignAuthority
|
||||
{
|
||||
[AddComponentMenu("")]
|
||||
public class UI : NetworkBehaviour
|
||||
{
|
||||
public List<ObserverBehaviour> Objects;
|
||||
public List<Button> Buttons;
|
||||
|
||||
void Awake()
|
||||
{
|
||||
for (int i = 0; i < Buttons.Count; i++)
|
||||
{
|
||||
Button button = Buttons[i];
|
||||
int index = i;
|
||||
button.onClick.AddListener(() =>
|
||||
{
|
||||
CmdTakeAuthority(index, null /* will be filled by mirror */);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
[Command(requiresAuthority = false)]
|
||||
public void CmdTakeAuthority(int index, NetworkConnectionToClient sender)
|
||||
{
|
||||
if (index < 0 || index >= Objects.Count)
|
||||
return;
|
||||
ObserverBehaviour obj = Objects[index];
|
||||
if (obj.netIdentity.connectionToClient != sender)
|
||||
{
|
||||
if (obj.netIdentity.connectionToClient != null)
|
||||
obj.netIdentity.RemoveClientAuthority();
|
||||
obj.netIdentity.AssignClientAuthority(sender);
|
||||
obj.Color = sender.identity.GetComponent<Player>().Color;
|
||||
}
|
||||
else
|
||||
{
|
||||
obj.netIdentity.RemoveClientAuthority();
|
||||
obj.Color = Color.gray;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
11
Assets/Mirror/Examples/AssignAuthority/Scripts/UI.cs.meta
Normal file
11
Assets/Mirror/Examples/AssignAuthority/Scripts/UI.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d5d8b0678c380124f903f79446ad4b5f
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user