Skip to content

Commit 03ae136

Browse files
committed
fix: simplify BasicPlayerSpawner for Basic example
1 parent af711f7 commit 03ae136

5 files changed

Lines changed: 69 additions & 161 deletions

File tree

Assets/Mirage/Samples~/Basic/Scenes/Example.unity

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -174,18 +174,10 @@ MonoBehaviour:
174174
m_Script: {fileID: 11500000, guid: ed4b54e25a3ee0940b4b750c66fa2c56, type: 3}
175175
m_Name:
176176
m_EditorClassIdentifier:
177-
Client: {fileID: 249891958}
178177
Server: {fileID: 249891959}
179-
SceneManager: {fileID: 0}
180-
ClientObjectManager: {fileID: 249891961}
181178
ServerObjectManager: {fileID: 249891962}
182179
PlayerPrefab: {fileID: 1088833923132447018, guid: 22f1fa3a0aff72b46a371f667bb4fb30,
183180
type: 3}
184-
AutoSpawn: 1
185-
SetName: 1
186-
startPositionIndex: 0
187-
startPositions: []
188-
playerSpawnMethod: 0
189181
Parent: {fileID: 379082679}
190182
--- !u!114 &249891956
191183
MonoBehaviour:
@@ -201,7 +193,6 @@ MonoBehaviour:
201193
m_EditorClassIdentifier:
202194
Server: {fileID: 249891959}
203195
Client: {fileID: 249891958}
204-
NetworkSceneManager: {fileID: 0}
205196
ServerObjectManager: {fileID: 249891962}
206197
ClientObjectManager: {fileID: 249891961}
207198
ValidateReferences: 1
@@ -234,6 +225,7 @@ MonoBehaviour:
234225
m_EditorClassIdentifier:
235226
EnablePeerMetrics: 0
236227
MetricsSize: 10
228+
PeerConfigProfile: 0
237229
SocketFactory: {fileID: 249891954}
238230
ObjectManager: {fileID: 249891961}
239231
DisconnectOnException: 1
@@ -271,9 +263,8 @@ MonoBehaviour:
271263
m_EditorClassIdentifier:
272264
EnablePeerMetrics: 0
273265
MetricsSize: 10
266+
PeerConfigProfile: 0
274267
MaxConnections: 4
275-
DisconnectOnException: 1
276-
RethrowException: 1
277268
RunInBackground: 1
278269
Listening: 1
279270
SocketFactory: {fileID: 249891954}
@@ -304,6 +295,13 @@ MonoBehaviour:
304295
_event:
305296
m_PersistentCalls:
306297
m_Calls: []
298+
DisconnectOnException: 1
299+
RethrowException: 1
300+
ErrorRateLimitEnabled: 1
301+
ErrorRateLimitConfig:
302+
Interval: 1
303+
Refill: 50
304+
MaxTokens: 200
307305
ManualUpdate: 0
308306
--- !u!114 &249891961
309307
MonoBehaviour:

Assets/Mirage/Samples~/Basic/Scripts/BasicPlayer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ private void UpdateData()
6363
public void OnStartClient()
6464
{
6565
// Get spawner so we can set the parent under the canvas
66-
var spawner = Client.GetComponent<CanvasCharacterSpawner>();
66+
var spawner = Client.GetComponent<BasicPlayerSpawner>();
6767
transform.SetParent(spawner.Parent);
6868

6969
var size = rectTransform.sizeDelta + padding;
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
using UnityEngine;
2+
3+
namespace Mirage.Examples.Basic
4+
{
5+
/// <summary>
6+
/// A lightweight character spawner for single-scene setups.
7+
/// Spawns and configures a player UI object as soon as the client authenticates with the server.
8+
/// </summary>
9+
public class BasicPlayerSpawner : MonoBehaviour
10+
{
11+
[Header("References")]
12+
public NetworkServer Server;
13+
public ServerObjectManager ServerObjectManager;
14+
15+
[Header("Character Spawning")]
16+
public NetworkIdentity PlayerPrefab;
17+
public Transform Parent;
18+
19+
// counter to give each player their own id
20+
private int _playerCounter;
21+
22+
private void Awake()
23+
{
24+
Server.Started.AddListener(OnServerStarted);
25+
Server.Authenticated.AddListener(OnServerAuthenticated);
26+
}
27+
28+
private void OnServerStarted()
29+
{
30+
// Reset player numbering when the server starts so player IDs begin at 1 for each session
31+
_playerCounter = 0;
32+
}
33+
34+
private void OnServerAuthenticated(INetworkPlayer player)
35+
{
36+
// In a single-scene example without scene transitions, the client is immediately ready on authentication
37+
player.SceneIsReady = true;
38+
39+
SpawnCharacterForPlayer(player);
40+
}
41+
42+
private void SpawnCharacterForPlayer(INetworkPlayer player)
43+
{
44+
Debug.Assert(player.Identity == null, "Player already has a character spawned.");
45+
46+
// Instantiate under the UI parent canvas so the element appears in the layout hierarchy
47+
var character = Instantiate(PlayerPrefab, Parent);
48+
49+
// Set initial SyncVars prior to AddCharacter so they are packed into the initial SpawnMessage
50+
var basicPlayer = character.GetComponent<BasicPlayer>();
51+
// increment first, so first player has id=1
52+
_playerCounter++;
53+
basicPlayer.playerNo = _playerCounter;
54+
55+
// AddCharacter spawns the object on clients and assigns authority to the connection
56+
ServerObjectManager.AddCharacter(player, character.gameObject);
57+
}
58+
}
59+
}

Assets/Mirage/Samples~/Basic/Scripts/CanvasCharacterSpawner.cs.meta renamed to Assets/Mirage/Samples~/Basic/Scripts/BasicPlayerSpawner.cs.meta

File renamed without changes.

Assets/Mirage/Samples~/Basic/Scripts/CanvasCharacterSpawner.cs

Lines changed: 0 additions & 149 deletions
This file was deleted.

0 commit comments

Comments
 (0)