ScreenProjection.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. namespace SmartBowLib.lebo
  6. {
  7. /*
  8. * 投屏工具
  9. */
  10. public class ScreenProjection : MonoBehaviour
  11. {
  12. //单例
  13. public static ScreenProjection ins;
  14. private static float _lastReleaseTime = -100; //上次销毁是什么时候
  15. //初始化-单例
  16. public static void Init() {
  17. if (ins) return;
  18. if (Time.realtimeSinceStartup - _lastReleaseTime < 0.5) return;
  19. ins = new GameObject("ScreenProjection").AddComponent<ScreenProjection>();
  20. DontDestroyOnLoad(ins);
  21. }
  22. private bool _released = false;
  23. //释放(销毁实例)
  24. public static void Release() {
  25. if (ins) {
  26. _lastReleaseTime = Time.realtimeSinceStartup;
  27. ins._released = true;
  28. Destroy(ins.gameObject);
  29. ins = null;
  30. }
  31. }
  32. void OnDestroy()
  33. {
  34. _lastReleaseTime = Time.realtimeSinceStartup;
  35. MsgReceiver.ins.onMessage -= onMessage;
  36. UnbindSDK();
  37. if (_objectScreenProjection != null) {
  38. // _objectScreenProjection.Call
  39. _objectScreenProjection.Dispose();
  40. }
  41. if (_classUnityPlayer != null) {
  42. _objectScreenProjection.Dispose();
  43. }
  44. if (_currentActivity != null) {
  45. _currentActivity.Dispose();
  46. }
  47. }
  48. void Start() {
  49. MsgReceiver.ins.onMessage += onMessage;
  50. InitSDK();
  51. }
  52. AndroidJavaObject _objectScreenProjection;
  53. AndroidJavaClass _classUnityPlayer;
  54. AndroidJavaObject _currentActivity;
  55. private bool _hasCallInitedSDK;
  56. void InitSDK() {
  57. MsgReceiver.ins.Log("在unity中初始化");
  58. _objectScreenProjection = new AndroidJavaObject("com.example.smartbowlib.lebo.ScreenProjection");
  59. _classUnityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
  60. _currentActivity = _classUnityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
  61. _hasCallInitedSDK = true;
  62. _objectScreenProjection.Call("initSDK", _currentActivity);
  63. }
  64. void UnbindSDK() {
  65. if (_hasCallInitedSDK) {
  66. _objectScreenProjection.Call("unBindSdk");
  67. }
  68. }
  69. bool _browsing = false;
  70. public void StartBrowse() {
  71. if (!_authed) return;
  72. if (_browsing) return;
  73. _browsing = true;
  74. _objectScreenProjection.Call("startBrowse");
  75. }
  76. public void StopBrowse() {
  77. if (_browsing) {
  78. _browsing = false;
  79. _objectScreenProjection.Call("stopBrowse");
  80. }
  81. }
  82. string[] _connectingDevice;
  83. public void Connect(string uid, string name) {
  84. _connectingDevice = new string[]{uid, name};
  85. _objectScreenProjection.Call("connect", uid);
  86. }
  87. public void setPassword(string text) {
  88. _objectScreenProjection.Call("setPassword", text);
  89. }
  90. public void StartMirror() {
  91. _objectScreenProjection.Call("startMirror", mirrorResolutionID);
  92. }
  93. public void GetConnectList() {
  94. _objectScreenProjection.Call("getConnectList");
  95. }
  96. void onMessage(string tag, string msg) {
  97. if (_released) return;
  98. if (tag == "lebo.onAuthSuccess") onAuthSuccess();
  99. else if (tag == "lebo.onConnect") onConnect();
  100. else if (tag == "lebo.onDisconnect") onDisconnect();
  101. else if (tag == "lebo.onStart") onStart();
  102. else if (tag == "lebo.onStop") onStop();
  103. else if (tag == "lebo.onError") onError(msg);
  104. else if (tag == "lebo.onBrowseStop") onBrowseStop();
  105. }
  106. bool _authed = false;
  107. void onAuthSuccess() {
  108. _authed = true;
  109. StartBrowse();
  110. }
  111. void onBrowseStop() {
  112. _browsing= false;
  113. if (!HasDeviceInConnect()) {
  114. StartBrowse();
  115. }
  116. }
  117. string[] _connectedDevice;
  118. public static int mirrorResolutionID = 2;
  119. void onConnect() {
  120. _connectedDevice = _connectingDevice;
  121. _connectingDevice = null;
  122. StartMirror();
  123. }
  124. void onDisconnect() {
  125. _connectedDevice = null;
  126. _connectingDevice = null;
  127. }
  128. void onStart() {
  129. }
  130. void onStop() {
  131. }
  132. void onError(string text) {
  133. PopupMgr.ins.ShowTipTop(text);
  134. if (text == "请输入密码") {
  135. ScreenProjectionView.ins?.ShowInputPWD();
  136. }
  137. }
  138. //判断
  139. public bool HasDeviceInConnect() {
  140. return _connectingDevice != null || _connectedDevice != null;
  141. }
  142. }
  143. }