Synchronize UI gamebject over the network

Hi,
I have a photon voice chat demo and I want to synchronize a gameobject that is not a player (for example a Text element with variable string text). How to do that?
Use PhotonView is a valid option? some step by step instruction please?

Comments

  • Hi @StyleMaster,

    if you have a low frequent updated text object, you may consider using the Custom Room Properties because this might give you the least overhead. For a high frequent updated object, a PhotonView with observed components might be the better choice. You can use OnPhotonSerializeView(...) to send data across the network. A tutorial how to use OnPhotonSerializeView is available in Part 7 of the PUN Basics Tutorial.

    However I have read your other topic and I guess you still want to implement a timer, don't you? Therefore OnPhotonserializeView isn't the best way to go because the receiving client will have delay and his timer will be inaccurate. Of course there are ways to handle the delay but there is maybe an easier possibility. Therefore you can add the current PhotonNetwork.time value to the custom room properties whenever you start a new round. When updating the custom room properties, OnPhotonPlayerPropertiesChanged(object[] playerAndUpdatedProps) will be called. This allows each client to store the synchronized start time. Using PhotonNetwork.time again, every client is now able calculate the elapsed round time on his own by using PhotonNetwork.time - startTimeFromProps; and is furthermore able to calculate the round timer afterwards.
  • Hi,
    Yes I want to implement a Timer, but also other UI elements during the game.
    I´ve tried to:
    1) create a prefab with a text gameobject inside with a script that decrease the value of the timer
    2) add the prefab to the scene (drag and drop)
    3) add a PhotonView component and put the script above as observed component
    But still when I am in the room and another user join the room, the other user don´t see my timer.
    I want that if the user join the room when my timer is at 3 seconds, he see on the timer 3 seconds. Instead, right now, when he join the room the timer start from zero.
    What is missing?
    Other ways to to that?
  • An other example: One user see a video stream on a texture and the other user in the same room not. I want that both see that video.
  • I just noticed that it has to be OnPhotonCustomRoomPropertiesChanged(Hashtable propertiesThatChanged) instead of OnPhotonPlayerPropertiesChanged(...). Sorry for the mistake. When using this callback you don't have to add this script as a observed component by a PhotonView component.
    using UnityEngine;
    using UnityEngine.UI;
    using Hashtable = ExitGames.Client.Photon.Hashtable;
    
    public class Timer : MonoBehaviour
    {
        public Text TextComponent;
    
        private bool hasStartTime;
        private int startTime;
    
        public void Update()
        {
            if (!hasStartTime)
            {
                return;
            }
    
            TextComponent.text = ((PhotonNetwork.ServerTimestamp - startTime) / 1000.0f) + " seconds";
        }
    
        public void OnJoinedRoom()
        {
            if (!PhotonNetwork.isMasterClient)
            {
                return;
            }
    
            Hashtable ht = new Hashtable();
            ht.Add("StartTime", PhotonNetwork.ServerTimestamp);
    
            PhotonNetwork.room.SetCustomProperties(ht);
        }
    
        public void OnPhotonCustomRoomPropertiesChanged(Hashtable propertiesThatChanged)
        {
            if (propertiesThatChanged.ContainsKey("StartTime"))
            {
                hasStartTime = true;
    
                startTime = (int) propertiesThatChanged["StartTime"];
            }
        }
    }
    Note: the ServerTimeStamp can be a negative value as far as I know so you need to handle this yourself.

    An other example: One user see a video stream on a texture and the other user in the same room not. I want that both see that video.


    I would suggest using a RPC call for this. If one of the clients start watching the movie, he sends a RPC in order to start playback on all clients.
  • Hey @StyleMaster ! Did you fix this problem? I need to sync and instantiate some prefabs in canvas. The answer to your question coudl help me a lot.

    Cheers,

    dflozano