オンラインゲームをPUNを使いながら作っています。画面上にタイマーを表示させたく、CanvasにtextUIを設置し、そのUIに以下のスクリプトを張りました。それをobserved conponentにアタッチしました
c#
1using UnityEngine; 2using UnityEngine.UI; 3using UnityEngine.EventSystems; 4using System.Collections; 5using System; 6 7public class TimeScript : Photon.PunBehaviour 8{ 9 private float time = 100; 10 11 void Start() 12 { 13 //float型からint型へCastし、String型に変換して表示 14 GetComponent<Text>().text = ((int)time).ToString(); 15 } 16 17 void Update() 18 { 19 //1秒に1ずつ減らしていく 20 time -= Time.deltaTime; 21 //マイナスは表示しない 22 if (time < 0) time = 0; 23 GetComponent<Text>().text = ((int)time).ToString(); 24 } 25 26 public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info) 27 { 28 if (stream.isWriting) 29 { 30 stream.SendNext(this.time); 31 } 32 else 33 { 34 this.time = (int)stream.ReceiveNext(); 35 } 36 } 37 38}
ところが、time変数は同期されず、それぞれの画面で100秒から始まってしまいます。
同期方法をご存知の方御教授願います。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。