Skip to content

Commit c301900

Browse files
committed
Merge branch 'master' into pooled-scene-buffering
2 parents 8359d44 + 330b0b8 commit c301900

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+1653
-462
lines changed

FUNDING.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
liberapay: TwoTen

MLAPI-Editor/NetworkingManagerEditor.cs

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
using UnityEditorInternal;
77
using MLAPI;
88
using MLAPI.Transports;
9-
using UnityEditor.Callbacks;
109

1110
[CustomEditor(typeof(NetworkingManager), true)]
1211
[CanEditMultipleObjects]
@@ -22,7 +21,7 @@ public class NetworkingManagerEditor : Editor
2221

2322
// NetworkConfig fields
2423
private SerializedProperty protocolVersionProperty;
25-
private SerializedProperty allowRuntimeSceneChanges;
24+
private SerializedProperty allowRuntimeSceneChangesProperty;
2625
private SerializedProperty networkTransportProperty;
2726
private SerializedProperty receiveTickrateProperty;
2827
private SerializedProperty maxReceiveEventsPerTickRateProperty;
@@ -38,6 +37,7 @@ public class NetworkingManagerEditor : Editor
3837
private SerializedProperty createPlayerPrefabProperty;
3938
private SerializedProperty forceSamePrefabsProperty;
4039
private SerializedProperty usePrefabSyncProperty;
40+
private SerializedProperty enableSceneManagementProperty;
4141
private SerializedProperty recycleNetworkIdsProperty;
4242
private SerializedProperty networkIdRecycleDelayProperty;
4343
private SerializedProperty rpcHashSizeProperty;
@@ -102,7 +102,7 @@ private void Init()
102102

103103
// NetworkConfig properties
104104
protocolVersionProperty = networkConfigProperty.FindPropertyRelative("ProtocolVersion");
105-
allowRuntimeSceneChanges = networkConfigProperty.FindPropertyRelative("AllowRuntimeSceneChanges");
105+
allowRuntimeSceneChangesProperty = networkConfigProperty.FindPropertyRelative("AllowRuntimeSceneChanges");
106106
networkTransportProperty = networkConfigProperty.FindPropertyRelative("NetworkTransport");
107107
receiveTickrateProperty = networkConfigProperty.FindPropertyRelative("ReceiveTickrate");
108108
maxReceiveEventsPerTickRateProperty = networkConfigProperty.FindPropertyRelative("MaxReceiveEventsPerTickRate");
@@ -118,6 +118,7 @@ private void Init()
118118
createPlayerPrefabProperty = networkConfigProperty.FindPropertyRelative("CreatePlayerPrefab");
119119
forceSamePrefabsProperty = networkConfigProperty.FindPropertyRelative("ForceSamePrefabs");
120120
usePrefabSyncProperty = networkConfigProperty.FindPropertyRelative("UsePrefabSync");
121+
enableSceneManagementProperty = networkConfigProperty.FindPropertyRelative("EnableSceneManagement");
121122
recycleNetworkIdsProperty = networkConfigProperty.FindPropertyRelative("RecycleNetworkIds");
122123
networkIdRecycleDelayProperty = networkConfigProperty.FindPropertyRelative("NetworkIdRecycleDelay");
123124
rpcHashSizeProperty = networkConfigProperty.FindPropertyRelative("RpcHashSize");
@@ -142,7 +143,7 @@ private void CheckNullProperties()
142143

143144
// NetworkConfig properties
144145
protocolVersionProperty = networkConfigProperty.FindPropertyRelative("ProtocolVersion");
145-
allowRuntimeSceneChanges = networkConfigProperty.FindPropertyRelative("AllowRuntimeSceneChanges");
146+
allowRuntimeSceneChangesProperty = networkConfigProperty.FindPropertyRelative("AllowRuntimeSceneChanges");
146147
networkTransportProperty = networkConfigProperty.FindPropertyRelative("NetworkTransport");
147148
receiveTickrateProperty = networkConfigProperty.FindPropertyRelative("ReceiveTickrate");
148149
maxReceiveEventsPerTickRateProperty = networkConfigProperty.FindPropertyRelative("MaxReceiveEventsPerTickRate");
@@ -158,6 +159,7 @@ private void CheckNullProperties()
158159
createPlayerPrefabProperty = networkConfigProperty.FindPropertyRelative("CreatePlayerPrefab");
159160
forceSamePrefabsProperty = networkConfigProperty.FindPropertyRelative("ForceSamePrefabs");
160161
usePrefabSyncProperty = networkConfigProperty.FindPropertyRelative("UsePrefabSync");
162+
enableSceneManagementProperty = networkConfigProperty.FindPropertyRelative("EnableSceneManagement");
161163
recycleNetworkIdsProperty = networkConfigProperty.FindPropertyRelative("RecycleNetworkIds");
162164
networkIdRecycleDelayProperty = networkConfigProperty.FindPropertyRelative("NetworkIdRecycleDelay");
163165
rpcHashSizeProperty = networkConfigProperty.FindPropertyRelative("RpcHashSize");
@@ -256,8 +258,11 @@ public override void OnInspectorGUI()
256258
EditorGUILayout.Space();
257259
networkPrefabsList.DoLayoutList();
258260

