Unity 3d PhotonEngine 其他玩家如何获取masterclient生成的数据

Posted

技术标签:

【中文标题】Unity 3d PhotonEngine 其他玩家如何获取masterclient生成的数据【英文标题】:Unity 3d PhotonEngine How other player can get data generated by masterclient 【发布时间】:2021-07-29 19:35:45 【问题描述】:

您好,我需要有关使用光子的在线 2 人游戏的帮助。每个玩家都会收到一个生成的票号,玩家 1 将客串 player2 的票号,而 player2 将客串 player1 的票号。

这是主客户端生成票证的脚本。当玩家 2 加入房间时,他将从玩家 1(主客户端)生成的票中获得票。我创建了一个附有照片视图的游戏对象和此处的脚本

void Start()

    if (PhotonNetwork.IsMasterClient)
    
        player1ticket = UnityEngine.Random.Range(1000, 5000);
        player2ticket = UnityEngine.Random.Range(1000, 5000);
    
    getTicket = true;


// Update is called once per frame
void Update()

    if (getTicket)
    
        if (PhotonNetwork.IsMasterClient)
        
            myTicket = player1ticket;
        
        else
        
            myTicket = player2ticket;
        
        getTicket = false;
    

但是玩游戏的时候玩家2没有票号

【问题讨论】:

【参考方案1】:

首先,您知道两者可能会获得相同的票号 - 不太可能但有可能。在这种情况下,您可能需要重复第二次随机

player1ticket = UnityEngine.Random.Range(1000, 5000);
do

    player2ticket = UnityEngine.Random.Range(1000, 5000); 
 
while(player2ticket == player1ticket);   

那么你只在主服务器上生成值。所以客户端上的getTicket 永远不是true

即使客户端也没有player2ticket 值。

也没有必要/感觉在更新中的每一帧都分配它们。


我宁愿使用 自定义房间属性 并在主服务器上使用 Room.SetCustomProperties,这将在所有客户端上触发 OnRoomPropertiesUpdate(Hashtable。此呼叫也会在客户加入房间后触发一次,因此您可以确保新连接的客户不会错过它。

void Start()

    if (PhotonNetwork.IsMasterClient)
    
        player1ticket = UnityEngine.Random.Range(1000, 5000);
        player2ticket = UnityEngine.Random.Range(1000, 5000);

        // You are master so already get your ticket
        myTicket = player1ticket;

        // Then simply push the other ticket(s) into the room properties
        Hashtable properties = PhotonNetwork.room.customProperties; 
        properties["ticket2"] =  player2ticket);  

        // This will store the value in the current room
        // and triggers OnRoomPropertiesUpdate on all already connected clients
        PhotonNetwork.room.SetCustomProperties(properties);
    


// This will be called
// - as soon as you entered a room
// - whenever someone writes into the room properties
public override void OnRoomPropertiesUpdate(Hashtable propertiesThatChanged)

    // Not sure about the exact API right now, it's not in their docs
    // it's either ContainsKey, HasKey or something similar ..maybe even TryGetValue works
    if(!propertiesThatChanged.ContainsKey("ticket2")) return;

    if(!PhotonNetwork.IsMasterClient)
    
        myTicket = (int) PhotonNetwork.room.customProperties["ticket2"];
    

【讨论】:

非常感谢,但房间 Hashtable properties = PhotonNetwork.room.customProperties;和 OnRoomPropertiesUpdate @user3840726 什么错误?这可能是因为您没有 Hashtable 的相应 using ExitGames.Client.Photon; (afaik 这应该是命名空间)@ 我有它,但我不能通过 PhotonNetwork.room 使用 CustomProperties,但是当我通过 CurrentRoom 换房间时没有任何错误

以上是关于Unity 3d PhotonEngine 其他玩家如何获取masterclient生成的数据的主要内容,如果未能解决你的问题,请参考以下文章

安卓手机如何打开.unity3d文件

电脑火狐浏览器屏蔽了 unity插件导致我不能玩网页3d游戏。它说不安全就禁用了启用是灰色的

Unity3D 超多体位教你玩转UnityEvent

玩Unity3D游戏时,人物自动向左走(不是键盘问题),插件我也重新安装了,但是还不行,这是怎么回事?

2019年unity3d从入门到精通必看

Unity 3D CS0246 问题 [关闭]