简体   繁体   中英

Copy object data between clients using Photon Unity Networking

I have a class that is structured as followed:

public class Game : MonoBehaviour 
{
    public string Name { get; set;} 
    public int NumberOfPlayers { get; set; }
    public int CurrentPlayers { get; set; }

    public Game(string name, int num)
    {
            Name = name;
            NumberOfPlayers = num;
            CurrentPlayers = 0;
    }

I create a global variable when the game starts using

public static Game firstGame;

Using Photon Unity Networking, how can I share this single object's info between all clients I create?

您可以创建一个 RPC 来发送游戏类的 3 个变量,然后在接收客户端上,他们根据发送的数据创建一个 Game 对象。

You can create an RPC call over other players, putting these values inside a dictionary object and then pass that object in RPC call. And on another side, you can unwrap this object.

Eg:

Dictionary<string, string> dataToShare = new Dictionary<string, string>();
dataToShare.Add("PlayerFirstName", PlayerInformation.loginDetails.FirstName);
dataToShare.Add("Avatar", PlayerInformation.loginDetails.Avatar);
photonView.RPC("InitializeMyPlayerRPC",PhotonTargets.OthersBuffered,new object[]{ dataToShare});

[PunRPC]

void InitializeMyPlayerRPC(object dataToShare)
{
    Dictionary<string, string> data = dataToShare as Dictionary<string, string>;          
    player.Name = data["PlayerFirstName"].ToString();
    player.Avatat = data["Avatar"].ToString();
}

I know you have already done, but if someone needs, it will help them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM