Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Unity客户端未连接到主机_C#_Unity3d_Networking - Fatal编程技术网

C# Unity客户端未连接到主机

C# Unity客户端未连接到主机,c#,unity3d,networking,C#,Unity3d,Networking,我试图使用unity将网络客户端连接到主机,但通过调试检查debug.Log(myClient.isConnected.ToString())返回false。我正在创建一个新的客户端,并使用方法setupClient()进行连接,但我想我做得不对。我怎样才能解决这个问题?我的调试是否正确 using System.Collections; using System.IO; using UnityEngine; using UnityEngine.Networking; using UnityEn

我试图使用unity将网络客户端连接到主机,但通过调试检查
debug.Log(myClient.isConnected.ToString())返回false。我正在创建一个新的客户端,并使用方法
setupClient()
进行连接,但我想我做得不对。我怎样才能解决这个问题?我的调试是否正确

using System.Collections;
using System.IO;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.Networking.NetworkSystem;

public class MyMsgType
{
    public static short texture = MsgType.Highest + 1;
};

//Create a class that holds a variables to send
public class TextureMessage : MessageBase
{
    public byte[] textureBytes;
    public string message = "Test Received Message"; //Optional
}

public class UNETChat : Chat
{
    NetworkClient myClient;
    public Texture2D previewTexture;
    string messageToSend = "Screen Short Image";

    private void Start()
    {
        //if the client is also the server
        if (NetworkServer.active) 
        {
            // Register to connect event
            myClient.RegisterHandler(MsgType.Connect, OnConnected);
            // Register to texture receive event
            myClient.RegisterHandler(MyMsgType.texture, OnTextureReceive);
        }
        setupClient();
    }

    public void DoSendTexture()
    {
        StartCoroutine(TakeSnapshot(Screen.width, Screen.height));
    }

    WaitForEndOfFrame frameEnd = new WaitForEndOfFrame();

    public IEnumerator TakeSnapshot(int width, int height)
    {
        yield return frameEnd;

        Texture2D texture = new Texture2D(800, 800, TextureFormat.RGB24, true);
        texture.ReadPixels(new Rect(0, 0, 800, 800), 0, 0);
        texture.LoadRawTextureData(texture.GetRawTextureData());
        texture.Apply();

        Debug.Log("Texture size is : " + texture.EncodeToPNG().Length);

        sendTexture(texture, messageToSend);

        // gameObject.renderer.material.mainTexture = TakeSnapshot;
    }

    //Call to send the Texture and a simple string message
    public void sendTexture(Texture2D texture, string message)
    {
        TextureMessage msg = new TextureMessage();

        //Convert Texture2D to byte array
        msg.textureBytes = texture.GetRawTextureData();
        msg.message = message;

        NetworkServer.SendToAll(MyMsgType.texture, msg);

        Debug.Log("Texture Sent!!");
    }

    // Create a client and connect to the server port
    public void setupClient()
    {
        Debug.Log("Setup Client");
        //Create new client
        myClient = new NetworkClient();
        //Register to connect event
        myClient.RegisterHandler(MsgType.Connect, OnConnected);
        //Register to texture receive event
        myClient.RegisterHandler(MyMsgType.texture, OnTextureReceive);
        //Connect to server
        myClient.Connect("localhost", 4444);

        Debug.Log(myClient.isConnected.ToString());
    }

    //Called when texture is received
    public void OnTextureReceive(NetworkMessage netMsg)
    {
        TextureMessage msg = netMsg.ReadMessage<TextureMessage>();

        //Your Received message
        string message = msg.message;

        Debug.Log("Texture Messsage " + message);

        //Your Received Texture2D
        Texture2D receivedtexture = new Texture2D(4, 4);
        receivedtexture.LoadRawTextureData(msg.textureBytes);
        receivedtexture.Apply();
    }

