9
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【PUN】全クライアントでカウントダウンを共有する

Posted at

PhotonNetwork.time
ルームが作成されてからのサーバーの時間で、
ルーム内の全クライアントに同じ値を返す。

最初に入室した人(ここではマスタークライアントという前提)の時間をStartTime という名前でルームのプロパティに設定。プレイヤーがルームに入室したらCountdown.csをアタッチしたカウントダウン用のゲームオブジェクトを有効にする。

PlayerManager.cs(一部抜粋)
public GameObject countdown;

public override void OnJoinedRoom() {
    if (PhotonNetwork.isMasterClient) {
        // 全体カウントダウンのスタート時間をセット
        var properties = new ExitGames.Client.Photon.Hashtable();
        properties.Add ("StartTime", PhotonNetwork.time);
        PhotonNetwork.room.SetCustomProperties (properties);
    }
    countdown.SetActive (true);
}

ルーム作成からの経過時間を計算して、maxCount から残りの時間を計算する。

Countdown.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Countdown : MonoBehaviour {

	[SerializeField] float maxCount;

	double startTime;
	float leftTime;

	void Start () {
		startTime = (double)PhotonNetwork.room.CustomProperties ["StartTime"];
	}

	void Update () {
		double elapsedTime = PhotonNetwork.time - startTime;
		leftTime = maxCount - (float)elapsedTime;
	}
}
9
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?