@@ -55,17 +55,21 @@ public override void OnInspectorGUI()
5555/// </summary>
5656public class NetworkManagerBootstrapper : NetworkManager
5757{
58+ #region Validation
5859#if UNITY_EDITOR
5960 // Inspector view expand/collapse settings for this derived child class
6061 [ HideInInspector ]
6162 public bool NetworkManagerBootstrapperExpanded ;
6263 protected override void OnValidateComponent ( )
6364 {
6465 m_OriginalVSyncCount = QualitySettings . vSyncCount ;
66+ m_ServicesRegistered = CloudProjectSettings . organizationName != string . Empty && CloudProjectSettings . organizationId != string . Empty ;
6567 base . OnValidateComponent ( ) ;
6668 }
6769#endif
70+ #endregion
6871
72+ #region Properties
6973 public static NetworkManagerBootstrapper Instance ;
7074
7175 public int TargetFrameRate = 100 ;
@@ -91,12 +95,17 @@ private enum ConnectionStates
9195 }
9296
9397 private ConnectionStates m_ConnectionState ;
98+
99+ [ SerializeField ]
94100 private bool m_ServicesRegistered ;
95101 private ISession m_CurrentSession ;
96102 private string m_SessionName ;
97103 private string m_ProfileName ;
98104 private Task m_SessionTask ;
99105
106+ #endregion
107+
108+ #region Initialization and Destroy
100109 public static string GetRandomString ( int length )
101110 {
102111 var r = new System . Random ( ) ;
@@ -115,8 +124,6 @@ private void Awake()
115124 SetFrameRate ( TargetFrameRate , EnableVSync ) ;
116125 SetSingleton ( ) ;
117126 m_SceneBootstrapLoader = GetComponent < SceneBootstrapLoader > ( ) ;
118-
119- m_ServicesRegistered = CloudProjectSettings . organizationName != string . Empty && CloudProjectSettings . organizationId != string . Empty ;
120127 }
121128
122129 private async void Start ( )
@@ -153,8 +160,9 @@ private void OnDestroy()
153160 OnClientDisconnectCallback -= OnClientDisconnect ;
154161 OnConnectionEvent -= OnClientConnectionEvent ;
155162 }
163+ #endregion
156164
157-
165+ #region Session and Connection Event Handling
158166 private void OnClientConnectionEvent ( NetworkManager networkManager , ConnectionEventData eventData )
159167 {
160168 LogMessage ( $ "[{ Time . realtimeSinceStartup } ] Connection event { eventData . EventType } for Client-{ eventData . ClientId } .") ;
@@ -207,6 +215,29 @@ private void SessionStopped(bool isHost)
207215 }
208216 }
209217
218+ private async Task < ISession > ConnectThroughLiveService ( )
219+ {
220+ try
221+ {
222+ var options = new SessionOptions ( )
223+ {
224+ Name = m_SessionName ,
225+ MaxPlayers = 32
226+ } . WithDistributedAuthorityNetwork ( ) ;
227+
228+ m_CurrentSession = await MultiplayerService . Instance . CreateOrJoinSessionAsync ( m_SessionName , options ) ;
229+ return m_CurrentSession ;
230+ }
231+ catch ( Exception e )
232+ {
233+ LogMessage ( $ "{ e . Message } ") ;
234+ Debug . LogException ( e ) ;
235+ }
236+ return null ;
237+ }
238+ #endregion
239+
240+ #region GUI Menu
210241 public void StartOrConnectToDistributedAuthoritySession ( )
211242 {
212243 m_SessionTask = ConnectThroughLiveService ( ) ;
@@ -333,37 +364,21 @@ private void OnGUI()
333364 }
334365 GUILayout . EndArea ( ) ;
335366 }
367+ #endregion
336368
337-
338- private async Task < ISession > ConnectThroughLiveService ( )
339- {
340- try
341- {
342- var options = new SessionOptions ( )
343- {
344- Name = m_SessionName ,
345- MaxPlayers = 32
346- } . WithDistributedAuthorityNetwork ( ) ;
347-
348- m_CurrentSession = await MultiplayerService . Instance . CreateOrJoinSessionAsync ( m_SessionName , options ) ;
349- return m_CurrentSession ;
350- }
351- catch ( Exception e )
352- {
353- LogMessage ( $ "{ e . Message } ") ;
354- Debug . LogException ( e ) ;
355- }
356- return null ;
357- }
358-
359-
369+ #region Server Camera Handling
360370 private Vector3 m_CameraOriginalPosition ;
361371 private Quaternion m_CameraOriginalRotation ;
362372 private int m_CurrentFollowPlayerIndex = - 1 ;
373+ private MoverScriptNoRigidbody m_CurrentPlayerFollowed ;
363374
364375 private void ResetMainCamera ( )
365376 {
366377 m_CurrentFollowPlayerIndex = - 1 ;
378+ SetCameraDefaults ( ) ;
379+ }
380+ private void SetCameraDefaults ( )
381+ {
367382 if ( Camera . main != null && Camera . main . transform . parent != null )
368383 {
369384 Camera . main . transform . SetParent ( null , false ) ;
@@ -372,14 +387,15 @@ private void ResetMainCamera()
372387 }
373388 }
374389
375- #region Update Methods and Properties
376-
377390 /// <summary>
378- /// General update for server-side
391+ /// Server only (i.e. not host), follow players as they move around
379392 /// </summary>
380- private void ServerSideUpdate ( )
393+ private void ServerFollowPlayerCheck ( )
381394 {
382- if ( Input . GetKeyDown ( KeyCode . P ) && ConnectedClientsIds . Count > 0 )
395+ bool leftBracket = Input . GetKeyDown ( KeyCode . LeftBracket ) ;
396+ bool rightBracket = Input . GetKeyDown ( KeyCode . RightBracket ) ;
397+
398+ if ( ( leftBracket || rightBracket ) && ConnectedClientsIds . Count > 0 )
383399 {
384400 // Capture the main camera's original position and rotation the first time the server-side
385401 // follows a player.
@@ -388,21 +404,53 @@ private void ServerSideUpdate()
388404 m_CameraOriginalPosition = Camera . main . transform . position ;
389405 m_CameraOriginalRotation = Camera . main . transform . rotation ;
390406 }
391- m_CurrentFollowPlayerIndex ++ ;
407+
408+ if ( leftBracket )
409+ {
410+ m_CurrentFollowPlayerIndex -- ;
411+ if ( m_CurrentFollowPlayerIndex < 0 )
412+ {
413+ m_CurrentFollowPlayerIndex = ConnectedClientsIds . Count - 1 ;
414+ }
415+ }
416+ else
417+ {
418+ m_CurrentFollowPlayerIndex ++ ;
419+ }
420+
392421 m_CurrentFollowPlayerIndex %= ConnectedClientsIds . Count ;
393422
394423 var playerId = ConnectedClientsIds [ m_CurrentFollowPlayerIndex ] ;
395- var playerObject = ConnectedClients [ playerId ] ;
396- Camera . main . transform . SetParent ( playerObject . PlayerObject . transform , false ) ;
424+ var playerNetworkClient = ConnectedClients [ playerId ] ;
425+ m_CurrentPlayerFollowed = playerNetworkClient . PlayerObject . GetComponent < MoverScriptNoRigidbody > ( ) ;
426+ Camera . main . transform . SetParent ( playerNetworkClient . PlayerObject . transform , false ) ;
397427 }
398428 else if ( Input . GetKeyDown ( KeyCode . Backspace ) )
399429 {
400- Camera . main . transform . SetParent ( null , false ) ;
401- Camera . main . transform . position = m_CameraOriginalPosition ;
402- Camera . main . transform . rotation = m_CameraOriginalRotation ;
430+ ClearFollowPlayer ( ) ;
431+ }
432+ }
433+ public void ClearFollowPlayer ( )
434+ {
435+ if ( m_CurrentPlayerFollowed != null )
436+ {
437+ m_CurrentPlayerFollowed = null ;
438+ SetCameraDefaults ( ) ;
403439 }
404440 }
441+ #endregion
405442
443+ #region Update Methods and Properties
444+ /// <summary>
445+ /// General update for server-side
446+ /// </summary>
447+ private void ServerSideUpdate ( )
448+ {
449+ if ( ! IsHost )
450+ {
451+ ServerFollowPlayerCheck ( ) ;
452+ }
453+ }
406454
407455 /// <summary>
408456 /// General update for client-side
@@ -441,8 +489,7 @@ private void Update()
441489 }
442490 #endregion
443491
444-
445- #region Message Logging Methods and Properties
492+ #region Message Logging
446493
447494 private List < MessageLog > m_MessageLogs = new List < MessageLog > ( ) ;
448495
0 commit comments