Skip to content

Commit 7560f03

Browse files
committed
feat: Added the ability to add runtime scenes
1 parent 8a05a04 commit 7560f03

File tree

4 files changed

+70
-3
lines changed

4 files changed

+70
-3
lines changed

MLAPI/Configuration/NetworkConfig.cs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,13 @@ public class NetworkConfig
3333
[Tooltip("The Scenes that can be switched to by the server")]
3434
public List<string> RegisteredScenes = new List<string>();
3535
/// <summary>
36+
/// Whether or not runtime scene changes should be allowed and expected.
37+
/// If this is true, clients with different initial configurations will not worth together.
38+
/// </summary>
39+
[Tooltip("Whether or not runtime scene changes should be allowed and expected.\n " +
40+
"If this is true, clients with different initial configurations will not worth together.")]
41+
public bool AllowRuntimeSceneChanges = false;
42+
/// <summary>
3643
/// A list of spawnable prefabs
3744
/// </summary>
3845
[Tooltip("The prefabs that can be spawned across the network")]
@@ -233,6 +240,7 @@ public string ToBase64()
233240
writer.WriteBool(RecycleNetworkIds);
234241
writer.WriteSinglePacked(NetworkIdRecycleDelay);
235242
writer.WriteBool(EnableNetworkedVar);
243+
writer.WriteBool(AllowRuntimeSceneChanges);
236244
stream.PadStream();
237245

238246
return Convert.ToBase64String(stream.ToArray());
@@ -280,6 +288,7 @@ public void FromBase64(string base64)
280288
config.RecycleNetworkIds = reader.ReadBool();
281289
config.NetworkIdRecycleDelay = reader.ReadSinglePacked();
282290
config.EnableNetworkedVar = reader.ReadBool();
291+
config.AllowRuntimeSceneChanges = reader.ReadBool();
283292
}
284293
}
285294
}
@@ -305,9 +314,12 @@ public ulong GetConfig(bool cache = true)
305314
writer.WriteUInt16Packed(ProtocolVersion);
306315
writer.WriteString(MLAPIConstants.MLAPI_PROTOCOL_VERSION);
307316

308-
for (int i = 0; i < RegisteredScenes.Count; i++)
317+
if (!AllowRuntimeSceneChanges)
309318
{
310-
writer.WriteString(RegisteredScenes[i]);
319+
for (int i = 0; i < RegisteredScenes.Count; i++)
320+
{
321+
writer.WriteString(RegisteredScenes[i]);
322+
}
311323
}
312324

313325
if (ForceSamePrefabs)
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using System;
2+
3+
namespace MLAPI.Exceptions
4+
{
5+
/// <summary>
6+
/// Exception thrown when the operation can only be done on the server
7+
/// </summary>
8+
public class NetworkConfigurationException : Exception
9+
{
10+
/// <summary>
11+
/// Constructs a NetworkConfigurationException
12+
/// </summary>
13+
public NetworkConfigurationException()
14+
{
15+
16+
}
17+
18+
/// <summary>
19+
/// Constructs a NetworkConfigurationException with a message
20+
/// </summary>
21+
/// <param name="message">The exception message</param>
22+
public NetworkConfigurationException(string message) : base(message)
23+
{
24+
25+
}
26+
27+
/// <summary>
28+
/// Constructs a NetworkConfigurationException with a message and a inner exception
29+
/// </summary>
30+
/// <param name="message">The exception message</param>
31+
/// <param name="inner">The inner exception</param>
32+
public NetworkConfigurationException(string message, Exception inner) : base(message, inner)
33+
{
34+
35+
}
36+
}
37+
}

MLAPI/Exceptions/NotServerException.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace MLAPI.Exceptions
88
public class NotServerException : Exception
99
{
1010
/// <summary>
11-
/// Constructs a ObjectNotSpawnedException
11+
/// Constructs a NotServerException
1212
/// </summary>
1313
public NotServerException()
1414
{

MLAPI/SceneManagement/NetworkSceneManager.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,24 @@ internal static void SetCurrentSceneIndex()
5252

5353
internal static uint CurrentActiveSceneIndex { get; private set; } = 0;
5454

55+
/// <summary>
56+
/// Adds a scene during runtime.
57+
/// The index is REQUIRED to be unique AND the same across all instances.
58+
/// </summary>
59+
/// <param name="sceneName">Scene name.</param>
60+
/// <param name="index">Index.</param>
61+
public static void AddRuntimeSceneName(string sceneName, uint index)
62+
{
63+
if (!NetworkingManager.Singleton.NetworkConfig.AllowRuntimeSceneChanges)
64+
{
65+
throw new NetworkConfigurationException("Cannot change the scene configuration when AllowRuntimeSceneChanges is false");
66+
}
67+
68+
registeredSceneNames.Add(sceneName);
69+
sceneIndexToString.Add(index, sceneName);
70+
sceneNameToIndex.Add(sceneName, index);
71+
}
72+
5573
/// <summary>
5674
/// Switches to a scene with a given name. Can only be called from Server
5775
/// </summary>

0 commit comments

Comments
 (0)