我使用树莓派通过UDP向Unity发送信息。为了规避Unity的多线程问题,我在一部分脚本中委派了一个任务。我收到错误声明:
"错误CS0120:访问非静态成员所需的对象引用`UDPReceive.te'"
如何使用此任务委派技术正确发送信息?我有覆盆子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();
}
}
和
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);
}
}
}