removing automatic client server separation

setting updated UTP values
improving instruction text
This commit is contained in:
Samuel Bellomo 2021-10-18 17:09:17 -04:00
Родитель e6a8042051
Коммит 45578350bf
11 изменённых файлов: 69 добавлений и 70 удалений

Просмотреть файл

@ -1062,8 +1062,8 @@ MonoBehaviour:
m_EditorClassIdentifier:
m_ProtocolType: 0
m_MaximumPacketSize: 1400
m_MaxPacketQueueSize: 128
m_SendQueueBatchSize: 4096
m_MaxPacketQueueSize: 256
m_SendQueueBatchSize: 16000
ConnectionData:
Address: 127.0.0.1
Port: 7777
@ -2139,7 +2139,7 @@ MonoBehaviour:
m_Name:
m_EditorClassIdentifier:
m_Material: {fileID: 0}
m_Color: {r: 0.19607843, g: 0.19607843, b: 0.19607843, a: 1}
m_Color: {r: 0, g: 0, b: 0, a: 1}
m_RaycastTarget: 1
m_RaycastPadding: {x: 0, y: 0, z: 0, w: 0}
m_Maskable: 1
@ -2159,8 +2159,8 @@ MonoBehaviour:
m_HorizontalOverflow: 0
m_VerticalOverflow: 1
m_LineSpacing: 1
m_Text: Grab ingredients, convert them to the right color and destroy them in their
respective zone
m_Text: Grab ingredients by pressing E, convert them to the right color and destroy
them in their respective zone.
--- !u!222 &2076744355
CanvasRenderer:
m_ObjectHideFlags: 0

Просмотреть файл

