first commit
This commit is contained in:
60
Assets/Scripts/HelloWorldManager.cs
Normal file
60
Assets/Scripts/HelloWorldManager.cs
Normal file
@@ -0,0 +1,60 @@
|
||||
using Unity.Netcode;
|
||||
using UnityEngine;
|
||||
|
||||
namespace HelloWorld
|
||||
{
|
||||
public class HelloWorldManager : MonoBehaviour
|
||||
{
|
||||
void OnGUI()
|
||||
{
|
||||
GUILayout.BeginArea(new Rect(10, 10, 300, 300));
|
||||
if (!NetworkManager.Singleton.IsClient && !NetworkManager.Singleton.IsServer)
|
||||
{
|
||||
StartButtons();
|
||||
}
|
||||
else
|
||||
{
|
||||
StatusLabels();
|
||||
|
||||
SubmitNewPosition();
|
||||
}
|
||||
|
||||
GUILayout.EndArea();
|
||||
}
|
||||
|
||||
static void StartButtons()
|
||||
{
|
||||
if (GUILayout.Button("Host")) NetworkManager.Singleton.StartHost();
|
||||
if (GUILayout.Button("Client")) NetworkManager.Singleton.StartClient();
|
||||
if (GUILayout.Button("Server")) NetworkManager.Singleton.StartServer();
|
||||
}
|
||||
|
||||
static void StatusLabels()
|
||||
{
|
||||
var mode = NetworkManager.Singleton.IsHost ?
|
||||
"Host" : NetworkManager.Singleton.IsServer ? "Server" : "Client";
|
||||
|
||||
GUILayout.Label("Transport: " +
|
||||
NetworkManager.Singleton.NetworkConfig.NetworkTransport.GetType().Name);
|
||||
GUILayout.Label("Mode: " + mode);
|
||||
}
|
||||
|
||||
static void SubmitNewPosition()
|
||||
{
|
||||
if (GUILayout.Button(NetworkManager.Singleton.IsServer ? "Move" : "Request Position Change"))
|
||||
{
|
||||
if (NetworkManager.Singleton.IsServer && !NetworkManager.Singleton.IsClient )
|
||||
{
|
||||
foreach (ulong uid in NetworkManager.Singleton.ConnectedClientsIds)
|
||||
NetworkManager.Singleton.SpawnManager.GetPlayerNetworkObject(uid).GetComponent<HelloWorldPlayer>().Move();
|
||||
}
|
||||
else
|
||||
{
|
||||
var playerObject = NetworkManager.Singleton.SpawnManager.GetLocalPlayerObject();
|
||||
var player = playerObject.GetComponent<HelloWorldPlayer>();
|
||||
player.Move();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
11
Assets/Scripts/HelloWorldManager.cs.meta
Normal file
11
Assets/Scripts/HelloWorldManager.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ba4b698ac704847f38baedb9f4872749
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
48
Assets/Scripts/HelloWorldPlayer.cs
Normal file
48
Assets/Scripts/HelloWorldPlayer.cs
Normal file
@@ -0,0 +1,48 @@
|
||||
using Unity.Netcode;
|
||||
using UnityEngine;
|
||||
|
||||
namespace HelloWorld
|
||||
{
|
||||
public class HelloWorldPlayer : NetworkBehaviour
|
||||
{
|
||||
public NetworkVariable<Vector3> Position = new NetworkVariable<Vector3>();
|
||||
|
||||
public override void OnNetworkSpawn()
|
||||
{
|
||||
if (IsOwner)
|
||||
{
|
||||
Move();
|
||||
}
|
||||
}
|
||||
|
||||
public void Move()
|
||||
{
|
||||
if (NetworkManager.Singleton.IsServer)
|
||||
{
|
||||
var randomPosition = GetRandomPositionOnPlane();
|
||||
transform.position = randomPosition;
|
||||
Position.Value = randomPosition;
|
||||
}
|
||||
else
|
||||
{
|
||||
SubmitPositionRequestServerRpc();
|
||||
}
|
||||
}
|
||||
|
||||
[ServerRpc]
|
||||
void SubmitPositionRequestServerRpc(ServerRpcParams rpcParams = default)
|
||||
{
|
||||
Position.Value = GetRandomPositionOnPlane();
|
||||
}
|
||||
|
||||
static Vector3 GetRandomPositionOnPlane()
|
||||
{
|
||||
return new Vector3(Random.Range(-3f, 3f), 1f, Random.Range(-3f, 3f));
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
transform.position = Position.Value;
|
||||
}
|
||||
}
|
||||
}
|
11
Assets/Scripts/HelloWorldPlayer.cs.meta
Normal file
11
Assets/Scripts/HelloWorldPlayer.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 868a87f6b25e948cbab8939b40b07bf8
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
Reference in New Issue
Block a user