259-
registeredScenesList.DoLayoutList();
260-
EditorGUILayout.Space();
261+
using (new EditorGUI.DisabledScope(!networkingManager.NetworkConfig.EnableSceneManagement))
262+
{
263+
registeredScenesList.DoLayoutList();
264+
EditorGUILayout.Space();
265+
}
261266

262267

263268
EditorGUILayout.LabelField("General", EditorStyles.boldLabel);
@@ -321,7 +326,23 @@ public override void OnInspectorGUI()
321326
EditorGUILayout.LabelField("Spawning", EditorStyles.boldLabel);
322327
EditorGUILayout.PropertyField(createPlayerPrefabProperty);
323328
EditorGUILayout.PropertyField(forceSamePrefabsProperty);
324-
EditorGUILayout.PropertyField(usePrefabSyncProperty);
329+
330+
using (new EditorGUI.DisabledScope(!networkingManager.NetworkConfig.EnableSceneManagement))
331+
{
332+
bool value = networkingManager.NetworkConfig.UsePrefabSync;
333+
334+
if (!networkingManager.NetworkConfig.EnableSceneManagement)
335+
{
336+
usePrefabSyncProperty.boolValue = true;
337+
}
338+
339+
EditorGUILayout.PropertyField(usePrefabSyncProperty);
340+
341+
if (!networkingManager.NetworkConfig.EnableSceneManagement)
342+
{
343+
usePrefabSyncProperty.boolValue = value;
344+
}
345+
}
325346

326347
EditorGUILayout.PropertyField(recycleNetworkIdsProperty);
327348

@@ -341,8 +362,13 @@ public override void OnInspectorGUI()
341362
EditorGUILayout.PropertyField(rpcHashSizeProperty);
342363

343364
EditorGUILayout.LabelField("Scene Management", EditorStyles.boldLabel);
344-
EditorGUILayout.PropertyField(loadSceneTimeOutProperty);
345-
EditorGUILayout.PropertyField(allowRuntimeSceneChanges);
365+
EditorGUILayout.PropertyField(enableSceneManagementProperty);
366+
367+
using (new EditorGUI.DisabledScope(!networkingManager.NetworkConfig.EnableSceneManagement))
368+
{
369+
EditorGUILayout.PropertyField(loadSceneTimeOutProperty);
370+
EditorGUILayout.PropertyField(allowRuntimeSceneChangesProperty);
371+
}
346372

347373
EditorGUILayout.LabelField("Cryptography", EditorStyles.boldLabel);
348374
EditorGUILayout.PropertyField(enableEncryptionProperty);
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
using System;
2+
using MLAPI;
3+
using MLAPI.Messaging;
4+
using UnityEngine;
5+
6+
namespace MLAPI_Examples
7+
{
8+
public class BasicRpcPositionSync : NetworkedBehaviour
9+
{
10+
// Send position no more than 20 times a second
11+
public int SendsPerSecond = 20;
12+
// Dont send position unless we have moved at least 1 cm
13+
public float MinimumMovement = 0.01f;
14+
// Dont send position unless we have rotated at least 1 degree
15+
public float MinimumRotation = 1f;
16+
// Allows you to change the channel position updates are sent on. Its prefered to be UnreliableSequenced for fast paced.
17+
public string Channel = "MLAPI_DEFAULT_MESSAGE";
18+
19+
private float lastSendTime;
20+
private Vector3 lastSendPosition;
21+
private Quaternion lastSendRotation;
22+
23+
public override void NetworkStart()
24+
{
25+
// This is called when the object is spawned. Once this gets invoked. The object is ready for RPC and var changes.
26+
27+
// Set the defaults to prevent a position update straight after spawn with the same redundant values. (The MLAPI syncs positions on spawn)
28+
lastSendPosition = transform.position;
29+
lastSendRotation = transform.rotation;
30+
}
31+
32+
private void Update()
33+
{
34+
// Check if its time to send a new position update
35+
if (IsOwner && Time.time - lastSendTime > (1f / SendsPerSecond))
36+
{
37+
// Check if we have moved enough or rotated enough for a position update
38+
if (Vector3.Distance(lastSendPosition, transform.position) >= MinimumMovement || Quaternion.Angle(lastSendRotation, transform.rotation) > MinimumRotation)
39+
{
40+
// We moved enough.
41+
42+
// Set the last states
43+
lastSendTime = Time.time;
44+
lastSendPosition = transform.position;
45+
lastSendRotation = transform.rotation;
46+
47+
if (IsClient)
48+
{
49+
// If we are a client. (A client can be either a normal client or a HOST), we want to send a ServerRPC. ServerRPCs does work for host to make code consistent.
50+
InvokeServerRpc(SendPositionToServer, transform.position, transform.rotation, Channel);
51+
}
52+
else if (IsServer)
53+
{
54+
// This is a strict server with no client attached. We can thus send the ClientRPC straight away without the server inbetween.
55+
InvokeClientRpcOnEveryone(SetPosition, transform.position, transform.rotation, Channel);
56+
}
57+
}
58+
}
59+
}
60+
61+
[ServerRPC(RequireOwnership = true)]
62+
public void SendPositionToServer(Vector3 position, Quaternion rotation)
63+
{
64+
// This code gets ran on the server at the request of clients or the host
65+
66+
// Tell every client EXCEPT the owner (since they are the ones that actually send the position) to apply the new position
67+
InvokeClientRpcOnEveryoneExcept(SetPosition, OwnerClientId, position, rotation, Channel);
68+
}
69+
70+
[ClientRPC]
71+
public void SetPosition(Vector3 position, Quaternion rotation)
72+
{
73+
// This code gets ran on the clients at the request of the server.
74+
75+
transform.position = position;
76+
transform.rotation = rotation;
77+
}
78+
}
79+
}

