UIServerPanel.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. namespace Rokid.MRC
  6. {
  7. public class UIServerPanel : UIPanelBase
  8. {
  9. public enum SubPanel
  10. {
  11. None,
  12. DescriptionView,
  13. IPListView,
  14. }
  15. private GameObject clientListView;
  16. private RectTransform clientItemParent;
  17. private GameObject clientItemPrefab;
  18. private GameObject descView;
  19. private Text txtDesc;
  20. private Button btnIPList;
  21. private Text txtTips;
  22. private List<GameObject> ipItemList = new List<GameObject>();
  23. private Button btnInterface;
  24. private GameObject interfaceInfo;
  25. private Text txtInterfaceInfo;
  26. private Button btnDebug;
  27. private GameObject debugInfo;
  28. private Text txtDebugInfo;
  29. private Button btnObj;
  30. private Button btnRandomPos;
  31. public GameObject syncObj;
  32. public override void OnInit()
  33. {
  34. descView = transform.Find("DescView").gameObject;
  35. txtDesc = transform.Find("DescView/TxtDesc").GetComponent<Text>();
  36. clientListView = transform.Find("ClientScrollView").gameObject;
  37. clientItemParent = transform.Find("ClientScrollView/Viewport/Content").GetComponent<RectTransform>();
  38. clientItemPrefab = transform.Find("ClientScrollView/Viewport/Content/ClientItem").gameObject;
  39. clientItemPrefab.SetActive(false);
  40. txtTips = transform.Find("TxtTips").GetComponent<Text>();
  41. btnIPList = transform.Find("BtnIPList").GetComponent<Button>();
  42. btnIPList.onClick.AddListener(OnClickOpenIPListView);
  43. //IP信息
  44. btnInterface = transform.Find("InterfaceView/BtnOpenInterface").GetComponent<Button>();
  45. btnInterface.onClick.AddListener(() =>
  46. {
  47. interfaceInfo.SetActive(!interfaceInfo.activeSelf);
  48. RefreshNetworkInfo();
  49. });
  50. interfaceInfo = transform.Find("InterfaceView/InterfaceInfo").gameObject;
  51. interfaceInfo.SetActive(false);
  52. txtInterfaceInfo = interfaceInfo.transform.Find("TxtInfo").GetComponent<Text>();
  53. //调试信息
  54. btnDebug = transform.Find("DebugView/BtnInfo").GetComponent<Button>();
  55. btnDebug.onClick.AddListener(() =>
  56. {
  57. debugInfo.SetActive(!debugInfo.activeSelf);
  58. });
  59. debugInfo = transform.Find("DebugView/DebugInfo").gameObject;
  60. debugInfo.SetActive(false);
  61. txtDebugInfo = debugInfo.transform.Find("TxtInfo").GetComponent<Text>();
  62. //生成物体
  63. btnObj = transform.Find("BtnObj").GetComponent<Button>();
  64. btnObj.onClick.AddListener(() =>
  65. {
  66. if(syncObj == null)
  67. {
  68. //syncObj = ResourceManager.Instance.CreateSyncObj();
  69. }
  70. });
  71. //测试,随机移动模型位置
  72. btnRandomPos = transform.Find("BtnPos").GetComponent<Button>();
  73. btnRandomPos.onClick.AddListener(() =>
  74. {
  75. if(syncObj != null)
  76. {
  77. float delta = Random.Range(0, 0.5f);
  78. Vector3 pos = new Vector3(syncObj.transform.position.x + delta, syncObj.transform.position.y, syncObj.transform.position.z + delta);
  79. syncObj.transform.position = pos;
  80. }
  81. });
  82. //界面类型
  83. Canvas canvas = GetComponent<Canvas>();
  84. if(is3DPanel)
  85. {
  86. canvas.renderMode = RenderMode.WorldSpace;
  87. }
  88. else
  89. {
  90. canvas.renderMode = RenderMode.ScreenSpaceOverlay;
  91. }
  92. }
  93. public override void OnOpen()
  94. {
  95. base.OnOpen();
  96. ClientData.onClientDataUpdate += UpdateIPItem;
  97. ShowSubView(SubPanel.None);
  98. txtTips.gameObject.SetActive(false);
  99. RefreshNetworkInfo();
  100. }
  101. private void Update()
  102. {
  103. if(!debugInfo.activeSelf)
  104. {
  105. return;
  106. }
  107. txtDebugInfo.text = $"Camera \n Pos : {Camera.main.transform.position} \n Rot : {Camera.main.transform.rotation}";
  108. }
  109. public override void OnClose()
  110. {
  111. base.OnClose();
  112. ClientData.onClientDataUpdate -= UpdateIPItem;
  113. }
  114. private void RefreshNetworkInfo()
  115. {
  116. string infos = "";
  117. List<string> interfaceInfos = MRCUtility.GetLocalIP(AddressType.IPv4);
  118. foreach(var info in interfaceInfos)
  119. {
  120. infos += info + "\n";
  121. }
  122. txtInterfaceInfo.text = infos;
  123. }
  124. private void OnClickOpenIPListView()
  125. {
  126. if(clientListView.activeSelf)
  127. {
  128. ShowSubView(SubPanel.None);
  129. }
  130. else
  131. {
  132. ShowSubView(SubPanel.IPListView);
  133. }
  134. }
  135. private void CheckClientList()
  136. {
  137. if(ClientData.clientIPList.Count > 0)
  138. {
  139. ShowSubView(SubPanel.IPListView);
  140. }
  141. }
  142. public void ShowDesc(string desc)
  143. {
  144. ShowSubView(SubPanel.DescriptionView);
  145. txtDesc.text = desc;
  146. }
  147. public void ShowTips(string tips)
  148. {
  149. txtTips.gameObject.SetActive(true);
  150. txtTips.text = tips;
  151. UIManager.Instance.StartCoroutine(WaitAndClose());
  152. }
  153. IEnumerator WaitAndClose()
  154. {
  155. yield return new WaitForSeconds(1);
  156. txtTips.gameObject.SetActive(false);
  157. }
  158. public void ShowSubView(SubPanel subPanel)
  159. {
  160. switch(subPanel)
  161. {
  162. case SubPanel.DescriptionView:
  163. clientListView.SetActive(false);
  164. descView.SetActive(true);
  165. break;
  166. case SubPanel.IPListView:
  167. clientListView.SetActive(true);
  168. descView.SetActive(false);
  169. break;
  170. case SubPanel.None:
  171. clientListView.SetActive(false);
  172. descView.SetActive(false);
  173. break;
  174. default:
  175. break;
  176. }
  177. }
  178. private void UpdateIPItem()
  179. {
  180. CheckClientList();
  181. DeleteCachedItem();
  182. for(int i = 0;i < ClientData.clientIPList.Count;i++)
  183. {
  184. ulong ip = ClientData.clientIPList[i];
  185. AddIPItem(ip);
  186. }
  187. LayoutRebuilder.ForceRebuildLayoutImmediate(clientItemParent);
  188. }
  189. private void AddIPItem(ulong ip)
  190. {
  191. GameObject objItem = Instantiate(clientItemPrefab, clientItemParent);
  192. ipItemList.Add(objItem);
  193. Text textIP = objItem.transform.Find("TxtIP").GetComponent<Text>();
  194. objItem.SetActive(true);
  195. textIP.text = "UserID : " + ip;
  196. }
  197. private void DeleteCachedItem()
  198. {
  199. foreach(GameObject ipItem in ipItemList)
  200. {
  201. if(ipItem != null)
  202. {
  203. Destroy(ipItem);
  204. }
  205. }
  206. ipItemList.Clear();
  207. }
  208. }
  209. }