各プレーヤーの名前を表示する(Photonのカスタムプロパティの利用)


プレーヤー名の表示
using UnityEngine;
// ★追加
using Photon.Pun;
using ExitGames.Client.Photon; // Hashtable用
using TMPro;
public class ScoreManager : MonoBehaviourPunCallbacks // ★変更
{
public TextMeshProUGUI playerLabel_1;
public TextMeshProUGUI playerLabel_2;
// プロパティ用のキーを定数(書き換えのできない変数)にしておく(テクニック)
private const string P1_NAME = "p1n";
private const string P2_NAME = "p2n";
private const string P1_SCORE = "p1s";
private const string P2_SCORE = "p2s";
void Start()
{
// 自分がマスター(最初に入室した者)ならばプロパティを初期化する
if (PhotonNetwork.IsMasterClient)
{
InitRoomProperties();
}
else
{
// 2人目として入室した場合は自分の名前を登録
ResisterPlayer2();
}
RefreshUI();
}
void InitRoomProperties()
{
Hashtable props = new Hashtable
{
{P1_NAME, PhotonNetwork.NickName},
{P1_SCORE,0},
{P2_NAME,"Waiting..."},
{P2_SCORE,0}
};
PhotonNetwork.CurrentRoom.SetCustomProperties(props);
}
void ResisterPlayer2()
{
Hashtable props = new Hashtable { { P2_NAME, PhotonNetwork.NickName } };
PhotonNetwork.CurrentRoom.SetCustomProperties(props);
}
// ルームプロパティが更新されたら自動で呼ばれる
public override void OnRoomPropertiesUpdate(Hashtable propertiesThatChanged)
{
base.OnRoomPropertiesUpdate(propertiesThatChanged);
RefreshUI();
}
void RefreshUI()
{
if (PhotonNetwork.CurrentRoom == null)
{
return;
}
var props = PhotonNetwork.CurrentRoom.CustomProperties;
// Player1の表示更新
if (props.ContainsKey(P1_NAME) && props.ContainsKey(P1_SCORE))
{
playerLabel_1.text = props[P1_NAME].ToString() + ": " + ((int)props[P1_SCORE]).ToString("D4");
}
// Player2の表示更新
if (props.ContainsKey(P2_NAME) && props.ContainsKey(P2_SCORE))
{
playerLabel_2.text = props[P2_NAME].ToString() + ": " + ((int)props[P2_SCORE]).ToString("D4");
}
}
}

【Unity6版】BattleOnline(全38回)
他のコースを見る

プレーヤー名の表示
using UnityEngine;
// ★追加
using Photon.Pun;
using ExitGames.Client.Photon; // Hashtable用
using TMPro;
public class ScoreManager : MonoBehaviourPunCallbacks // ★変更
{
public TextMeshProUGUI playerLabel_1;
public TextMeshProUGUI playerLabel_2;
// プロパティ用のキーを定数(書き換えのできない変数)にしておく(テクニック)
private const string P1_NAME = "p1n";
private const string P2_NAME = "p2n";
private const string P1_SCORE = "p1s";
private const string P2_SCORE = "p2s";
void Start()
{
// 自分がマスター(最初に入室した者)ならばプロパティを初期化する
if (PhotonNetwork.IsMasterClient)
{
InitRoomProperties();
}
else
{
// 2人目として入室した場合は自分の名前を登録
ResisterPlayer2();
}
RefreshUI();
}
void InitRoomProperties()
{
Hashtable props = new Hashtable
{
{P1_NAME, PhotonNetwork.NickName},
{P1_SCORE,0},
{P2_NAME,"Waiting..."},
{P2_SCORE,0}
};
PhotonNetwork.CurrentRoom.SetCustomProperties(props);
}
void ResisterPlayer2()
{
Hashtable props = new Hashtable { { P2_NAME, PhotonNetwork.NickName } };
PhotonNetwork.CurrentRoom.SetCustomProperties(props);
}
// ルームプロパティが更新されたら自動で呼ばれる
public override void OnRoomPropertiesUpdate(Hashtable propertiesThatChanged)
{
base.OnRoomPropertiesUpdate(propertiesThatChanged);
RefreshUI();
}
void RefreshUI()
{
if (PhotonNetwork.CurrentRoom == null)
{
return;
}
var props = PhotonNetwork.CurrentRoom.CustomProperties;
// Player1の表示更新
if (props.ContainsKey(P1_NAME) && props.ContainsKey(P1_SCORE))
{
playerLabel_1.text = props[P1_NAME].ToString() + ": " + ((int)props[P1_SCORE]).ToString("D4");
}
// Player2の表示更新
if (props.ContainsKey(P2_NAME) && props.ContainsKey(P2_SCORE))
{
playerLabel_2.text = props[P2_NAME].ToString() + ": " + ((int)props[P2_SCORE]).ToString("D4");
}
}
}

各プレーヤーの名前を表示する(Photonのカスタムプロパティの利用)