Scene Object not synchronized
Options
Hi,
I have a 3D Text of a timer over the network, visible by all users in the room, but not synchronized. I want that all users that join the room later will see the timer already started (if they join at 05:00, they have to see the timer starting from 05:00 and not from 00:00) How to solve this?
The script Observed by the photon view component of the prefab is the following:
[RequireComponent(typeof(PhotonView))]
public class countdownTimer : Photon.MonoBehaviour
{
private float startTime;
private string textTime;
private float guiTime;
private int minutes;
private int seconds;
private int fraction;
[SerializeField]
public TextMesh textField;
void Start () {
startTime = Time.time;
}
void Update () {
guiTime = Time.time - startTime;
minutes = (int) guiTime / 60; //Divide the guiTime by sixty to get the minutes.
seconds = (int) guiTime % 60;//Use the euclidean division for the seconds.
fraction = (int) (guiTime * 100) % 100;
textTime = string.Format("{0:00}:{1:00}", minutes, seconds, fraction);
textField.text = textTime;
}
void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
{
if (stream.isWriting)
{
stream.SendNext(textField);
}
else
{
textTime = (string)stream.ReceiveNext();
}
}
}
I have a 3D Text of a timer over the network, visible by all users in the room, but not synchronized. I want that all users that join the room later will see the timer already started (if they join at 05:00, they have to see the timer starting from 05:00 and not from 00:00) How to solve this?
The script Observed by the photon view component of the prefab is the following:
[RequireComponent(typeof(PhotonView))]
public class countdownTimer : Photon.MonoBehaviour
{
private float startTime;
private string textTime;
private float guiTime;
private int minutes;
private int seconds;
private int fraction;
[SerializeField]
public TextMesh textField;
void Start () {
startTime = Time.time;
}
void Update () {
guiTime = Time.time - startTime;
minutes = (int) guiTime / 60; //Divide the guiTime by sixty to get the minutes.
seconds = (int) guiTime % 60;//Use the euclidean division for the seconds.
fraction = (int) (guiTime * 100) % 100;
textTime = string.Format("{0:00}:{1:00}", minutes, seconds, fraction);
textField.text = textTime;
}
void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
{
if (stream.isWriting)
{
stream.SendNext(textField);
}
else
{
textTime = (string)stream.ReceiveNext();
}
}
}
0
Comments
-
stream.SendNext(textField.text);0
-
Thanks, now it works. I also made a workaround in the Update function. Now, if a third user join the room later, the timer is updated with the same value of the other user already present in the room.
Updated code:
[RequireComponent(typeof(PhotonView))]
public class countdownTimer : Photon.MonoBehaviour
{
private float startTime;
[SerializeField]
private string textTime;
private float guiTime;
private int minutes;
private int seconds;
private int fraction;
public TextMesh textField;
void Start () {
startTime = Time.time;
}
void Update () {
if (PhotonNetwork.isMasterClient)
{
guiTime = Time.time - startTime;
minutes = (int)guiTime / 60; //Divide the guiTime by sixty to get the minutes.
seconds = (int)guiTime % 60;//Use the euclidean division for the seconds.
fraction = (int)(guiTime * 100) % 100;
textTime = string.Format("{0:00}:{1:00}", minutes, seconds, fraction);
textField.text = textTime;
}
else {
textField.text = textTime;
}
}
void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
{
if (stream.isWriting)
{
stream.SendNext(textTime);
}
else
{
textTime = (string)stream.ReceiveNext();
}
}
}0