C# TCP/IP统一Android错误Helfe
嘿,我正在用unity3d制作一个android游戏,我想从我的笔记本电脑上控制我的游戏我制作了一个TCP/IP聊天程序,通过互联网将键盘输入到我的android,我为我的笔记本电脑(Windows form APP)制作了一个,为Unity制作了一个,除了一个小的恼人的错误外,一切正常 错误: 当我按下X按钮将球体从点a移动到点B时,我想要,但当发送“X”值时,它会显示: 只能从主线程调用内部\u get\u位置。 加载场景时,将从加载线程执行构造函数和字段初始值设定项。 不要在构造函数或字段初始值设定项中使用此函数,而是将初始化代码移到Awake或Start函数中 当我关闭Windows窗体应用程序时,球会移动,我也尝试了唤醒和启动功能 PS:我在同一台计算机上测试这个,所以它与IP地址无关 以下是我的统一部分代码:C# TCP/IP统一Android错误Helfe,c#,android,unity3d,tcp,C#,Android,Unity3d,Tcp,嘿,我正在用unity3d制作一个android游戏,我想从我的笔记本电脑上控制我的游戏我制作了一个TCP/IP聊天程序,通过互联网将键盘输入到我的android,我为我的笔记本电脑(Windows form APP)制作了一个,为Unity制作了一个,除了一个小的恼人的错误外,一切正常 错误: 当我按下X按钮将球体从点a移动到点B时,我想要,但当发送“X”值时,它会显示: 只能从主线程调用内部\u get\u位置。 加载场景时,将从加载线程执行构造函数和字段初始值设定项。 不要在构造函数或字段
using UnityEngine;
using System.Collections;
using System.Text;
using System;
using System.Net;
using System.Net.Sockets;
public class CHAT : MonoBehaviour {
private Socket sck;
EndPoint epLocal, epRemote;
//Gameobjects
public Transform ball , point;
//logic
string xIsHere;
// Use this for initialization
void Start () {
sck = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
sck.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
epLocal = new IPEndPoint(IPAddress.Parse("192.168.1.9"), Convert.ToInt32("81"));
sck.Bind(epLocal);
epRemote = new IPEndPoint(IPAddress.Parse("192.168.1.9"), Convert.ToInt32("80"));
sck.Connect(epRemote);
Debug.Log("COnnected");
}
void Awake()
{
}
// Update is called once per frame
void Update () {
byte[] buffer = new byte[1500];
sck.BeginReceiveFrom(buffer, 0, buffer.Length, SocketFlags.None, ref epRemote, new AsyncCallback(MessageCAllBack), buffer);
}
private void MessageCAllBack(IAsyncResult aResult)
{
try
{
int size = sck.EndReceiveFrom(aResult, ref epRemote);
if (size > 0)
{
byte[] receivedData = new byte[1464];
receivedData = (byte[])aResult.AsyncState;
ASCIIEncoding eEncoding = new ASCIIEncoding();
string receivedMessage = eEncoding.GetString(receivedData);
//bn3mal if statement bnshof weslat el X wela la2 w iza weslat bnmasi el tabeh
xIsHere = receivedMessage;
if (xIsHere.Contains("x"))
{
Debug.Log("X is here");
ball.position = Vector3.MoveTowards(ball.position, point.position, 5 * Time.deltaTime);
}
//b3deen bntba3 el msg bs b7aletna bdna n5li el touch active.
//ListMessage.Items.Add("Sender:" + receivedMessage);
}
byte[] buffer = new byte[1500];
sck.BeginReceiveFrom(buffer, 0, buffer.Length, SocketFlags.None, ref epRemote, new AsyncCallback(MessageCAllBack), buffer);
}
catch(Exception exp)
{
Debug.Log(exp.ToString());
}
}
}
好的,我修复了它,我只是更改了我使用的5050和5040端口,它工作得很好,在update()函数中添加了一个if语句,使对象移动。。总之,代码如下:
using UnityEngine;
using System.Collections;
using System.Text;
using System;
using System.Net;
using System.Net.Sockets;
public class CHAT : MonoBehaviour {
private Socket sck;
EndPoint epLocal, epRemote;
//Gameobjects
public Transform ball , point;
//logic
static Boolean conn = false,arrievedX = false;
string xIsHere;
// Use this for initialization
void Start () {
sck = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
sck.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
epLocal = new IPEndPoint(IPAddress.Parse("192.168.1.8"), Convert.ToInt32("5050"));
sck.Bind(epLocal);
epRemote = new IPEndPoint(IPAddress.Parse("192.168.1.9"), Convert.ToInt32("5040"));
sck.Connect(epRemote);
conn = true;
Debug.Log("COnnected");
}
void Awake()
{
}
// Update is called once per frame
void Update () {
byte[] buffer = new byte[1500];
sck.BeginReceiveFrom(buffer, 0, buffer.Length, SocketFlags.None, ref epRemote, new AsyncCallback(MessageCAllBack), buffer);
if (arrievedX)
{
ball.position = Vector3.MoveTowards(ball.position, point.position, 5 * Time.deltaTime);
}
}
void OnGUI()
{
if (arrievedX)
{
GUI.Box(new Rect(100, 100, 100, 25), "X is here");
}
if (conn)
{
GUI.Box(new Rect(100, 50, 100, 50), "Connected");
}
}
private void MessageCAllBack(IAsyncResult aResult )
{
try
{
int size = sck.EndReceiveFrom(aResult, ref epRemote);
if (size > 0)
{
byte[] receivedData = new byte[1464];
receivedData = (byte[])aResult.AsyncState;
ASCIIEncoding eEncoding = new ASCIIEncoding();
string receivedMessage = eEncoding.GetString(receivedData);
//bn3mal if statement bnshof weslat el X wela la2 w iza weslat bnmasi el tabeh
if (receivedMessage.Contains("x"))
{
Debug.Log("X is here");
arrievedX = true;
}
//b3deen bntba3 el msg bs b7aletna bdna n5li el touch active.
//ListMessage.Items.Add("Sender:" + receivedMessage);
}
byte[] buffer = new byte[1500];
sck.BeginReceiveFrom(buffer, 0, buffer.Length, SocketFlags.None, ref epRemote, new AsyncCallback(MessageCAllBack), buffer);
}
catch(Exception exp)
{
Debug.Log(exp.ToString());
}
}
}
问题在于非同步回调
MessageCAllBack
将从另一个线程调用,由于Unity不是线程安全的,因此任何修改游戏对象的尝试都将导致该错误。也许您应该尝试在某些内容更改后设置一个标志,然后更新update()
函数中的位置,然后再次清除标志?也许你也可以使用协同程序。