Raspberry Pi to Unity:基于UDP数据移动对象

时间:2015-07-24 13:55:35

标签: c# unity3d udp

我已将Raspberry Pi链接到Unity,并以字符串形式接收数据" xxx yyy"其中xxx是3位x位置,yyy是3位数y位置。我目前从raspberry pi获取信息写入Unity控制台,但我的代码没有正确地从控制台读取该信息。我该如何解决?以下是与此问题相关的两个脚本:

using System;
using UnityEngine;
using System.Collections;

public class MoveBox : MonoBehaviour {

    //string data;
    string[] split_data;
    int x;
    int y;

    // Use this for initialization
    void Start () {


    }

    // Update is called once per frame
    void Update () {

        string data = Console.ReadLine ();


        split_data = data.Split(' ');
        x = Int32.Parse(split_data [0]);
        y = Int32.Parse(split_data [1]);
        transform.position = new Vector3(x, y, 0);


    }
}

using UnityEngine;
using System.Collections;

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

public class UDPRecieve : MonoBehaviour {

    /*

    -----------------------
    UDP-Receive (send to)
    -----------------------
    // [url]http://msdn.microsoft.com/de-de/library/bb979228.aspx#ID0E3BAC[/url]


    // > receive
    // 128.235.117.235 : 8051

    // send
    // nc -u 128.235.117.235 8051

*/
        // receiving Thread
        Thread receiveThread;

        // udpclient object
        UdpClient client;

        // public
        // public string IP = "128.235.117.235"; default local
        public int port; // define > init

        public static GameObject cube;


        // infos
        public string lastReceivedUDPPacket="";
        public string allReceivedUDPPackets=""; // clean up this from time to time!

        private string info;
        public static int x;
        public static int y;
        private string[] arraySplit; 



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


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

            string text="";
            do
            {
                text = Console.ReadLine();
                cube.transform.position = new Vector3 (x, y, 0);
            }
            while(!text.Equals("exit"));


        }
        // start from unity3d
        public void Start()
        {

            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\n128.235.117.235 "+port+" #\n"
                    + "shell> nc -u 128.235.117.235 : "+port+" \n"
                    + "\nLast Packet: \n"+ lastReceivedUDPPacket
                    + "\n\nAll Messages: \n"+allReceivedUDPPackets
                    ,style);
        }

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

            // define port
            port = 6666;  

            // status
            print("Sending to 128.235.117.235 : "+port);
            print("Test-Sending to this Port: nc -u 128.235.117.235  "+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);

                    arraySplit = text.Split(' ');
                    x = Int32.Parse(arraySplit [0]);
                    y = Int32.Parse(arraySplit [1]);
                    //this.transform.position = new Vector3 (x, y, 0);

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

                    // latest UDPpacket
                    lastReceivedUDPPacket=text;

                    // ....
                    allReceivedUDPPackets=allReceivedUDPPackets+text;

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

        // getLatestUDPPacket
        // cleans up the rest
        public string getLatestUDPPacket()
        {
            allReceivedUDPPackets="";
            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(); 
    } 
}

0 个答案:

没有答案