public void OnDisable() { if (instance == this) { instance = null; } }
// Start is called before the first frame update public void initiate() { DontDestroyOnLoad(this); UnityThread.initUnityThread(); ClientHandleData.InizializzaPacchetti(); ClientTcp.InizializzaNetwork(); }
//Used to initialize UnityThread. Call once before any function here public static void initUnityThread(bool visible = false) { if (instance != null) { return; } if (Application.isPlaying) { // add an invisible game object to the scene GameObject obj = new GameObject("MainThreadExecuter"); if (!visible) { obj.hideFlags = HideFlags.HideAndDontSave; } DontDestroyOnLoad(obj); instance = obj.AddComponent <UnityThread>(); } }
private static void ReceiveCallBack(IAsyncResult result) { try { int length = myStream.EndRead(result); if (length <= 0) { return; } byte[] newbytes = new byte[length]; Array.Copy(recBuffer, newbytes, length); UnityThread.executeInFixedUpdate(() => { ClientHandleData.HandleData(newbytes); }); myStream.BeginRead(recBuffer, 0, 4096 * 2, ReceiveCallBack, null); } catch (Exception) { Debug.Log("Error!"); } }