【问题标题】:Multiplayer UI using Mirror unity使用 Mirror unity 的多人游戏 UI
【发布时间】:2021-12-06 13:28:59
【问题描述】:

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

目前公告仅显示在主机游戏中,如果公告来自属于玩家的 Networkbehaviour 类,则可以使用简单的 ClientRPC 函数轻松解决,但我希望 UI 函数从处理 UI 元素的不同类。

实现这一点的正确方法是什么? UIHandler 是否需要从任何网络类继承?会喜欢关于这个主题的一些提示。

提前致谢, 阿米特·沃尔夫。

【问题讨论】:

    标签: unity3d multiplayer mirror


    【解决方案1】:

    一种常见的策略是构建一个单例网络事件管理器,将 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
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2022-08-07
      • 2022-01-15
      • 2021-09-29
      • 1970-01-01
      • 2022-08-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-22
      相关资源
      最近更新 更多