Ejemplo n.º 1
0
    void Start()
    {
        GameData.networkPlayers.Clear();
        discoveredServers.Clear();

        roomListPanel.SetActive(!GameData.isHost);
        playerListPanel.SetActive(GameData.isHost);
        networkDiscovery = GetComponent <MyNetworkDiscovery>();
        networkDiscovery.OnServerFound.AddListener(OnDiscoveredServer);
        roomPanelBackBtn.onClick.AddListener(Back);
        playerPanelBackBtn.onClick.AddListener(Back);
        startGameBtn.gameObject.SetActive(false); // 开始不显示游戏开始按钮
        startGameBtn.onClick.AddListener(OnClickStartGame);
        MyNetworkRoomManager mamager = NetworkManager.singleton as MyNetworkRoomManager;

        mamager.onCreateRoomPlayer     = OnCreateRoomPlayer;
        mamager.onClientDisconnect     = OnClientDisconnect;
        mamager.onGameStartStateChange = OnGameStartStateChange;
        if (GameData.isHost)
        {
            roomName.text = $"[{GameData.playerName}的房间]";
            NetworkManager.singleton.StartHost();
            networkDiscovery.AdvertiseServer();
        }
        else
        {
            networkDiscovery.MyStartDiscovery();
        }
    }
Ejemplo n.º 2
0
    private void OnClickStartGame()
    {
        if (!GameData.isHost)
        {
            return;
        }
        MyNetworkRoomManager manager = NetworkManager.singleton as MyNetworkRoomManager;

        if (manager.numPlayers < manager.minPlayer)
        {
            return;
        }
        // 关闭服务器广播
        networkDiscovery.StopDiscovery();
        Global.EnterGame();
    }