MLAPI/Configuration/MLAPIConstants.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
namespace MLAPI.Configuration
1+
namespace MLAPI.Configuration
22
{
33
/// <summary>
44
/// A static class containing MLAPI constants
@@ -30,6 +30,7 @@ internal static class MLAPIConstants
3030
internal const byte MLAPI_UNNAMED_MESSAGE = 20;
3131
internal const byte MLAPI_DESTROY_OBJECTS = 21;
3232
internal const byte MLAPI_NAMED_MESSAGE = 22;
33+
internal const byte MLAPI_SYNCED_VAR = 23;
3334
internal const byte INVALID = 32;
3435

3536
internal static readonly string[] MESSAGE_NAMES = {
@@ -56,7 +57,7 @@ internal static class MLAPIConstants
5657
"MLAPI_UNNAMED_MESSAGE",
5758
"MLAPI_DESTROY_OBJECTS",
5859
"MLAPI_NAMED_MESSAGE",
59-
"",
60+
"MLAPI_SYNCED_VAR",
6061
"",
6162
"",
6263
"",

MLAPI/Configuration/NetworkConfig.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,13 @@ public class NetworkConfig
126126
[Tooltip("Ensures that NetworkedVars can be read even if a client accidental writes where its not allowed to. This will cost some CPU time and bandwidth")]
127127
public bool EnsureNetworkedVarLengthSafety = false;
128128
/// <summary>
129+
/// Enables scene management. This will allow network scene switches and automatic scene diff corrections upon connect.
130+
/// SoftSynced scene objects wont work with this disabled. That means that disabling SceneManagement also enables PrefabSync.
131+
/// </summary>
132+
[Tooltip("Enables scene management. This will allow network scene switches and automatic scene diff corrections upon connect.\n" +
133+
"SoftSynced scene objects wont work with this disabled. That means that disabling SceneManagement also enables PrefabSync.")]
134+
public bool EnableSceneManagement = true;
135+
/// <summary>
129136
/// Whether or not the MLAPI should check for differences in the prefabs at connection.
130137
/// If you dynamically add prefabs at runtime, turn this OFF
131138
/// </summary>
@@ -253,6 +260,7 @@ public string ToBase64()
253260
writer.WriteBits((byte)config.RpcHashSize, 3);
254261
writer.WriteBool(ForceSamePrefabs);
255262
writer.WriteBool(UsePrefabSync);
263+
writer.WriteBool(EnableSceneManagement);
256264
writer.WriteBool(RecycleNetworkIds);
257265
writer.WriteSinglePacked(NetworkIdRecycleDelay);
258266
writer.WriteBool(EnableNetworkedVar);
@@ -300,6 +308,7 @@ public void FromBase64(string base64)
300308
config.RpcHashSize = (HashSize)reader.ReadBits(3);
301309
config.ForceSamePrefabs = reader.ReadBool();
302310
config.UsePrefabSync = reader.ReadBool();
311+
config.EnableSceneManagement = reader.ReadBool();
303312
config.RecycleNetworkIds = reader.ReadBool();
304313
config.NetworkIdRecycleDelay = reader.ReadSinglePacked();
305314
config.EnableNetworkedVar = reader.ReadBool();
@@ -349,6 +358,7 @@ public ulong GetConfig(bool cache = true)
349358
writer.WriteBool(EnableNetworkedVar);
350359
writer.WriteBool(ForceSamePrefabs);
351360
writer.WriteBool(UsePrefabSync);
361+
writer.WriteBool(EnableSceneManagement);
352362
writer.WriteBool(EnsureNetworkedVarLengthSafety);
353363
writer.WriteBool(EnableEncryption);
354364
writer.WriteBool(SignKeyExchange);

0 commit comments

Comments
 (0)