_EagleOwle_

PhotonTimer

Feb 19th, 2020
73
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using Photon.Pun;
  2. using Photon.Realtime;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using UnityEngine;
  6. using Hashtable = ExitGames.Client.Photon.Hashtable;
  7.  
  8. public class PhotonTimer : MonoBehaviour
  9. {
  10.     #region SINGLETON
  11.     public static PhotonTimer Singleton
  12.     {
  13.         get
  14.         {
  15.             return GameObject.FindObjectOfType<PhotonTimer>();
  16.         }
  17.     }
  18.     #endregion
  19.  
  20.     public bool startTimer = false;
  21.  
  22.     [Space(10)]
  23.     public double milliseconds;
  24.     public int seconds;
  25.     public int minutes;
  26.  
  27.     [Space(10)]
  28.     double startTime;
  29.     double timerIncrementValue;
  30.     [SerializeField] double timer = 20;
  31.     ExitGames.Client.Photon.Hashtable CustomeValue;
  32.  
  33.     void OnEnable()
  34.     {
  35.         if (!PhotonNetwork.IsConnected)
  36.             return;
  37.  
  38.         if (PhotonNetwork.IsMasterClient)
  39.         {
  40.             CustomeValue = new ExitGames.Client.Photon.Hashtable();
  41.             startTime = PhotonNetwork.Time;
  42.             startTimer = true;
  43.             CustomeValue.Add("StartTime", startTime);
  44.             PhotonNetwork.CurrentRoom.SetCustomProperties(CustomeValue);
  45.         }
  46.         else
  47.         {
  48.             startTime = double.Parse(PhotonNetwork.CurrentRoom.CustomProperties["StartTime"].ToString());
  49.             startTimer = true;
  50.         }
  51.     }
  52.  
  53.     void Update()
  54.     {
  55.         if (!startTimer)
  56.             return;
  57.  
  58.         if (PhotonNetwork.IsConnected)
  59.         {
  60.             timerIncrementValue = PhotonNetwork.Time - startTime;
  61.            
  62.             milliseconds  = timerIncrementValue % 1;
  63.             seconds = (int)timerIncrementValue % 60;
  64.             minutes = (int)timerIncrementValue / 60;
  65.  
  66.  
  67.             if (timerIncrementValue >= timer)
  68.             {
  69.                 //Timer Completed
  70.                 //Do What Ever You What to Do Here
  71.             }
  72.         }
  73.     }
  74. }
RAW Paste Data