2016-05-10 13 views
2

キューブオブジェクトを作成し、このスクリプトを添付しました。ユニティメソッドでUDPを使用する方法

using UnityEngine; 
using System.Collections; 

public class CubeMove : MonoBehaviour { 
    void Start() { 
    } 

    void Update() { 
    } 

    public void Move() { 
     Vector3 moveVector = new Vector3(10, 0, 0); 
     transform.Translate(moveVector); 
    } 
} 

UDPを使用してキューブの移動を制御したいので、UDPManagerを作成しました。

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

public class UDPManager : MonoBehaviour 
{ 
    static UdpClient udp; 
    Thread thread; 

    public GameObject cube; 
    public CubeMove cubemove; 

    void Start() 
    { 
     udp = new UdpClient(12345); 
     cubemove = cube.GetComponent<CubeMove>(); 
     thread = new Thread(new ThreadStart(ThreadMethod)); 
     thread.Start(); 
    } 

    void Update() 
    { 
    } 

    void OnApplicationQuit() 
    { 
     udp.Close(); 
     thread.Abort(); 
    } 

    private void ThreadMethod() 
    { 
     while(true) 
     { 
      IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0); 

      byte[] receiveBytes = udp.Receive(ref RemoteIpEndPoint); 
      string returnData = Encoding.ASCII.GetString(receiveBytes); 
      Debug.Log(returnData); 
      if (returnData == "1\n") { 
       cube.SendMessage ("Move"); 
       // or 
       cubemove.Move(); 

      } 
     } 
    } 
} 

ただし、これらは以下のエラーでは機能しません。

- SendMessage can only be called from the main thread. 
- get_transform can only be called from the main thread. 

udpコマンドを受け取ったときにユニティメソッドを呼び出すことはできますか?

答えて

6

Unity APIを別のスレッドから呼び出すことはできません。 Unityのスレッドの使い方:

開始スレッド

新しいスレッドのプロセス入力

あなたに処理が完了したことをUnityに伝えます。これを行うには、グローバルboolean変数をtrueに設定します。 outputのデータをもう1つグローバルの変数に格納します。

Update()機能でboolean変数が変更されていないか確認してください。完了した場合はfalseに設定します。処理出力...

udp = new UdpClient(12345);Start機能からThreadMethod機能に移動します。

static readonly object lockObject = new object(); 
string returnData = ""; 
bool precessData = false; 

void Start() 
{ 
    cubemove = cube.GetComponent<CubeMove>(); 
    thread = new Thread(new ThreadStart(ThreadMethod)); 
    thread.Start(); 
} 

void Update() 
{ 
    if (precessData) 
    { 
     /*lock object to make sure there data is 
     *not being accessed from multiple threads at thesame time*/ 
     lock (lockObject) 
     { 
      precessData = false; 
      cube.SendMessage("Move"); 
      // or 
      cubemove.Move(); 

      //Process received data 
      Debug.Log("Received: " + returnData); 

      //Reset it for next read(OPTIONAL) 
      returnData = ""; 
     } 
    } 
} 

private void ThreadMethod() 
{ 
    udp = new UdpClient(12345); 
    while (true) 
    { 
     IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0); 

     byte[] receiveBytes = udp.Receive(ref RemoteIpEndPoint); 

     /*lock object to make sure there data is 
     *not being accessed from multiple threads at thesame time*/ 
     lock (lockObject) 
     { 
      returnData = Encoding.ASCII.GetString(receiveBytes); 

      Debug.Log(returnData); 
      if (returnData == "1\n") 
      { 
       //Done, notify the Update function 
       precessData = true; 
      } 
     } 
    } 
} 
+0

ありがとうございます!私はそれをすることができた。 – uzimith

2
using UnityEngine; 
using System.Net.Sockets; 
using System.Net; 
using System.Text; 
using System; 

public class UDPRT: ScriptableObject { 
static public string ReceivedMsg; // INPUT DATA 
static private UdpClient udpc; 
static IPEndPoint IP; 
static private object obj; 
static private AsyncCallback AC; 
static byte[] DATA; 

public static UDPRT CreateInstance(int Port) // RECEVE UDP 
    { 
    IP = new IPEndPoint(IPAddress.Any, Port); 
    udpc = new UdpClient(Port); 
    AC = new AsyncCallback(ReceiveIt); 
    StartUdpReceive(); 
    return ScriptableObject.CreateInstance <UDPRT>(); 
    } 

public static UDPRT CreateInstance(int Port, string Host, string msg) // SEND UDP 
    { 
    udpc = new UdpClient(Host, Port); 
    AC = new AsyncCallback(SendIt); 
    byte[] data = Encoding.UTF8.GetBytes(msg); 
    udpc.BeginSend(data, data.Length, AC, obj); 
    return ScriptableObject.CreateInstance <UDPRT>(); 
    } 

static void ReceiveIt(IAsyncResult result) { 
    DATA = (udpc.EndReceive(result, ref IP)); 
    Debug.Log(Encoding.UTF8.GetString(DATA)); 
    ReceivedMsg = Encoding.UTF8.GetString(DATA); 
    StartUdpReceive(); 
} 

static void SendIt(IAsyncResult result) { 
    udpc.EndSend(result); 
} 


static void StartUdpReceive() { 
    udpc.BeginReceive(AC, obj); 
} 
} 
関連する問題