C# Unity线程问题:无法委派任务

C# Unity线程问题:无法委派任务,c#,multithreading,unity3d,C#,Multithreading,Unity3d,我使用树莓pi通过UDP向Unity发送信息。为了避免Unity的多线程问题,我在脚本的一部分中委派了一项任务。我收到一个错误,说明: “错误CS0120:访问非静态成员'udprecive.te'需要对象引用” 如何使用此任务委派技术正确发送信息?我让raspberry pi将信息正确地发送到Unity,但由于线程问题,无法检索信息,这正是我试图解决的问题 using UnityEngine; using System.Collections; using System; using Sys

我使用树莓pi通过UDP向Unity发送信息。为了避免Unity的多线程问题,我在脚本的一部分中委派了一项任务。我收到一个错误,说明:

“错误CS0120:访问非静态成员'udprecive.te'需要对象引用”

如何使用此任务委派技术正确发送信息?我让raspberry pi将信息正确地发送到Unity,但由于线程问题,无法检索信息,这正是我试图解决的问题

using UnityEngine;
using System.Collections;

using System;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;

public class UDPReceive : MonoBehaviour {

    // receiving Thread
    Thread receiveThread;

    // udpclient object
    UdpClient client;

    // public
    public string IP = "128.235.117.210";
    public int port; // define > init
    public TaskExecutorScript te;


    // NEW
    public int X;
    public int Y;
    string[] split_data;
    public static GameObject cube;



    // infos
    public string lastReceivedUDPPacket="";

    private string info;

    // start from shell
    private static void Main()
        //public void Update()
    {


        UDPReceive receiveObj=new UDPReceive();
        receiveObj.init();

        string text="";
        do
        {
            text = Console.ReadLine();

        }
        while(!text.Equals("exit"));



        // NEW
        te.ScheduleTask(new Task(delegate
                                                 {
            // Add whatever Unity API code you want here
            //split_data = text.Split(' ');
            //X = Int32.Parse(split_data [0]);
            //Y = Int32.Parse(split_data [1]);

            transform.position = new Vector3(0, 0, 0);
        }
        ));








    }
    // start from unity3d
    public void Start()
    {
        te = GetComponent<TaskExecutorScript> ();
        init();  

        // cube = GameObject.Find ("Cube");
    }

    // OnGUI
    void OnGUI()
    {
        Rect rectObj=new Rect(40,10,200,400);
        GUIStyle style = new GUIStyle();
        style.alignment = TextAnchor.UpperLeft;
        GUI.Box(rectObj,"# UDPReceive\n"+IP+": "+port+" #\n"
                + "shell> nc -u "+IP+": "+port+" \n"
                + "\nLast Packet: \n"+ lastReceivedUDPPacket
                ,style);
    }

    // init
    private void init()
    {
        // Endpunkt definieren, von dem die Nachrichten gesendet werden.
        print("UDPSend.init()");

        // define port
        port = 6666;  

        // status
        print("Sending to "+IP+": "+port);
        print("Test-Sending to this Port: nc -u "+IP+": "+port+"");


        // ----------------------------
        // Abhören
        // ----------------------------
        // Lokalen Endpunkt definieren (wo Nachrichten empfangen werden).
        // Einen neuen Thread für den Empfang eingehender Nachrichten erstellen.
        receiveThread = new Thread(
            new ThreadStart(ReceiveData));
        receiveThread.IsBackground = true;
        receiveThread.Start();

    }

    // receive thread
    private  void ReceiveData()
    {

        client = new UdpClient(port);
        while (true)
        {

            try
            {
                // Bytes empfangen.
                IPEndPoint anyIP = new IPEndPoint(IPAddress.Any, port);
                byte[] data = client.Receive(ref anyIP);

                // Bytes mit der UTF8-Kodierung in das Textformat kodieren.
                string text = Encoding.UTF8.GetString(data);

                // Den abgerufenen Text anzeigen.
                print(">> " + text);

                // latest UDPpacket
                lastReceivedUDPPacket=text;

            }
            catch (Exception err)
            {
                print(err.ToString());
            }
        }
    }

    // getLatestUDPPacket
    // cleans up the rest
    public string getLatestUDPPacket()
    {
        return lastReceivedUDPPacket;
    }
    /*
        public void UseInfo()
        {
            info = lastReceivedUDPPacket;
            arraySplit = info.Split ('0');
            x =  int.Parse(arraySplit [0]);
            y =  int.Parse(arraySplit [1]);
            this.transform.position = new Vector3 (x, y, 0);

        } */

