简体   繁体   English

结合Photon的PUN 2的倒数计时器

[英]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. 在我的游戏中,我需要创建一个为所有玩家同步的倒计时计时器,该计时器需要处于DontDestroyOnLoad(支持场景更改)上,因为在我的游戏中,这些场景更改很多。 Do you know how can I do this? 你知道我该怎么做吗? PS: I'm using Photon's PUN2, So, almost nothing of PUN1 will work. PS:我使用的是Photon的PUN2,因此,几乎所有PUN1都不起作用。

Simplest Way to Implement Timer on Multiplayer Game Using Photon. 使用光子在多人游戏上实现计时器的最简单方法。 Apply this Script on the UI with Timer 使用Timer在UI上应用此脚本

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
     }
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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