ScreenProjectionView.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using SmartBowLib;
  6. using SmartBowLib.lebo;
  7. using Newtonsoft.Json.Linq;
  8. /*
  9. * 投屏功能的用户操作界面
  10. */
  11. public class ScreenProjectionView : MonoBehaviour, MenuBackInterface
  12. {
  13. //stage1
  14. public Transform uiDeviceListContent;
  15. public GameObject uiDeviceListItem;
  16. public Button btnResolution;
  17. //stage2
  18. public Text textDeviceLabel;
  19. public Button btnStop;
  20. private int _stageNum = 1;
  21. public static ScreenProjectionView ins;
  22. void Awake() {
  23. ins = this;
  24. DontDestroyOnLoad(this);
  25. uiDeviceListItem.SetActive(false);
  26. }
  27. void Start() {
  28. InitBtn_SetResolution();
  29. btnStop.onClick.AddListener(OnClick_BtnStop);
  30. MsgReceiver.ins.onMessage += onMessage;
  31. #if UNITY_ANDROID && !UNITY_EDITOR
  32. ScreenProjection.Init();
  33. if (!ScreenProjection.ins) _needReinitSDK = true;
  34. #endif
  35. }
  36. void OnDestroy()
  37. {
  38. if (ins == this) ins = null;
  39. MsgReceiver.ins.onMessage -= onMessage;
  40. }
  41. void OnEnable() {
  42. PersistenHandler.ins?.menuBackCtr.views.Add(this);
  43. }
  44. void OnDisable() {
  45. PersistenHandler.ins?.menuBackCtr.views.Remove(this);
  46. }
  47. public bool OnMenuBack() {
  48. LogicBtnBack();
  49. return true;
  50. }
  51. void onMessage(string tag, string msg) {
  52. if (tag == "lebo.onBrowse") OnBrowse(JArray.Parse(msg));
  53. }
  54. class LeboDeviceServiceInfo {
  55. public string uid;
  56. public string name;
  57. public GameObject o;
  58. public bool valid;
  59. }
  60. Dictionary<string, LeboDeviceServiceInfo> deviceMap = new Dictionary<string, LeboDeviceServiceInfo>();
  61. void OnBrowse(JArray list) {
  62. foreach (var item in deviceMap) { item.Value.valid = false; }
  63. foreach (var jo in list) {
  64. string uid = jo.Value<string>("uid");
  65. string name = jo.Value<string>("name");
  66. if (deviceMap.ContainsKey(uid)) {
  67. deviceMap[uid].valid = true;
  68. continue;
  69. }
  70. var info = new LeboDeviceServiceInfo();
  71. info.uid = uid;
  72. info.name = name;
  73. GameObject o = Instantiate(uiDeviceListItem, uiDeviceListContent);
  74. o.GetComponentInChildren<Text>().text = name;
  75. o.GetComponent<Button>().onClick.AddListener(() => {
  76. AudioMgr.ins.PlayBtn();
  77. ChangeStage(2);
  78. textDeviceLabel.text = name + $" <color=#00FF00>{btnResolution.GetComponentInChildren<Text>().text}</color> ";
  79. ScreenProjection.ins.Connect(uid, name);
  80. ScreenProjection.ins.StopBrowse();
  81. });
  82. o.SetActive(true);
  83. info.o = o;
  84. info.valid = true;
  85. deviceMap[uid] = info;
  86. }
  87. List<string> willDels = null;
  88. foreach (var item in deviceMap) {
  89. if (!item.Value.valid) {
  90. if (willDels == null) willDels = new List<string>();
  91. willDels.Add(item.Value.uid);
  92. };
  93. }
  94. if (willDels != null) {
  95. foreach (var mapkey in willDels) {
  96. Destroy(deviceMap[mapkey].o);
  97. deviceMap.Remove(mapkey);
  98. }
  99. }
  100. }
  101. void InitBtn_SetResolution() {
  102. btnResolution.onClick.AddListener(() => {
  103. AudioMgr.ins.PlayBtn();
  104. ScreenProjection.mirrorResolutionID = ScreenProjection.mirrorResolutionID % 2 + 1;
  105. RefreshBtn_SetResolution();
  106. });
  107. RefreshBtn_SetResolution();
  108. }
  109. void RefreshBtn_SetResolution() {
  110. if (ScreenProjection.mirrorResolutionID == 1) {
  111. btnResolution.GetComponentInChildren<Text>().text = "720P";
  112. } else if (ScreenProjection.mirrorResolutionID == 2) {
  113. btnResolution.GetComponentInChildren<Text>().text = "1080P";
  114. }
  115. }
  116. public void OnClick_BtnBack() {
  117. AudioMgr.ins.PlayBtn();
  118. LogicBtnBack();
  119. }
  120. void LogicBtnBack() {
  121. if (ScreenProjection.ins && ScreenProjection.ins.HasDeviceInConnect()) {
  122. gameObject.SetActive(false);
  123. } else {
  124. ScreenProjection.Release();
  125. Destroy(gameObject);
  126. }
  127. }
  128. void ChangeStage(int stage) {
  129. _stageNum = stage;
  130. if (_stageNum == 1) {
  131. transform.Find("Stage1").gameObject.SetActive(true);
  132. transform.Find("Stage2").gameObject.SetActive(false);
  133. OnBrowse(new JArray());
  134. } else if (_stageNum == 2) {
  135. transform.Find("Stage1").gameObject.SetActive(false);
  136. transform.Find("Stage2").gameObject.SetActive(true);
  137. }
  138. }
  139. void OnClick_BtnStop() {
  140. AudioMgr.ins.PlayBtn();
  141. ScreenProjection.Release();
  142. ChangeStage(1);
  143. _needReinitSDK = true;
  144. }
  145. bool _needReinitSDK = false;
  146. void Update() {
  147. if (_needReinitSDK) {
  148. ScreenProjection.Init();
  149. if (ScreenProjection.ins) _needReinitSDK = false;
  150. }
  151. }
  152. bool _pwdInited = false;
  153. InputField _pwdInputField;
  154. public void ShowInputPWD() {
  155. if (!_pwdInited) {
  156. _pwdInited = true;
  157. _pwdInputField = transform.Find("PWD/InputField").GetComponent<InputField>();
  158. transform.Find("PWD/BtnEnter").GetComponent<Button>().onClick.AddListener(OnClick_InputPWDEnter);
  159. }
  160. _pwdInputField.text = "";
  161. transform.Find("PWD").gameObject.SetActive(true);
  162. }
  163. void OnClick_InputPWDEnter() {
  164. AudioMgr.ins.PlayBtn();
  165. transform.Find("PWD").gameObject.SetActive(false);
  166. ScreenProjection.ins.setPassword(_pwdInputField.text);
  167. ScreenProjection.ins.StartMirror();
  168. }
  169. }