    public void OnConnected(NetworkMessage netMsg)
    {
        Debug.Log("Connected to server");
    }
}
使用系统集合;
使用System.IO;
使用UnityEngine;
使用UnityEngine。联网;
使用UnityEngine.Networking.NetworkSystem;
公共类MyMsgType
{
公共静态短纹理=MsgType.Highest+1;
};
//创建一个包含要发送的变量的类
公共类TextureMessage:MessageBase
{
公共字节[]纹理字节;
public string message=“Test Received message”;//可选
}
公共类UNETChat:Chat
{
网络客户端myClient;
公共纹理2D预览纹理;
string messageToSend=“屏幕短图像”;
私有void Start()
{
//如果客户端也是服务器
if(NetworkServer.active)
{
//注册以连接事件
myClient.RegisterHandler(MsgType.Connect,OnConnected);
//注册到纹理接收事件
myClient.RegisterHandler(MyMsgType.texture,OnTextureReceive);
}
setupClient();
}
公共void DoSendTexture()
{
开始例行程序(拍摄快照(屏幕宽度、屏幕高度));
}
WaitForEndOfFrame frameEnd=新的WaitForEndOfFrame();
公共IEnumerator快照(整数宽度、整数高度)
{
屈服返回帧结束;
Texture2D纹理=新的Texture2D(800,800,TextureFormat.RGB24,真);
读取像素(新的矩形(0,0,800,800),0,0);
加载RawTextureData(texture.GetRawTextureData());
纹理。应用();
Log(“纹理大小为:”+Texture.EncodeToPNG().Length);
sendTexture(纹理,messageToSend);
//gameObject.renderer.material.mainTexture=TakeSnapshot;
}
//调用以发送纹理和简单的字符串消息
公共void sendTexture(Texture2D纹理,字符串消息)
{
TextureMessage msg=新的TextureMessage();
//将Texture2D转换为字节数组
msg.textureBytes=texture.GetRawTextureData();
msg.message=消息;
NetworkServer.SendToAll(MyMsgType.texture,msg);
Log(“纹理已发送!!”);
}
//创建客户端并连接到服务器端口
公共void setupClient()
{
Log(“安装客户端”);
//创建新客户端
myClient=新的NetworkClient();
//注册以连接事件
myClient.RegisterHandler(MsgType.Connect,OnConnected);
//注册到纹理接收事件
myClient.RegisterHandler(MyMsgType.texture,OnTextureReceive);
//连接到服务器
Connect(“localhost”,4444);
Log(myClient.isConnected.ToString());
}
//接收纹理时调用
public void OnTextureReceive(NetworkMessage netMsg)
{
TextureMessage msg=netMsg.ReadMessage();
//您收到的信息
字符串消息=msg.message;
Debug.Log(“纹理消息”+消息);
//您收到的纹理2d
Texture2D receivedtexture=新的Texture2D(4,4);
接收Texture.LoadRawTextureData(msg.textureBytes);
receivedtexture.Apply();
}
未连接的公共无效(NetworkMessage netMsg)
{
Log(“连接到服务器”);
}
}
我的调试是否正确

using System.Collections;
using System.IO;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.Networking.NetworkSystem;

public class MyMsgType
{
    public static short texture = MsgType.Highest + 1;
};

//Create a class that holds a variables to send
public class TextureMessage : MessageBase
{
    public byte[] textureBytes;
    public string message = "Test Received Message"; //Optional
}

public class UNETChat : Chat
{
    NetworkClient myClient;
    public Texture2D previewTexture;
    string messageToSend = "Screen Short Image";

    private void Start()
    {
        //if the client is also the server
        if (NetworkServer.active) 
        {
            // Register to connect event
            myClient.RegisterHandler(MsgType.Connect, OnConnected);
            // Register to texture receive event
            myClient.RegisterHandler(MyMsgType.texture, OnTextureReceive);
        }
        setupClient();
    }

    public void DoSendTexture()
    {
        StartCoroutine(TakeSnapshot(Screen.width, Screen.height));
    }

    WaitForEndOfFrame frameEnd = new WaitForEndOfFrame();

    public IEnumerator TakeSnapshot(int width, int height)
    {
        yield return frameEnd;

        Texture2D texture = new Texture2D(800, 800, TextureFormat.RGB24, true);
        texture.ReadPixels(new Rect(0, 0, 800, 800), 0, 0);
        texture.LoadRawTextureData(texture.GetRawTextureData());
        texture.Apply();

        Debug.Log("Texture size is : " + texture.EncodeToPNG().Length);

        sendTexture(texture, messageToSend);

        // gameObject.renderer.material.mainTexture = TakeSnapshot;
    }

    //Call to send the Texture and a simple string message
    public void sendTexture(Texture2D texture, string message)
    {
        TextureMessage msg = new TextureMessage();

        //Convert Texture2D to byte array
        msg.textureBytes = texture.GetRawTextureData();
        msg.message = message;

        NetworkServer.SendToAll(MyMsgType.texture, msg);

        Debug.Log("Texture Sent!!");
    }

    // Create a client and connect to the server port
    public void setupClient()
    {
        Debug.Log("Setup Client");
        //Create new client
        myClient = new NetworkClient();
        //Register to connect event
        myClient.RegisterHandler(MsgType.Connect, OnConnected);
        //Register to texture receive event
        myClient.RegisterHandler(MyMsgType.texture, OnTextureReceive);
        //Connect to server
        myClient.Connect("localhost", 4444);

        Debug.Log(myClient.isConnected.ToString());
    }

    //Called when texture is received
    public void OnTextureReceive(NetworkMessage netMsg)
    {
        TextureMessage msg = netMsg.ReadMessage<TextureMessage>();

        //Your Received message
        string message = msg.message;

        Debug.Log("Texture Messsage " + message);

        //Your Received Texture2D
        Texture2D receivedtexture = new Texture2D(4, 4);
        receivedtexture.LoadRawTextureData(msg.textureBytes);
        receivedtexture.Apply();
    }

    public void OnConnected(NetworkMessage netMsg)
    {
        Debug.Log("Connected to server");
    }
}
否,并且您没有使用NetworkClient。已正确连接

