66using ExitGames . Client . Photon ;
77using System ;
88using UnityEngine . Events ;
9+ using Photon . Pun . UtilityScripts ;
910
1011namespace CoReality . Avatars
1112{
@@ -138,6 +139,9 @@ void Awake()
138139 {
139140 Instance = this ;
140141
142+ //Create PlayerNumbering gameobject
143+ new GameObject ( "PlayerNumberingUtil" ) . AddComponent < Photon . Pun . UtilityScripts . PlayerNumbering > ( ) ;
144+
141145 //Check for network module (Ensure this is after NetworkModule in Script Execution Order)
142146 if ( ! NetworkModule . Instance )
143147 throw new System . Exception ( "NetworkModule not present in scene? Can't start AvatarModule without it." ) ;
@@ -180,6 +184,13 @@ private void SpawnAvatar(bool remote, int viewID = -1)
180184 ) ;
181185 _onAvatarCreated ? . Invoke ( avatar ) ;
182186 PopulateAvatarList ( ) ;
187+
188+ //Set color
189+ _localAvatar . Name = _randomAdjectives [ UnityEngine . Random . Range ( 0 , _randomAdjectives . Count ) ] + " "
190+ + _randomNouns [ UnityEngine . Random . Range ( 0 , _randomNouns . Count ) ] ;
191+
192+ //Add listener to player number change
193+ PlayerNumbering . OnPlayerNumberingChanged += PlayerColorChanged ;
183194 }
184195 else
185196 {
@@ -199,17 +210,17 @@ private void SpawnAvatar(bool remote, int viewID = -1)
199210 //Forward property change event
200211 avatar . OnPropertyChanged . AddListener ( ( prop , val ) => { _onAvatarPropertyChanged ? . Invoke ( avatar , prop , val ) ; } ) ;
201212
202- //Set color
203- _localAvatar . Name = _randomAdjectives [ UnityEngine . Random . Range ( 0 , _randomAdjectives . Count ) ] + " "
204- + _randomNouns [ UnityEngine . Random . Range ( 0 , _randomNouns . Count ) ] ;
205-
206- //Set the avatar's color to the actor number
207- int actorNum = PhotonNetwork . LocalPlayer . ActorNumber ;
208- if ( actorNum >= 0 ) //Ensure not -1
209- _localAvatar . Color = _randomColors [ PhotonNetwork . LocalPlayer . ActorNumber ] ;
213+ }
210214
215+ private void PlayerColorChanged ( )
216+ {
217+ //Set color
218+ _localAvatar . Color = _randomColors [ PhotonNetwork . LocalPlayer . GetPlayerNumber ( ) ] ;
219+ //Remove listener
220+ PlayerNumbering . OnPlayerNumberingChanged -= PlayerColorChanged ;
211221 }
212222
223+
213224 /// <summary>
214225 /// Populates the avatar list with the current avatars [0] is always local
215226 /// </summary>
0 commit comments