【发布时间】:2016-06-11 12:58:08
【问题描述】:
我想用 Unity 制作套接字网络程序,所以我做了一个测试项目,并在该项目中添加了 NetworkManager 类和 PacketManager 类进行测试。
程序启动时,我调用NetworkManager中的connect()方法,网络连接成功。而且我必须将数据包制作成字节流以发送到服务器。所以我使用编组和调用getBytes() 方法创建了PacketManager 类(静态)。但它不起作用(unity 程序被终止)。
这是我的脚本
Move.cs
using UnityEngine;
using System.Collections;
using System.Net.Sockets;
using System.Net;
using System;
using System.Runtime.InteropServices;
public class move : MonoBehaviour {
private NetworkManager nManager;
// Use this for initialization
void Start () {
Debug.Log ("start");
nManager = new NetworkManager ();
nManager.connect ();
packet p = new packet();
p.type = 100;
p.msg = "abcdabcd";
byte[] array = PacketManager.getBytes (p);
}
// Update is called once per frame
void Update () {
}
}
NetworkManager.cs
using UnityEngine;
using System.Collections;
using System.Net.Sockets;
using System.Net;
using System;
using System.Runtime.InteropServices;
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct packet
{
public int type;
public int number;
public int room;
public float x, y, z;
public float rotate;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 100)]
public string msg;
}
public class NetworkManager : MonoBehaviour
{
private const string serverIP = "127.0.0.1";
private const int portNum = 9090;
private const int buffersize = 128;
private static TcpClient client = null;
private static NetworkStream netStream = null;
public NetworkManager() {}
public Boolean connect()
{
Boolean success = false;
try
{
Debug.Log("try to connenct..");
client = new TcpClient(serverIP, portNum);
netStream = client.GetStream();
Debug.Log("connected success!");
}
catch (Exception e)
{
success = false;
Debug.Log(e.Message);
}
return success;
}
public void sendPacket(packet p)
{
byte[] array = PacketManager.getBytes(p);
netStream.Write(array, 0, buffersize);
}
public packet recvPacket()
{
packet p = new packet();
byte[] bytebuffer = new Byte[buffersize];
int totalByteRcvd = 0;
int bytesRcvd = 0;
while (totalByteRcvd < buffersize)
{
if ((bytesRcvd = netStream.Read(bytebuffer, totalByteRcvd, buffersize - totalByteRcvd)) == 0)
{
p = PacketManager.BytesToSturct<packet>(bytebuffer);
break;
}
totalByteRcvd += bytesRcvd;
Debug.Log("byte : " + totalByteRcvd);
}
return p;
}
}
PacketManager.cs
using UnityEngine;
using System.Collections;
using System.Net.Sockets;
using System.Net;
using System;
using System.Runtime.InteropServices;
public class PacketManager : MonoBehaviour
{
public static byte[] getBytes(object str)
{
int size = Marshal.SizeOf(str);
byte[] arr = new byte[size];
IntPtr ptr = Marshal.AllocHGlobal(size);
Marshal.StructureToPtr(str, ptr, true);
Marshal.Copy(ptr, arr, 0, size);
Marshal.FreeHGlobal(ptr);
return arr;
}
public static T BytesToSturct<T>(byte[] buffer) where T : struct
{
int size = Marshal.SizeOf(typeof(T));
IntPtr ptr = Marshal.AllocHGlobal(size);
Marshal.Copy(buffer, 0, ptr, size);
T obj = (T)Marshal.PtrToStructure(ptr, typeof(T));
Marshal.FreeHGlobal(ptr);
return obj;
}
}
如果我删除:
byte[] array = PacketManager.getBytes (p);
在move.cs 中,日志打印得很好,但是如果我调用任何方法(不仅是那个方法),程序就会终止。
有什么问题?
【问题讨论】:
-
unity 程序终止 什么意思?结冰?
-
你能调试到导致你的程序失败的行吗?终止是什么意思?崩溃、ErrorMessage、Frezze?
-
我的意思是,“Unity 编辑器已停止工作”消息弹出,unity 程序突然退出并转到桌面(我必须重新打开 unity 程序)所以我无法检查日志消息