ScreenProjectionView.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. uiDeviceListItem.SetActive(false);
  25. }
  26. void Start() {
  27. InitBtn_SetResolution();
  28. btnStop.onClick.AddListener(OnClick_BtnStop);
  29. MsgReceiver.ins.onMessage += onMessage;
  30. #if UNITY_ANDROID && !UNITY_EDITOR
  31. ScreenProjection.Init();
  32. if (!ScreenProjection.ins) _needReinitSDK = true;
  33. #endif
  34. }
  35. void OnDestroy()
  36. {
  37. if (ins == this) ins = null;
  38. MsgReceiver.ins.onMessage -= onMessage;
  39. }
  40. void OnEnable() {
  41. PersistenHandler.ins?.menuBackCtr.views.Add(this);
  42. }
  43. void OnDisable() {
  44. PersistenHandler.ins?.menuBackCtr.views.Remove(this);
  45. }
  46. public bool OnMenuBack() {
  47. LogicBtnBack();
  48. return true;
  49. }
  50. void onMessage(string tag, string msg) {
  51. if (tag == "lebo.onBrowse") OnBrowse(JArray.Parse(msg));
  52. }
  53. class LeboDeviceServiceInfo {
  54. public string uid;
  55. public string name;
  56. public GameObject o;
  57. public bool valid;
  58. }
  59. Dictionary<string, LeboDeviceServiceInfo> deviceMap = new Dictionary<string, LeboDeviceServiceInfo>();
  60. void OnBrowse(JArray list) {
  61. foreach (var item in deviceMap) { item.Value.valid = false; }
  62. foreach (var jo in list) {
  63. string uid = jo.Value<string>("uid");
  64. string name = jo.Value<string>("name");
  65. if (deviceMap.ContainsKey(uid)) {
  66. deviceMap[uid].valid = true;
  67. continue;
  68. }
  69. var info = new LeboDeviceServiceInfo();
  70. info.uid = uid;
  71. info.name = name;
  72. GameObject o = Instantiate(uiDeviceListItem, uiDeviceListContent);
  73. o.GetComponentInChildren<Text>().text = name;
  74. o.GetComponent<Button>().onClick.AddListener(() => {
  75. AudioMgr.ins.PlayBtn();
  76. ChangeStage(2);
  77. textDeviceLabel.text = name + $" <color=#00FF00>{btnResolution.GetComponentInChildren<Text>().text}</color> ";
  78. ScreenProjection.ins.Connect(uid, name);
  79. ScreenProjection.ins.StopBrowse();
  80. });
  81. o.SetActive(true);
  82. info.o = o;
  83. info.valid = true;
  84. deviceMap[uid] = info;
  85. }
  86. List<string> willDels = null;
  87. foreach (var item in deviceMap) {
  88. if (!item.Value.valid) {
  89. if (willDels == null) willDels = new List<string>();
  90. willDels.Add(item.Value.uid);
  91. };
  92. }
  93. if (willDels != null) {
  94. foreach (var mapkey in willDels) {
  95. Destroy(deviceMap[mapkey].o);
  96. deviceMap.Remove(mapkey);
  97. }
  98. }
  99. }
  100. void InitBtn_SetResolution() {
  101. btnResolution.onClick.AddListener(() => {
  102. AudioMgr.ins.PlayBtn();
  103. ScreenProjection.mirrorResolutionID = ScreenProjection.mirrorResolutionID % 2 + 1;
  104. RefreshBtn_SetResolution();
  105. });
  106. RefreshBtn_SetResolution();
  107. }
  108. void RefreshBtn_SetResolution() {
  109. if (ScreenProjection.mirrorResolutionID == 1) {
  110. btnResolution.GetComponentInChildren<Text>().text = "720P";
  111. } else if (ScreenProjection.mirrorResolutionID == 2) {
  112. btnResolution.GetComponentInChildren<Text>().text = "1080P";
  113. }
  114. }
  115. public void OnClick_BtnBack() {
  116. AudioMgr.ins.PlayBtn();
  117. LogicBtnBack();
  118. }
  119. void LogicBtnBack() {
  120. if (ScreenProjection.ins && ScreenProjection.ins.HasDeviceInConnect()) {
  121. gameObject.SetActive(false);
  122. } else {
  123. ScreenProjection.Release();
  124. Destroy(gameObject);
  125. }
  126. }
  127. void ChangeStage(int stage) {
  128. _stageNum = stage;
  129. if (_stageNum == 1) {
  130. transform.Find("Stage1").gameObject.SetActive(true);
  131. transform.Find("Stage2").gameObject.SetActive(false);
  132. OnBrowse(new JArray());
  133. } else if (_stageNum == 2) {
  134. transform.Find("Stage1").gameObject.SetActive(false);
  135. transform.Find("Stage2").gameObject.SetActive(true);
  136. }
  137. }
  138. void OnClick_BtnStop() {
  139. AudioMgr.ins.PlayBtn();
  140. ScreenProjection.Release();
  141. ChangeStage(1);
  142. _needReinitSDK = true;
  143. }
  144. bool _needReinitSDK = false;
  145. void Update() {
  146. if (_needReinitSDK) {
  147. ScreenProjection.Init();
  148. if (ScreenProjection.ins) _needReinitSDK = false;
  149. }
  150. }
  151. }