CastScreen.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. using LitJson;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.Runtime.InteropServices;
  6. using UnityEngine;
  7. public class CastScreen : MonoBehaviour
  8. {
  9. [DllImport("NativeCameraPlugin")]
  10. public static extern void SetTextureLocal(System.IntPtr texture);
  11. [DllImport("NativeCameraPlugin")]
  12. private static extern IntPtr GetLocalEventFunc();
  13. private AndroidJavaClass jc;
  14. public Camera ca;
  15. private AndroidJavaObject CurrentActivity;
  16. // Start is called before the first frame update
  17. void Start()
  18. {
  19. jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
  20. CurrentActivity = jc.GetStatic<AndroidJavaObject>("currentActivity");
  21. Invoke("init",1f);
  22. }
  23. private RenderTexture renderTexture;
  24. public int TextureID;
  25. SDKCallbackListener aaa;
  26. public TClient tcp;
  27. //public TextMesh fpsobj;
  28. //WebSocketClient wc;
  29. public void init()
  30. {
  31. #if UNITY_ANDROID && !UNITY_EDITOR
  32. tcp.InitSocket();
  33. renderTexture = new RenderTexture(CustomInfo.mWidth, CustomInfo.mHight, 1);
  34. ca.targetTexture = renderTexture;
  35. aaa = new SDKCallbackListener();
  36. CurrentActivity.Call("initSDK2", aaa);
  37. // wc = WebSocketClient.webSockets["castScreen"];
  38. // wc.OnMessaged += scoketMessage;
  39. tcp.OnMessaged += scoketMessage;
  40. //CurrentActivity.Call("InitTouPin", "test2");
  41. // Debug.Log("连接init");
  42. // Invoke("StartCast",1f);
  43. #endif
  44. }
  45. public void close()
  46. {
  47. CurrentActivity.Call("closeTou");
  48. TClient.Instance.SocketQuit();
  49. }
  50. public bool isInit;
  51. public void scoketMessage(string msg)
  52. {
  53. Debug.Log(msg+ "scoketMessage接收到的信息=====<[lgs]");
  54. //textMesh.text = msg;
  55. CurrentActivity.Call("onMessage", msg);
  56. try
  57. {
  58. JsonData data = JsonMapper.ToObject(msg);
  59. {
  60. if (data["type"].ToString() == "View")
  61. {
  62. sendInfo = data["data"];
  63. ChangeView(sendInfo);
  64. }
  65. else if (data["type"].ToString() == "RePos")
  66. {
  67. Debug.Log("[lgs] " + data.ToString());
  68. ReLocation();
  69. }
  70. else if (data["type"].ToString() == "init")
  71. {
  72. CurrentActivity.Call("InitTouPin", "test2");
  73. Debug.Log("连接init");
  74. Invoke("StartCast", 1f);
  75. }
  76. else
  77. {
  78. }
  79. }
  80. }
  81. catch
  82. {
  83. }
  84. }
  85. //public TextMesh textMesh;
  86. private IEnumerator CallPluginAtEndOfFrames()
  87. {
  88. while (true)
  89. {
  90. if (TextureID != 0)
  91. {
  92. // RenderTexture.active = renderTexture;
  93. // float t = Time.time;
  94. GL.IssuePluginEvent(GetLocalEventFunc(), TextureID);
  95. // UnityEngine.Debug.Log(string.Format("totalUnity消耗时间: {0} ms", Time.time - t));
  96. }
  97. // skip one frame
  98. yield return new WaitForSeconds(0.013f);
  99. }
  100. }
  101. public MeshRenderer mr;
  102. public void StartCast()
  103. {
  104. StopCoroutine("CallPluginAtEndOfFrames");
  105. StartCoroutine("CallPluginAtEndOfFrames");
  106. int width = renderTexture.width;
  107. int height = renderTexture.height;
  108. TextureID = renderTexture.GetNativeTexturePtr().ToInt32();
  109. mr.material = (Material)Resources.Load("renderMat");
  110. mr.material.mainTexture = renderTexture;
  111. SetTextureLocal(renderTexture.GetNativeTexturePtr());
  112. CurrentActivity.Call("touping", "0001", renderTexture.GetNativeTexturePtr().ToInt32(), width, height);
  113. // CurrentActivity.Call("changeVideo", width, height, 60);
  114. Debug.Log("房间开启。。。");
  115. JsonData initTcp = new JsonData();
  116. initTcp["action"] = "init";
  117. initTcp["data"] = "init";
  118. TClient.Instance.SocketSend(initTcp.ToJson());
  119. }
  120. public void changeSize()
  121. {
  122. if (renderTexture != null)
  123. renderTexture.Release();
  124. renderTexture = new RenderTexture(CustomInfo.mWidth, CustomInfo.mHight, 1);
  125. ca.targetTexture = renderTexture;
  126. mr.material.mainTexture = renderTexture;
  127. int width = renderTexture.width;
  128. int height = renderTexture.height;
  129. TextureID = renderTexture.GetNativeTexturePtr().ToInt32();
  130. CurrentActivity.Call("changeSize", width, height, TextureID);
  131. }
  132. public Transform tf;
  133. JsonData sendInfo;
  134. JsonData sendInfo2;
  135. JsonData data = new JsonData();
  136. JsonData data2 = new JsonData();
  137. public GameObject backGround;
  138. public List<Vector3> poslist = new List<Vector3>();
  139. public List<Vector3> eullist = new List<Vector3>();
  140. private int delay = 7;
  141. // Update is called once per frame
  142. void Update()
  143. {
  144. poslist.Add(SvrManager.Instance.head.position);
  145. eullist.Add(SvrManager.Instance.head.eulerAngles);
  146. if(poslist.Count > delay)
  147. {
  148. poslist.RemoveAt(0);
  149. eullist.RemoveAt(0);
  150. }
  151. }
  152. private void ChangeView(JsonData sendInfo)
  153. {
  154. if (sendInfo != null)
  155. {
  156. JsonData data = sendInfo;
  157. if (data["method"].ToString() == "firstView")
  158. {
  159. backGround.SetActive(true);
  160. tf.position = poslist[0];
  161. ca.transform.localRotation =Quaternion.Euler(-10,0,0);
  162. tf.eulerAngles = eullist[0];
  163. }
  164. else if (data["method"].ToString() == "thirdView")
  165. {
  166. backGround.SetActive(false);
  167. ca.transform.localRotation = Quaternion.Euler(0, 0, 0);
  168. string[] strs = data["pose"].ToString().Split('_');
  169. tf.position = new Vector3(float.Parse(strs[0]), float.Parse(strs[1]), float.Parse(strs[2]));
  170. tf.eulerAngles = new Vector3(float.Parse(strs[3]), float.Parse(strs[4]), float.Parse(strs[5]));
  171. //fpsobj.text = data["fps"].ToString();
  172. }
  173. }
  174. }
  175. private void ReLocation()
  176. {
  177. data["action"] = "Relocation";
  178. data["data"] = data2;
  179. data2["info"] = SvrManager.Instance.head.position.x + "_" + SvrManager.Instance.head.position.y + "_" + SvrManager.Instance.head.position.z + "_" + SvrManager.Instance.head.eulerAngles.x + "_" + SvrManager.Instance.head.eulerAngles.y + "_" + SvrManager.Instance.head.eulerAngles.z;
  180. TClient.Instance.SocketSend(data.ToJson());
  181. Debug.Log("[lgs] sendPos"+data.ToString());
  182. }
  183. private void OnDestroy()
  184. {
  185. }
  186. }