使用 Mirror unity 的多人游戏 UI

Multiplayer UI using Mirror unity

我是镜像包的新手,我正在尝试构建一个基本上只使用 UI 元素(按钮)玩的简单多人游戏。游戏的很大一部分是公告,例如“玩家 X 的回合”。

目前公告只出现在主机游戏中,如果公告来自属于玩家的 Networkbehaviour class 使用简单的 ClientRPC 函数很容易解决,但我想要UI 从处理 UI 元素的不同 class 函数到 运行。

实现这个的正确方法是什么? UI处理程序是否需要从任何网络继承class?会喜欢关于这个主题的一些提示。

提前致谢, 阿米特沃尔夫.

一个常见的策略是构建一个将 RPC 作为事件触发的单例网络事件管理器。

public class EventManager: NetworkBehaviour
{
    public static EventManager Instance;

    
    void Awake()
    {
        if(Instance == null)
            Instance = this;
        else
            Destroy(this);
    }

    public event Action<int> OnPlayerTurnChanged;
    
    [ClientRpc]
    public void ChangeTurn(int playerId)
    {
        OnPlayerTurnChanged?.Invoke(damage);
    }
}

然后您可以在任何其他脚本中订阅该事件并执行逻辑:

public class UIScript: NetworkBehaviour
{    
    void Awake()
    {
        EventManager.Instance.OnPlayerTurnChanged+= UpdateUI;
        timer = 0f;
    }

    void UpdateUI(int playerId)
    {
        //UI Logic to set the UI for the proper player
    }
}