Unity3D中程序后台运行,我原本认为当Application.runInBackground=false时,Socket保持连接,但是不接受数据,经过测试并非如此,当Application.runInBackground=false时,同样可以接受来自Socket发送的消息。下面是我测试的代码:
UnitySocket clientSocket; string message = ""; // Use this for initialization void Start () { XmlHelper helper = new XmlHelper(); helper.LoadData(Application.dataPath+"/"+"Config.xml"); clientSocket=new UnitySocket(); clientSocket.OnDataByteIn += new UnitySocket.delDataByteIn(clientSocket_OnDataByteIn); } void clientSocket_OnDataByteIn(string data) { Debug.Log("Unity3d Rev:"+data); message = data; if (data == "aaa") { Application.runInBackground = false; } else if (data == "bbb") { Application.runInBackground = true; } } // Update is called once per frame void Update () { } void OnGUI() { GUI.Label(new Rect(0,0,200,30),message); }
测试结果:
Unity3d默认设置runInBackground=false开启Unity3D 给socket发送数据,unity3d的OnGUI没显示任何内容,但是Socket是有数据产生的。只是OnGUI暂时停止了工作,
当我发送bbb到应用程序时:
GUI有内容显示了:
说明后台还是可以处理数据的。
转载于:https://www.cnblogs.com/WilliamJiang/archive/2013/04/02/2994901.html