PlayCast.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. using LitJson;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.Runtime.InteropServices;
  6. using UnityEngine;
  7. using UnityEngine.UI;
  8. public class PlayCast : MonoBehaviour
  9. {
  10. [DllImport("NativeCameraPlugin")]
  11. public static extern void SetTextureFromUnity(System.IntPtr texture);
  12. [DllImport("NativeCameraPlugin")]
  13. private static extern IntPtr GetRenderEventFunc();
  14. private AndroidJavaClass jc;
  15. public static PlayCast Instance;
  16. private AndroidJavaObject CurrentActivity;
  17. // Start is called before the first frame update
  18. void Start()
  19. {
  20. Instance = this;
  21. Application.targetFrameRate = 60;
  22. jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
  23. CurrentActivity = jc.GetStatic<AndroidJavaObject>("currentActivity");
  24. }
  25. // public RenderTexture renderTexture;
  26. public int TextureID;
  27. SDKCallbackListener aaa;
  28. // WebSocketClient wc;
  29. public TClient tcp;
  30. public void init(string ip)
  31. {
  32. isChoose = false;
  33. if (tcp!=null)
  34. tcp.OnMessaged -= scoketMessage;
  35. CurrentActivity.Call("closeTou");
  36. Invoke("initStart", 1f);
  37. TClient.Instance.InitSocket(ip);
  38. }
  39. void initStart()
  40. {
  41. //if(wc==null)
  42. // {
  43. aaa = new SDKCallbackListener();
  44. CurrentActivity.Call("initSDK2", aaa);
  45. // wc = WebSocketClient.webSockets["castScreen"];
  46. tcp.OnMessaged += scoketMessage;
  47. CurrentActivity.Call("InitTouPin", "172.16.15.42", "3300", "test3");
  48. // }
  49. Invoke("StartCast", 1f);
  50. }
  51. public void scoketMessage(string msg)
  52. {
  53. Debug.Log(msg + "scoketMessage接收到的信息");
  54. CurrentActivity.Call("onMessage", msg);
  55. }
  56. public RawImage rimg;
  57. private IEnumerator CallPluginAtEndOfFrames()
  58. {
  59. while (true)
  60. {
  61. if (TextureID != 0)
  62. {
  63. // float t = Time.time;
  64. GL.IssuePluginEvent(GetRenderEventFunc(), TextureID);
  65. // UnityEngine.Debug.Log(string.Format("totalUnity消耗时间: {0} ms", Time.time - t));
  66. }
  67. // skip one frame
  68. yield return new WaitForSeconds(1f / 60);
  69. }
  70. }
  71. Texture2D tex;
  72. public void StartCast()
  73. {
  74. StopCoroutine("CallPluginAtEndOfFrames");
  75. StartCoroutine("CallPluginAtEndOfFrames");
  76. int width = 1280;
  77. int height = 720;
  78. tex = new Texture2D(width, height, TextureFormat.RGBA32, false);
  79. rimg.texture = tex;
  80. TextureID = tex.GetNativeTexturePtr().ToInt32();
  81. SetTextureFromUnity(tex.GetNativeTexturePtr());
  82. CurrentActivity.Call("jieshou", "0001", tex.GetNativeTexturePtr().ToInt32(), isdiyi);
  83. isChoose = true;
  84. }
  85. public void closeSocket()
  86. {
  87. CurrentActivity.Call("closeTou");
  88. TClient.Instance.SocketQuit();
  89. }
  90. bool isdiyi;
  91. public void qiehuan()
  92. {
  93. isChoose = true;
  94. isdiyi = !isdiyi;
  95. CurrentActivity.Call("changeShijiao", isdiyi);
  96. }
  97. bool isChoose = false;
  98. public Transform tf;
  99. // Update is called once per frame
  100. public Camera ca2;
  101. JsonData data = new JsonData();
  102. JsonData data2 = new JsonData();
  103. void Update()
  104. {
  105. if(aaa!=null)
  106. {
  107. if (aaa.data != null && aaa.data != "")
  108. {
  109. JsonData data = JsonMapper.ToObject(aaa.data);
  110. if (data["action"].ToString() == "changeResolution")
  111. {
  112. Debug.Log("更改action");
  113. int width = int.Parse(data["data"]["w"].ToString());
  114. int height = int.Parse(data["data"]["h"].ToString());
  115. tex = new Texture2D(width, height, TextureFormat.RGBA32, false);
  116. TextureID = tex.GetNativeTexturePtr().ToInt32();
  117. CurrentActivity.Call("changeTexturID", tex.GetNativeTexturePtr().ToInt32());
  118. rimg.texture = tex;
  119. }
  120. aaa.data = "";
  121. }
  122. if (isChoose)
  123. {
  124. if(isdiyi)
  125. {
  126. data["action"] = "firstView";
  127. data["data"] = data2;
  128. data2["info"] = "";
  129. TClient.Instance.SocketSend(data.ToJson());
  130. }
  131. else if(!isdiyi)
  132. {
  133. data["action"] = "thirdView";
  134. data["data"] = data2;
  135. data2["info"] = tf.position.x + "_" + tf.position.y + "_" + tf.position.z + "_" + tf.eulerAngles.x + "_" + tf.eulerAngles.y + "_" + tf.eulerAngles.z;
  136. TClient.Instance.SocketSend(data.ToJson());
  137. }
  138. }
  139. }
  140. }
  141. }