Skip to content

Commit

Permalink
Fixed player color setting to use OnPlayerNumberingChanged to correct…
Browse files Browse the repository at this point in the history
…ly get the players number when it is set
  • Loading branch information
jemmec committed Mar 15, 2022
1 parent 763abdb commit fd55651
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
27 changes: 19 additions & 8 deletions Assets/CoReality/Scripts/AvatarSystem/AvatarModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using ExitGames.Client.Photon;
using System;
using UnityEngine.Events;
using Photon.Pun.UtilityScripts;

namespace CoReality.Avatars
{
Expand Down Expand Up @@ -138,6 +139,9 @@ void Awake()
{
Instance = this;

//Create PlayerNumbering gameobject
new GameObject("PlayerNumberingUtil").AddComponent<Photon.Pun.UtilityScripts.PlayerNumbering>();

//Check for network module (Ensure this is after NetworkModule in Script Execution Order)
if (!NetworkModule.Instance)
throw new System.Exception("NetworkModule not present in scene? Can't start AvatarModule without it.");
Expand Down Expand Up @@ -180,6 +184,13 @@ private void SpawnAvatar(bool remote, int viewID = -1)
);
_onAvatarCreated?.Invoke(avatar);
PopulateAvatarList();

//Set color
_localAvatar.Name = _randomAdjectives[UnityEngine.Random.Range(0, _randomAdjectives.Count)] + " "
+ _randomNouns[UnityEngine.Random.Range(0, _randomNouns.Count)];

//Add listener to player number change
PlayerNumbering.OnPlayerNumberingChanged += PlayerColorChanged;
}
else
{
Expand All @@ -199,17 +210,17 @@ private void SpawnAvatar(bool remote, int viewID = -1)
//Forward property change event
avatar.OnPropertyChanged.AddListener((prop, val) => { _onAvatarPropertyChanged?.Invoke(avatar, prop, val); });

//Set color
_localAvatar.Name = _randomAdjectives[UnityEngine.Random.Range(0, _randomAdjectives.Count)] + " "
+ _randomNouns[UnityEngine.Random.Range(0, _randomNouns.Count)];

//Set the avatar's color to the actor number
int actorNum = PhotonNetwork.LocalPlayer.ActorNumber;
if (actorNum >= 0) //Ensure not -1
_localAvatar.Color = _randomColors[PhotonNetwork.LocalPlayer.ActorNumber];
}

private void PlayerColorChanged()
{
//Set color
_localAvatar.Color = _randomColors[PhotonNetwork.LocalPlayer.GetPlayerNumber()];
//Remove listener
PlayerNumbering.OnPlayerNumberingChanged -= PlayerColorChanged;
}


/// <summary>
/// Populates the avatar list with the current avatars [0] is always local
/// </summary>
Expand Down
1 change: 1 addition & 0 deletions Assets/CoReality/Scripts/AvatarSystem/HoloAvatar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public string Name
}
}

[SerializeField]
private Color _color;

/// <summary>
Expand Down

0 comments on commit fd55651

Please sign in to comment.