CastScreen.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. //WebSocketClient wc;
  28. public void init()
  29. {
  30. tcp.InitSocket();
  31. renderTexture = new RenderTexture(1280, 720, 1);
  32. ca.targetTexture = renderTexture;
  33. aaa = new SDKCallbackListener();
  34. CurrentActivity.Call("initSDK2", aaa);
  35. // wc = WebSocketClient.webSockets["castScreen"];
  36. // wc.OnMessaged += scoketMessage;
  37. tcp.OnMessaged += scoketMessage;
  38. CurrentActivity.Call("InitTouPin", "192.168.50.189", "50001", "test2");
  39. Debug.Log("连接init");
  40. Invoke("StartCast",1f);
  41. }
  42. public void close()
  43. {
  44. CurrentActivity.Call("closeTou");
  45. TClient.Instance.SocketQuit();
  46. }
  47. public void scoketMessage(string msg)
  48. {
  49. Debug.Log(msg+ "scoketMessage接收到的信息=====<");
  50. //textMesh.text = msg;
  51. CurrentActivity.Call("onMessage", msg);
  52. try
  53. {
  54. JsonData data = JsonMapper.ToObject(msg);
  55. {
  56. if (data["type"].ToString() == "View")
  57. {
  58. sendInfo = data["data"];
  59. ChangeView(sendInfo);
  60. }
  61. }
  62. }
  63. catch
  64. {
  65. }
  66. }
  67. //public TextMesh textMesh;
  68. private IEnumerator CallPluginAtEndOfFrames()
  69. {
  70. while (true)
  71. {
  72. if (TextureID != 0)
  73. {
  74. // RenderTexture.active = renderTexture;
  75. // float t = Time.time;
  76. GL.IssuePluginEvent(GetLocalEventFunc(), TextureID);
  77. GL.Clear(true, true,new Color(0,0,0,0));
  78. // UnityEngine.Debug.Log(string.Format("totalUnity消耗时间: {0} ms", Time.time - t));
  79. }
  80. // skip one frame
  81. yield return new WaitForSeconds(1f / 60);
  82. }
  83. }
  84. public MeshRenderer mr;
  85. public void StartCast()
  86. {
  87. StopCoroutine("CallPluginAtEndOfFrames");
  88. StartCoroutine("CallPluginAtEndOfFrames");
  89. int width = renderTexture.width;
  90. int height = renderTexture.height;
  91. TextureID = renderTexture.GetNativeTexturePtr().ToInt32();
  92. mr.material = (Material)Resources.Load("renderMat");
  93. mr.material.mainTexture = renderTexture;
  94. SetTextureLocal(renderTexture.GetNativeTexturePtr());
  95. CurrentActivity.Call("touping", "0001", renderTexture.GetNativeTexturePtr().ToInt32(), width, height);
  96. // CurrentActivity.Call("changeVideo", width, height, 60);
  97. Debug.Log("房间开启。。。");
  98. }
  99. public void changeSize()
  100. {
  101. if (renderTexture != null)
  102. renderTexture.Release();
  103. renderTexture = new RenderTexture(1280, 720,1);
  104. ca.targetTexture = renderTexture;
  105. mr.material.mainTexture = renderTexture;
  106. int width = renderTexture.width;
  107. int height = renderTexture.height;
  108. TextureID = renderTexture.GetNativeTexturePtr().ToInt32();
  109. CurrentActivity.Call("changeSize", width, height, TextureID);
  110. }
  111. public Transform tf;
  112. JsonData sendInfo;
  113. JsonData sendInfo2;
  114. public GameObject backGround;
  115. // Update is called once per frame
  116. void Update()
  117. {
  118. }
  119. private void ChangeView(JsonData sendInfo)
  120. {
  121. if (sendInfo != null)
  122. {
  123. JsonData data = sendInfo;
  124. Debug.Log(sendInfo.ToString()+"%%%%%");
  125. if (data["method"].ToString() == "firstView")
  126. {
  127. Debug.Log("第一视角:");
  128. backGround.SetActive(true);
  129. tf.position = GSXRManager.Instance.head.position;
  130. tf.eulerAngles = GSXRManager.Instance.head.eulerAngles;
  131. }
  132. else if (data["method"].ToString() == "thirdView")
  133. {
  134. Debug.Log("第三视角:");
  135. backGround.SetActive(false);
  136. string[] strs = data["pose"].ToString().Split('_');
  137. tf.position = new Vector3(float.Parse(strs[0]), float.Parse(strs[1]), float.Parse(strs[2]));
  138. tf.eulerAngles = new Vector3(float.Parse(strs[3]), float.Parse(strs[4]), float.Parse(strs[5]));
  139. }
  140. }
  141. }
  142. }