c# - TCP/IP Unity Android ERROR 帮助

标签 c# android unity3d tcp

嘿,我正在使用 unity3d 制作安卓游戏,我想通过我的笔记本电脑控制我的游戏(Windows 窗体应用程序)和一个用于 Unity 的应用程序,除了一个烦人的小错误外,一切正常。

错误: 我想当我按下 X 按钮将球体从 A 点移动到 B 点时,但是当发送“x”值时,它说:

INTERNAL_get_position 只能从主线程调用。 加载场景时,将从加载线程执行构造函数和字段初始化程序。 不要在构造函数或字段初始值设定项中使用此函数,而是将初始化代码移至 Awake 或 Start 函数。

当我关闭 Windows 窗体 APP 时,球会移动,我也尝试了 Awake 和启动功能。

PS:我在同一台计算机上进行测试,因此它与 IP 地址无关。

这是我的统一部分代码:

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());
        }

    }

}

关于c# - TCP/IP Unity Android ERROR 帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32605647/

相关文章:

c# - 打开 XML 创建多个工作表

c# - 在屏幕上检索凝视坐标

android - 使用 Android Studio 0.4.0 的渲染问题

ios - 错误 : You are not allowed to remove the Unity splash screen from your game

c# - 使 anchor 最小最大定位在 Gameobject ui 角的脚本?

c# - .NET 列表排序在所有返回 0 后返回不同的顺序

c# - context.SaveChanges 不会保存更改而不会出现任何错误

android - 有没有办法将消息从 Android 浏览器传递到应用程序?

java - 将文件中包含的字节值读入字节数组

c# - unity StopCoroutine 存在多层(或嵌套)时不停止协程 "yield return"