@ -2,10 +2,8 @@ using UnityEngine;
namespace Unity.Netcode.Samples
{
public class ClientObjectWithIngredientType : ClientServerBaseNetworkBehaviour
public class ClientObjectWithIngredientType : NetworkBehaviour
{
protected override bool ClientOnly => true;
[SerializeField]
private Material m_PurpleMaterial;
@ -24,6 +22,12 @@ namespace Unity.Netcode.Samples
m_Renderer = GetComponent<Renderer>();
}
public override void OnNetworkSpawn()
{
base.OnNetworkSpawn();
enabled = IsClient;
}
void UpdateMaterial()
{
switch (m_Server.CurrentIngredientType.Value)

Просмотреть файл

@ -7,10 +7,8 @@ using UnityEngine;
/// </summary>
[RequireComponent(typeof(ServerPlayerMove))]
[DefaultExecutionOrder(1)] // after server component
public class ClientPlayerMove : ClientServerBaseNetworkBehaviour
public class ClientPlayerMove : NetworkBehaviour
{
protected override bool ClientOnly { get; } = true;
private ServerPlayerMove m_Server;
[SerializeField]
@ -34,6 +32,8 @@ public class ClientPlayerMove : ClientServerBaseNetworkBehaviour
public override void OnNetworkSpawn()
{
base.OnNetworkSpawn();
enabled = IsClient;
if (!IsOwner)
{
m_Camera.gameObject.SetActive(false);
@ -56,6 +56,7 @@ public class ClientPlayerMove : ClientServerBaseNetworkBehaviour
// Update is called once per frame
void FixedUpdate()
{
// enabled = false if we're not the owner, so no need to guard the following code if isOwner check
// move client authoritative object. Those moves will be replicated to other players using ClientNetworkTransform (taken from Netcode's samples)
Vector3 move = Input.GetAxis("Horizontal") * transform.right + Input.GetAxis("Vertical") * transform.forward;
m_CharacterController.SimpleMove(move * Time.deltaTime * m_Speed);

Просмотреть файл

@ -1,35 +0,0 @@
namespace Unity.Netcode.Samples
{
/// <summary>
/// Helper to reduce the amount of times you forget about isServer/isClient checks. With this, all classes are server driven by default,
/// which can be overriden with the virtual properties Both/ServerOnly/ClientOnly
/// This really is an experiment, there will still be Netcode events called on disabled GameObjects. This class might give a false sense of security
/// </summary>
public abstract class ClientServerBaseNetworkBehaviour : NetworkBehaviour
{
protected virtual bool Both { get; } = false;
protected virtual bool ServerOnly { get; } = true;
protected virtual bool ClientOnly { get; } = false;
public override void OnNetworkSpawn()
{
base.OnNetworkSpawn();
if (Both)
{
return;
}
if (ClientOnly)
{
enabled = IsClient;
return;
}
if (ServerOnly)
{
enabled = IsServer;
}
}
}
}

Просмотреть файл

@ -1,3 +0,0 @@
fileFormatVersion: 2
guid: 3eb0fa4ef7ae4d2c953c6db30697b279
timeCreated: 1632343524

Просмотреть файл

@ -5,18 +5,9 @@ public class Dropzone : ServerObjectWithIngredientType
[SerializeField]
private ServerScoreReplicator m_ScoreTracker;
public override void OnNetworkSpawn()
{
base.OnNetworkSpawn();
if (!IsServer)
{
enabled = false;
}
}
private void OnTriggerEnter(Collider other)
{
if (!enabled) return;
if (!IsServer) return;
var ingredient = other.gameObject.GetComponent<ServerIngredient>();
if (ingredient == null)
{

Просмотреть файл

@ -1,10 +1,8 @@
using Unity.Netcode;
using Unity.Netcode.Samples;
using UnityEngine;
public class ServerIngredient : ServerObjectWithIngredientType
{
protected override bool Both { get; } = true;
}
public enum IngredientType
@ -15,8 +13,18 @@ public enum IngredientType
max // should be always last
}
public class ServerObjectWithIngredientType : ClientServerBaseNetworkBehaviour
public class ServerObjectWithIngredientType : NetworkBehaviour
{
public override void OnNetworkSpawn()
{
base.OnNetworkSpawn();
if (!IsServer)
{
enabled = false;
return;
}
}
[SerializeField]
public NetworkVariable<IngredientType> CurrentIngredientType;
}

Просмотреть файл

@ -1,9 +1,9 @@
using System.Collections.Generic;
using Unity.Netcode.Samples;
using Unity.Netcode;
using UnityEngine;
using Random = System.Random;
public class ServerIngredientSpawner : ClientServerBaseNetworkBehaviour
public class ServerIngredientSpawner : NetworkBehaviour
{
[SerializeField]
private List<GameObject> m_SpawnPoints;
@ -17,6 +17,16 @@ public class ServerIngredientSpawner : ClientServerBaseNetworkBehaviour
private float m_LastSpawnTime;
private Random r = new Random();
public override void OnNetworkSpawn()
{
base.OnNetworkSpawn();
if (!IsServer)
{
enabled = false;
return;
}
}
private void FixedUpdate()
{
if (NetworkManager != null && !IsServer) return;

Просмотреть файл

@ -1,7 +1,5 @@
using System.Collections;
using Unity.Netcode;
using Unity.Netcode.Components;
using Unity.Netcode.Samples;
using UnityEngine;
/// <summary>
@ -9,7 +7,7 @@ using UnityEngine;
/// and picking up objects
/// </summary>
[DefaultExecutionOrder(0)] // before client component
public class ServerPlayerMove : ClientServerBaseNetworkBehaviour
public class ServerPlayerMove : NetworkBehaviour
{
private ClientPlayerMove m_Client;
@ -26,7 +24,11 @@ public class ServerPlayerMove : ClientServerBaseNetworkBehaviour
public override void OnNetworkSpawn()
{
base.OnNetworkSpawn();
if (!enabled) return;
if (!IsServer)
{
enabled = false;
return;
}
// the following two lines should work, yet they don't. The second client connecting won't receive this position set and will spawn at the wrong position
var spawnPoint = ServerPlayerSpawnPoints.Instance.ConsumeNextSpawnPoint();

Просмотреть файл

@ -1,10 +1,20 @@
using Unity.Netcode;
using Unity.Netcode.Samples;
public class ServerScoreReplicator : ClientServerBaseNetworkBehaviour
public class ServerScoreReplicator : NetworkBehaviour
{
private NetworkVariable<int> m_ReplicatedScore = new NetworkVariable<int>();
public override void OnNetworkSpawn()
{
base.OnNetworkSpawn();
if (!IsServer)
{
enabled = false;
return;
}
}
public int Score
{
get => m_ReplicatedScore.Value;

Просмотреть файл

@ -9,9 +9,20 @@ public class ServerStove : ServerObjectWithIngredientType
[SerializeField]
private Transform m_IngredientCookingLocation;
public override void OnNetworkSpawn()
{
base.OnNetworkSpawn();
if (!IsServer)
{
enabled = false;
return;
}
}
private void OnTriggerEnter(Collider other)
{
if (!enabled) return;
if (!IsServer) return;
var ingredient = other.gameObject.GetComponent<ServerIngredient>();
if (ingredient == null)
{