    void OnDisable() 
    { 
        if ( receiveThread!= null) 
            receiveThread.Abort(); 

        client.Close(); 
    } 
}
使用UnityEngine;
使用系统集合;
使用制度;
使用系统文本;
Net系统;
使用System.Net.Sockets;
使用系统线程;
公共阶级:单一行为{
//接收线程
线程接收线程;
//udpclient对象
UDP客户;
//公开的
公共字符串IP=“128.235.117.210”;
公共int端口;//定义>初始化
公共任务执行器;
//新的
公共int X;
公共智力;
字符串[]分割数据;
公共静态游戏对象立方体;
//信息
公共字符串lastReceivedUDPPacket=“”;
私有字符串信息;
//从外壳开始
私有静态void Main()
//公共无效更新()
{
UDPRECIVE receiveObj=新UDPRECIVE();
receiveObj.init();
字符串文本=”;
做
{
text=Console.ReadLine();
}
而(!text.Equals(“exit”);
//新的
te.ScheduleTask(新任务(委托
{
//在此处添加所需的任何Unity API代码
//分割数据=文本分割(“”);
//X=Int32.Parse(split_数据[0]);
//Y=Int32.Parse(split_数据[1]);
transform.position=新矢量3(0,0,0);
}
));
}
//从unity3d开始
公开作废开始()
{
te=GetComponent();
init();
//cube=GameObject.Find(“cube”);
}
//昂吉
void OnGUI()
{
Rect rectObj=新的Rect(40,10200400);
GUIStyle style=新的GUIStyle();
style.alignment=TextAnchor.UpperLeft;
GUI.Box(rectObj,“#udprective\n”+IP+”:“+port+”#\n”
+“外壳>nc-u”+IP+:“+port+”\n”
+“\n上次数据包:\n”+上次接收的数据包
,风格);
}
//初始化
私有void init()
{
//这是一个明确的定义。
打印(“UDPSend.init()”;
//定义端口
端口=6666;
//地位
打印(“发送到“+IP+”:“+port”);
打印(“测试发送到此端口:nc-u”+IP+:“+Port+”);
// ----------------------------
//阿伯伦
// ----------------------------
//Lokalen Endpunkt Definerieren(我的名字叫Nachrichten Emphangen werden)。
//这是一种新的方法。
receiveThread=新线程(
新ThreadStart(接收数据));
receiveThread.IsBackground=true;
receiveThread.Start();
}
//接收线程
私有void ReceiveData()
{
客户端=新的UdpClient(端口);
while(true)
{
尝试
{
//范根。
IPEndPoint anyIP=新IPEndPoint(IPAddress.Any,端口);
字节[]数据=client.Receive(ref anyIP);
//以das Textformat kodieren格式输入UTF8 Kodierung的字节数。
string text=Encoding.UTF8.GetString(数据);
//这是一本书。
打印(“>>”+文本);
//最新UDPpacket
LastReceivedUppacket=文本;
}
捕获(异常错误)
{
打印(err.ToString());
}
}
}
//getLatestUDPPacket
//把剩下的清理干净
公共字符串getLatestUDPPacket()
{
返回上次接收的数据包;
}
/*
public void UseInfo()
{
info=最近收到的数据包;
arraySplit=info.Split('0');
x=int.Parse(arraySplit[0]);
y=int.Parse(arraySplit[1]);
this.transform.position=新矢量3(x,y,0);
} */
无效可禁用()
{ 
如果(receiveThread!=null)
receiveThread.Abort();
client.Close();
} 
}

使用UnityEngine;
使用制度;
使用系统集合;
使用System.Collections.Generic;
公共委托无效任务();
公共类TaskExecutor脚本:MonoBehavior{
专用队列TaskQueue=新队列();
私有对象_queueLock=新对象();
//每帧调用一次更新
无效更新(){
锁(_queueLock)
{
如果(TaskQueue.Count>0)
TaskQueue.Dequeue();
}
}
public void ScheduleTask(任务newTask)
{
锁(_queueLock)
{
如果(TaskQueue.Count<100)
TaskQueue.Enqueue(newTask);
}
}
}

static void Main在那里做什么?这不是统一代码,这是如何启动网络应用程序。。。无论如何,您使用的是
te
,它是静态方法(即奇怪的
Main
方法)中的一个实例变量。你不允许那样做。根据定义,静态方法不是实例的一部分,因此它不必访问它。
static void Main
在那里做什么?这不是统一代码,这是如何启动网络应用程序。。。无论如何,您使用的是
te
,它是静态方法(即奇怪的
Main
方法)中的一个实例变量。你不允许那样做。根据定义,静态方法不是实例的一部分,因此它不是
 using UnityEngine;
    using System;
    using System.Collections;
    using System.Collections.Generic;

    public delegate void Task();

    public class TaskExecutorScript : MonoBehaviour {

        private Queue<Task> TaskQueue = new Queue<Task>();
        private object _queueLock = new object();

        // Update is called once per frame
        void Update () {
            lock (_queueLock)
            {
                if (TaskQueue.Count > 0)
                    TaskQueue.Dequeue()();
            }
        }

        public void ScheduleTask(Task newTask)
        {
            lock (_queueLock)
            {
                if (TaskQueue.Count < 100)
                    TaskQueue.Enqueue(newTask);
            }
        }
    }