リザルトシーンの作成(シーン遷移とスコア表示)


リザルトシーンにスコアを表示する
using UnityEngine;
// ★追加
using Photon.Pun;
using TMPro;
public class ResultManager : MonoBehaviour
{
public TextMeshProUGUI playerlabel_1;
public TextMeshProUGUI playerLabel_2;
// ScoreManagerと共通のキー定義
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()
{
ShowResult();
}
void ShowResult()
{
// カスタムプロパティから最終スコアを取得
var props = PhotonNetwork.CurrentRoom.CustomProperties;
// Player1の結果表示
if (props.ContainsKey(P1_NAME) && props.ContainsKey(P1_SCORE))
{
string p1Name = props[P1_NAME].ToString();
int p1Score = (int)props[P1_SCORE];
playerlabel_1.text = p1Name + ": " + p1Score.ToString("D4");
}
// Player2の結果表示
if (props.ContainsKey(P2_NAME) && props.ContainsKey(P2_SCORE))
{
string p2Name = props[P2_NAME].ToString();
int p2Score = (int)props[P2_SCORE];
playerLabel_2.text = p2Name + ": " + p2Score.ToString("D4");
}
}
}


リザルトシーンに遷移する
using UnityEngine;
using TMPro;
using Photon.Pun;
using ExitGames.Client.Photon;
public class TimeManager : MonoBehaviourPunCallbacks
{
public TextMeshProUGUI timeLabel;
public float initialTime = 100f;
private float remainingTime;
// ★追加
private bool isGameFinished = false;
void Start()
{
remainingTime = initialTime;
if (PhotonNetwork.IsMasterClient)
{
UpdateRoomTimeProrerty(initialTime);
}
}
void Update()
{
if (PhotonNetwork.IsMasterClient)
{
// ★追加
if (isGameFinished)
{
return;
}
remainingTime -= Time.deltaTime;
// ★追加
if (remainingTime <= 0)
{
remainingTime = 0;
isGameFinished = true;
// 全員をリザルトシーンに遷移させる
// マスターが移動すると全員が移動する
PhotonNetwork.LoadLevel("Result");
}
UpdateRoomTimeProrerty(remainingTime);
}
}
public override void OnRoomPropertiesUpdate(Hashtable propertiesThatChanged)
{
base.OnRoomPropertiesUpdate(propertiesThatChanged);
if (propertiesThatChanged.ContainsKey("RemainingTime"))
{
float networkTime = (float)propertiesThatChanged["RemainingTime"];
DisplayTime(networkTime);
}
}
void UpdateRoomTimeProrerty(float time)
{
Hashtable props = new Hashtable { { "RemainingTime", time } };
PhotonNetwork.CurrentRoom.SetCustomProperties(props);
}
void DisplayTime(float timeToDisplay)
{
int minutes = Mathf.FloorToInt(timeToDisplay / 60);
int seconds = Mathf.FloorToInt(timeToDisplay % 60);
timeLabel.text = string.Format("Time {0:0}:{1:00}", minutes, seconds);
}
}

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

リザルトシーンにスコアを表示する
using UnityEngine;
// ★追加
using Photon.Pun;
using TMPro;
public class ResultManager : MonoBehaviour
{
public TextMeshProUGUI playerlabel_1;
public TextMeshProUGUI playerLabel_2;
// ScoreManagerと共通のキー定義
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()
{
ShowResult();
}
void ShowResult()
{
// カスタムプロパティから最終スコアを取得
var props = PhotonNetwork.CurrentRoom.CustomProperties;
// Player1の結果表示
if (props.ContainsKey(P1_NAME) && props.ContainsKey(P1_SCORE))
{
string p1Name = props[P1_NAME].ToString();
int p1Score = (int)props[P1_SCORE];
playerlabel_1.text = p1Name + ": " + p1Score.ToString("D4");
}
// Player2の結果表示
if (props.ContainsKey(P2_NAME) && props.ContainsKey(P2_SCORE))
{
string p2Name = props[P2_NAME].ToString();
int p2Score = (int)props[P2_SCORE];
playerLabel_2.text = p2Name + ": " + p2Score.ToString("D4");
}
}
}


リザルトシーンに遷移する
using UnityEngine;
using TMPro;
using Photon.Pun;
using ExitGames.Client.Photon;
public class TimeManager : MonoBehaviourPunCallbacks
{
public TextMeshProUGUI timeLabel;
public float initialTime = 100f;
private float remainingTime;
// ★追加
private bool isGameFinished = false;
void Start()
{
remainingTime = initialTime;
if (PhotonNetwork.IsMasterClient)
{
UpdateRoomTimeProrerty(initialTime);
}
}
void Update()
{
if (PhotonNetwork.IsMasterClient)
{
// ★追加
if (isGameFinished)
{
return;
}
remainingTime -= Time.deltaTime;
// ★追加
if (remainingTime <= 0)
{
remainingTime = 0;
isGameFinished = true;
// 全員をリザルトシーンに遷移させる
// マスターが移動すると全員が移動する
PhotonNetwork.LoadLevel("Result");
}
UpdateRoomTimeProrerty(remainingTime);
}
}
public override void OnRoomPropertiesUpdate(Hashtable propertiesThatChanged)
{
base.OnRoomPropertiesUpdate(propertiesThatChanged);
if (propertiesThatChanged.ContainsKey("RemainingTime"))
{
float networkTime = (float)propertiesThatChanged["RemainingTime"];
DisplayTime(networkTime);
}
}
void UpdateRoomTimeProrerty(float time)
{
Hashtable props = new Hashtable { { "RemainingTime", time } };
PhotonNetwork.CurrentRoom.SetCustomProperties(props);
}
void DisplayTime(float timeToDisplay)
{
int minutes = Mathf.FloorToInt(timeToDisplay / 60);
int seconds = Mathf.FloorToInt(timeToDisplay % 60);
timeLabel.text = string.Format("Time {0:0}:{1:00}", minutes, seconds);
}
}

リザルトシーンの作成(シーン遷移とスコア表示)