该函数是异步的,这意味着当您调用它时,它不会等待或阻止该连接实际连接。它使用线程进行连接,当您在调用
NetworkClient.Connect
之后立即使用
NetworkClient.isConnected
时,您将得到意外的结果

NetworkClient
在连接时使用回调函数通知您。回调函数在
NetworkClient.RegisterHandler
MsgType.Connect
中注册。在您的情况下,它已注册到
OnConnected
函数,因此连接到网络时应调用
OnConnected

您必须启动协同路由并在该协同路由函数中使用
NetworkClient.isConnected
,以等待如下连接:

while(!NetworkClient.isConnected)
{
    Debug.Log("Waiting for Connection");
    yield return null;
}
Debug.Log("Connected");
或者使用回调函数(
OnConnected
)检测客户端何时连接


不相关,但订阅错误和断开连接事件也是非常有用的。在对网络代码进行故障排除时,这些都很重要

myClient.RegisterHandler(MsgType.Error, OnError);
myClient.RegisterHandler(MsgType.Disconnect, OnDisconnect);

private void OnError(NetworkMessage netMsg)
{
ErrorMessage error=netMsg.ReadMessage();
Log(“连接时出错:+Error.errorCode”);
}
专用void OnDisconnect(NetworkMessage netMsg)
{
ErrorMessage error=netMsg.ReadMessage();
Debug.Log(“断开连接:“+error.errorCode”);
}
我的调试是否正确

using System.Collections;
using System.IO;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.Networking.NetworkSystem;

public class MyMsgType
{
    public static short texture = MsgType.Highest + 1;
};

//Create a class that holds a variables to send
public class TextureMessage : MessageBase
{
    public byte[] textureBytes;
    public string message = "Test Received Message"; //Optional
}

public class UNETChat : Chat
{
    NetworkClient myClient;
    public Texture2D previewTexture;
    string messageToSend = "Screen Short Image";

    private void Start()
    {
        //if the client is also the server
        if (NetworkServer.active) 
        {
            // Register to connect event
            myClient.RegisterHandler(MsgType.Connect, OnConnected);
            // Register to texture receive event
            myClient.RegisterHandler(MyMsgType.texture, OnTextureReceive);
        }
        setupClient();
    }

    public void DoSendTexture()
    {
        StartCoroutine(TakeSnapshot(Screen.width, Screen.height));
    }

    WaitForEndOfFrame frameEnd = new WaitForEndOfFrame();

    public IEnumerator TakeSnapshot(int width, int height)
    {
        yield return frameEnd;

        Texture2D texture = new Texture2D(800, 800, TextureFormat.RGB24, true);
        texture.ReadPixels(new Rect(0, 0, 800, 800), 0, 0);
        texture.LoadRawTextureData(texture.GetRawTextureData());
        texture.Apply();

        Debug.Log("Texture size is : " + texture.EncodeToPNG().Length);

        sendTexture(texture, messageToSend);

        // gameObject.renderer.material.mainTexture = TakeSnapshot;
    }

    //Call to send the Texture and a simple string message
    public void sendTexture(Texture2D texture, string message)
    {
        TextureMessage msg = new TextureMessage();

        //Convert Texture2D to byte array
        msg.textureBytes = texture.GetRawTextureData();
        msg.message = message;

        NetworkServer.SendToAll(MyMsgType.texture, msg);

        Debug.Log("Texture Sent!!");
    }

    // Create a client and connect to the server port
    public void setupClient()
    {
        Debug.Log("Setup Client");
        //Create new client
        myClient = new NetworkClient();
        //Register to connect event
        myClient.RegisterHandler(MsgType.Connect, OnConnected);
        //Register to texture receive event
        myClient.RegisterHandler(MyMsgType.texture, OnTextureReceive);
        //Connect to server
        myClient.Connect("localhost", 4444);

        Debug.Log(myClient.isConnected.ToString());
    }

    //Called when texture is received
    public void OnTextureReceive(NetworkMessage netMsg)
    {
        TextureMessage msg = netMsg.ReadMessage<TextureMessage>();

        //Your Received message
        string message = msg.message;

        Debug.Log("Texture Messsage " + message);

        //Your Received Texture2D
        Texture2D receivedtexture = new Texture2D(4, 4);
        receivedtexture.LoadRawTextureData(msg.textureBytes);
        receivedtexture.Apply();
    }

    public void OnConnected(NetworkMessage netMsg)
    {
        Debug.Log("Connected to server");
    }
}
否,并且您没有使用NetworkClient。已正确连接

该函数是异步的,这意味着当您调用它时,它不会等待或阻止该连接实际连接。它使用线程进行连接,当您在调用
NetworkClient.Connect
之后立即使用
NetworkClient.isConnected
时,您将得到意外的结果

NetworkClient
在连接时使用回调函数通知您。回调函数在
NetworkClient.RegisterHandler
MsgType.Connect
中注册。在您的情况下,它已注册到
OnConnected
函数,因此连接到网络时应调用
OnConnected

你要么开始合作,要么