简体   繁体   中英

Countdown Timer in Unity with Photon's PUN 2

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.

Simplest Way to Implement Timer on Multiplayer Game Using Photon. Apply this Script on the UI with 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
     }
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM