简体   繁体   English

Unity多人游戏

[英]Unity multiplayer

Down Below is the code for connection approval.下面是连接批准的代码。 As shown in the image I want to turn off the Home menu GameObject(The object containing the canvas) the moment we become host or client.如图所示,我想在我们成为主机或客户端时关闭主菜单游戏对象(包含画布的 object)。 But In the update loop the control do not go to else statement if I am the client but it does go when I am the host.但是在更新循环中,如果我是客户端,控件不会 go 到 else 语句,但当我是主机时它会执行 go。 Why is that?这是为什么?

[This is the image][1] [这是图片][1]

using Unity.Netcode;
using UnityEngine;
using TMPro;

public class ConnectionApproval : NetworkBehaviour
{
    public TMP_InputField ThePassword;
    public GameObject HomeMenu;

    private void Update()
    {
        if (!NetworkManager.Singleton.IsServer || !NetworkManager.Singleton.IsClient)
        {
            HomeMenu.SetActive(true);
        }
        else
        {
            Debug.Log("I am " + NetworkManager.Singleton.IsServer.ToString() + " " + NetworkManager.Singleton.IsClient.ToString());
            HomeMenu.SetActive(false);
        }

    }

    private void ApprovalCheck(byte[] connectionData_, ulong connectionId_, NetworkManager.ConnectionApprovedDelegate callback)
    {
        bool IsApproved = Encoding.ASCII.GetString(connectionData_) == ThePassword.text;
        callback(true, null, IsApproved, new Vector3(Random.Range(-3f, 3f), 1f, 0f), Quaternion.identity);
    }
    
    public void Host()
    {
        NetworkManager.Singleton.ConnectionApprovalCallback += ApprovalCheck;
        NetworkManager.Singleton.StartHost();
    }


    public void Client()
    {
        NetworkManager.Singleton.NetworkConfig.ConnectionData = Encoding.ASCII.GetBytes(ThePassword.text);
        NetworkManager.Singleton.StartClient();
    }

}```


  [1]: https://i.stack.imgur.com/ipWR8.png

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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