In my game I need to create a timer countdown that synchronizes for all players, this Timer need to be on DontDestroyOnLoad (support scene changes) because in my game there are a lot of these scene changes. Do you know how can I do this? PS: I'm using Photon's PUN2, So, almost nothing of PUN1 will work.
The easiest way to implement a timer in a multiplayer game using Photon is to apply this script on your UI (that contains a Timer).
bool startTimer = false;
double timerIncrementValue;
double startTime;
[SerializeField] double timer = 20;
ExitGames.Client.Photon.Hashtable CustomeValue;
void Start()
{
if (PhotonNetwork.player.IsMasterClient)
{
CustomeValue = new ExitGames.Client.Photon.Hashtable();
startTime = PhotonNetwork.time;
startTimer = true;
CustomeValue.Add("StartTime", startTime);
PhotonNetwork.room.SetCustomProperties(CustomeValue);
}
else
{
startTime = double.Parse(PhotonNetwork.room.CustomProperties["StartTime"].ToString());
startTimer = true;
}
}
void Update()
{
if (!startTimer) return;
timerIncrementValue = PhotonNetwork.time - startTime;
if (timerIncrementValue >= timer)
{
//Timer Completed
//Do What Ever You What to Do Here
}
}