UIConnectPanel.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. #if UMR
  2. #endif
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6. using UnityEngine.Video;
  7. namespace Rokid.MRC
  8. {
  9. public class UIConnectPanel : UIPanelBase
  10. {
  11. private Transform rootPanel;
  12. private Text txtTitle;
  13. private Text txtDeviceNameLabel;
  14. private Text txtDeviceName;
  15. private Text txtIPLabel;
  16. private Text txtIP;
  17. private Text txtStatusLabel;
  18. private Text txtStatusDisconnect;
  19. private Transform rootStatusConnect;
  20. private Text txtStatusConnect;
  21. private Text txtButtonDisconnect;
  22. private Text txtDesc;
  23. private Button btnNetwork;
  24. private Text txtButtonNetwork;
  25. private Image imgWifi;
  26. private Text txtWifiName;
  27. private Text txtWifiLabel;
  28. //播放
  29. private VideoPlayer videoPlayer;
  30. private Image imgPlay;
  31. private Button btnPlayBg;
  32. public override void OnInit()
  33. {
  34. base.OnInit();
  35. rootPanel = transform.Find("Root");
  36. //标题
  37. txtTitle = transform.Find("Root/TxtTittle").gameObject.GetComponent<Text>();
  38. //设备名称
  39. txtDeviceNameLabel = transform.Find("Root/InfoView/DeviceInfo/TxtNameLabel").GetComponent<Text>();
  40. txtDeviceName = transform.Find("Root/InfoView/DeviceInfo/TxtNameLabel/TxtName").GetComponent<Text>();
  41. //IP
  42. txtIPLabel = transform.Find("Root/InfoView/IPInfo/TxtIPLabel").GetComponent<Text>();
  43. txtIP = transform.Find("Root/InfoView/IPInfo/TxtIPLabel/TxtIP").GetComponent<Text>();
  44. //设备状态
  45. txtStatusLabel = transform.Find("Root/InfoView/SatusInfo/TxtStatusLabel").GetComponent<Text>();
  46. txtStatusDisconnect = transform.Find("Root/InfoView/SatusInfo/TxtDisconnect").GetComponent<Text>();
  47. rootStatusConnect = transform.Find("Root/InfoView/SatusInfo/Connected");
  48. txtStatusConnect = rootStatusConnect.Find("TxtStatus").GetComponent<Text>();
  49. txtButtonDisconnect = rootStatusConnect.Find("BtnStatus/Text").GetComponent<Text>();
  50. Button btnDisconnect = rootStatusConnect.Find("BtnStatus").GetComponent<Button>();
  51. btnDisconnect.onClick.AddListener(() =>
  52. {
  53. //断开按钮
  54. PlayerManager.Instance.CloseAllClient();
  55. });
  56. btnDisconnect.gameObject.SetActive(false);
  57. txtDesc = transform.Find("Root/InfoView/TxtDesc").GetComponent<Text>();
  58. //打开
  59. Button btnOpen = transform.Find("BtnOpen").GetComponent<Button>();
  60. btnOpen.onClick.AddListener(() =>
  61. {
  62. rootPanel.gameObject.SetActive(!rootPanel.gameObject.activeInHierarchy);
  63. if(rootPanel.gameObject.activeInHierarchy)
  64. {
  65. OnRefreshPanel(null);
  66. }
  67. });
  68. //设置网络
  69. btnNetwork = transform.Find("Root/InfoView/NetworkView/BtnNetwork").GetComponent<Button>();
  70. btnNetwork.onClick.AddListener(() =>
  71. {
  72. SDKManager.Instance.OpenWifiSetting();
  73. });
  74. Button btnWifi = transform.Find("Root/InfoView/NetworkView").GetComponent<Button>();
  75. btnWifi.onClick.AddListener(() =>
  76. {
  77. //刷新Wifi
  78. RefreshWifiStatus();
  79. });
  80. txtButtonNetwork = btnNetwork.transform.Find("Text").GetComponent<Text>();
  81. imgWifi = transform.Find("Root/InfoView/NetworkView/ImgWifi").GetComponent<Image>();
  82. txtWifiName = transform.Find("Root/InfoView/NetworkView/TxtWifiName").GetComponent<Text>();
  83. txtWifiLabel = transform.Find("Root/InfoView/NetworkView/TxtWifiLabel").GetComponent<Text>();
  84. //播放
  85. Transform displayView = transform.Find("Root/DisplayView");
  86. videoPlayer = displayView.GetComponent<VideoPlayer>();
  87. videoPlayer.loopPointReached += (source) =>
  88. {
  89. //视频播放结束
  90. imgPlay.gameObject.SetActive(true);
  91. };
  92. btnPlayBg = displayView.Find("BtnPlayBg").GetComponent<Button>();
  93. btnPlayBg.onClick.AddListener(() =>
  94. {
  95. if(videoPlayer.isPlaying)
  96. {
  97. videoPlayer.Pause();
  98. imgPlay.gameObject.SetActive(true);
  99. }
  100. else if(videoPlayer.isPaused)
  101. {
  102. videoPlayer.Play();
  103. imgPlay.gameObject.SetActive(false);
  104. }
  105. });
  106. imgPlay = displayView.Find("BtnPlayBg/ImgPlay").GetComponent<Image>();
  107. imgPlay.gameObject.SetActive(!videoPlayer.playOnAwake);
  108. //语言表
  109. txtTitle.text = LocalizationMgr.Instance.GetTextByKey("PanelTitle");
  110. txtDeviceNameLabel.text = LocalizationMgr.Instance.GetTextByKey("DeviceNameLabel");
  111. txtIPLabel.text = LocalizationMgr.Instance.GetTextByKey("IpLabel");
  112. txtStatusLabel.text = LocalizationMgr.Instance.GetTextByKey("StatusLabel");
  113. txtStatusDisconnect.text = LocalizationMgr.Instance.GetTextByKey("Disconnected");
  114. txtStatusConnect.text = LocalizationMgr.Instance.GetTextByKey("Connected");
  115. txtButtonDisconnect.text = LocalizationMgr.Instance.GetTextByKey("BtnDisconnect");
  116. txtDesc.text = LocalizationMgr.Instance.GetTextByKey("Description");
  117. txtButtonNetwork.text = LocalizationMgr.Instance.GetTextByKey("ButtonNetwork");
  118. txtWifiLabel.text = LocalizationMgr.Instance.GetTextByKey("WifiLabel");
  119. //注册网络事件
  120. MessageCenter.AddMsgListener(EventDefine.ServerConnectionEvent, HandleServerEvent);
  121. MessageCenter.AddMsgListener(GlobalDefine.RefreshConnectPanel, OnRefreshPanel);
  122. InitAnchorPanel();
  123. }
  124. public override void OnOpen()
  125. {
  126. base.OnOpen();
  127. //刷新IP
  128. RefreshIP();
  129. //设备状态
  130. RefreshDeviceStatus();
  131. //Wifi
  132. RefreshWifiStatus();
  133. }
  134. public override void OnClose()
  135. {
  136. base.OnClose();
  137. //移除事件
  138. MessageCenter.RemoveMsgListener(EventDefine.ServerConnectionEvent, HandleServerEvent);
  139. MessageCenter.RemoveMsgListener(GlobalDefine.RefreshConnectPanel, OnRefreshPanel);
  140. }
  141. private void OnRefreshPanel(object val)
  142. {
  143. //刷新IP
  144. RefreshIP();
  145. //设备状态
  146. RefreshDeviceStatus();
  147. //Wifi
  148. RefreshWifiStatus();
  149. }
  150. private void HandleServerEvent(object val)
  151. {
  152. ConnectionEventMessage msg = (ConnectionEventMessage)val;
  153. if(msg.ConnectStatus == ConnectStatus.Success)
  154. {
  155. rootPanel.gameObject.SetActive(false);
  156. }
  157. else if(msg.ConnectStatus == ConnectStatus.GenericDisconnect)
  158. {
  159. //某个客户端断开
  160. rootPanel.gameObject.SetActive(true);
  161. }
  162. RefreshDeviceStatus();
  163. }
  164. private void RefreshIP()
  165. {
  166. //IP
  167. string ip = MRCUtility.GetWirelessIP(AddressType.IPv4);
  168. if(string.IsNullOrEmpty(ip))
  169. {
  170. ip = LocalizationMgr.Instance.GetTextByKey("IpDesc");
  171. }
  172. txtIP.text = ip;
  173. }
  174. //刷新设备状态
  175. private void RefreshDeviceStatus()
  176. {
  177. //设备名称
  178. txtDeviceName.text = SystemInfo.deviceName;
  179. PlayerData? playerData = PlayerManager.Instance.GetConnectedPlayerData();
  180. bool haveConnectedClient = playerData == null ? false : true;
  181. rootStatusConnect.gameObject.SetActive(haveConnectedClient);
  182. txtStatusDisconnect.gameObject.SetActive(!haveConnectedClient);
  183. }
  184. //刷新Wifi状态
  185. private void RefreshWifiStatus()
  186. {
  187. if(Application.internetReachability == NetworkReachability.NotReachable)
  188. {
  189. imgWifi.color = Color.gray;
  190. txtWifiName.color = Color.gray;
  191. txtWifiName.text = LocalizationMgr.Instance.GetTextByKey("NoWifi");
  192. }
  193. else
  194. {
  195. string wifiName = SDKManager.Instance.GetCurConnectedWifiName();
  196. if(string.IsNullOrEmpty(wifiName))
  197. {
  198. imgWifi.color = Color.gray;
  199. txtWifiName.color = Color.gray;
  200. txtWifiName.text = LocalizationMgr.Instance.GetTextByKey("NoWifi");
  201. }
  202. else
  203. {
  204. imgWifi.color = Color.white;
  205. txtWifiName.color = Color.white;
  206. txtWifiName.text = wifiName;
  207. }
  208. }
  209. }
  210. //锚点界面
  211. private Transform clientAnchor;
  212. private GameObject anchorViewPort;
  213. private Text txtAnchorDesc;
  214. private RectTransform anchorItemParent;
  215. private GameObject anchorItemPrefab;
  216. //锚点UI集合
  217. private List<GameObject> anchorItemList = new List<GameObject>();
  218. public void InitAnchorPanel()
  219. {
  220. InitAnchor();
  221. InitComposite();
  222. }
  223. private void InitAnchor()
  224. {
  225. clientAnchor = transform.Find("ClientAnchor");
  226. clientAnchor.gameObject.SetActive(MRCDriver.Instance.ShowAnchorUI);
  227. if(!MRCDriver.Instance.ShowAnchorUI)
  228. {
  229. return;
  230. }
  231. anchorViewPort = transform.Find("ClientAnchor/ScrollView/Viewport").gameObject;
  232. txtAnchorDesc = transform.Find("ClientAnchor/TxtDesc").GetComponent<Text>();
  233. anchorItemParent = transform.Find("ClientAnchor/ScrollView/Viewport/Content").GetComponent<RectTransform>();
  234. anchorItemPrefab = transform.Find("ClientAnchor/ScrollView/Viewport/Content/Anchor").gameObject;
  235. anchorItemPrefab.SetActive(false);
  236. //注册网络事件
  237. MessageCenter.AddMsgListener(EventDefine.ServerConnectionEvent, UpdateAnchorPanel);
  238. MessageCenter.AddMsgListener(GlobalDefine.ClientAnchorInstantiate, UpdateAnchorPanel);
  239. UpdateAnchorItem();
  240. }
  241. private void UpdateAnchorPanel(object val)
  242. {
  243. if(val == null)
  244. {
  245. //生成Anchor事件
  246. UpdateAnchorItem();
  247. return;
  248. }
  249. ConnectionEventMessage msg = (ConnectionEventMessage)val;
  250. if(msg.ConnectStatus == ConnectStatus.Success || msg.ConnectStatus == ConnectStatus.GenericDisconnect)
  251. {
  252. UpdateAnchorItem();
  253. }
  254. }
  255. public void CheckAnchorList()
  256. {
  257. if(PlayerManager.Instance.HavePlayer())
  258. {
  259. anchorViewPort.SetActive(true);
  260. txtAnchorDesc.gameObject.SetActive(false);
  261. }
  262. else
  263. {
  264. anchorViewPort.SetActive(false);
  265. txtAnchorDesc.gameObject.SetActive(true);
  266. }
  267. }
  268. //显示每个客户端锚点UI
  269. public void UpdateAnchorItem()
  270. {
  271. CheckAnchorList();
  272. //删除所有UI的Item
  273. DeleteCachedItem();
  274. //生成新UI
  275. foreach(var player in PlayerManager.Instance.PlayerDataDic)
  276. {
  277. //Debug.Log($"Name : {player.Value.PlayerName} ClientID : {player.Value.ClientId}");
  278. if(player.Value.ClientId != 0)
  279. {
  280. AddAnchorItem(player.Value);
  281. }
  282. }
  283. //重新刷新
  284. LayoutRebuilder.ForceRebuildLayoutImmediate(anchorItemParent);
  285. }
  286. public void AddAnchorItem(PlayerData playerData)
  287. {
  288. GameObject objItem = Instantiate(anchorItemPrefab, anchorItemParent);
  289. anchorItemList.Add(objItem);
  290. objItem.SetActive(true);
  291. Image bg = objItem.GetComponent<Image>();
  292. Text TxtName = objItem.transform.Find("TxtName").GetComponent<Text>();
  293. Toggle tog = objItem.transform.Find("Toggle").GetComponent<Toggle>();
  294. //背景颜色
  295. if(playerData.Anchor != null && playerData.Anchor.activeInHierarchy)
  296. {
  297. //有生成Anchor
  298. bg.color = playerData.IsVideoClient ? Color.cyan : Color.green;
  299. tog.isOn = true;
  300. }
  301. else
  302. {
  303. bg.color = playerData.IsVideoClient ? Color.cyan : Color.red;
  304. tog.isOn = false;
  305. }
  306. //添加开关,显示或隐藏Anchor
  307. tog.onValueChanged.AddListener((isOn) =>
  308. {
  309. ResourceManager.Instance.CreateClientAnchor(playerData);
  310. playerData.Anchor.SetActive(isOn);
  311. bg.color = playerData.IsVideoClient ? Color.cyan : (isOn ? Color.green : Color.red);
  312. });
  313. //客户端名称
  314. //TxtName.text = playerData.PlayerName;
  315. TxtName.text = playerData.PlayerName + " IP : " + playerData.ClientIP;
  316. //Button btnConnect = objItem.transform.Find("BtnConnect").GetComponent<Button>();
  317. //btnConnect.onClick.AddListener(() =>
  318. //{
  319. //});
  320. }
  321. public void DeleteCachedItem()
  322. {
  323. foreach(GameObject anchorItem in anchorItemList)
  324. {
  325. if(anchorItem != null)
  326. {
  327. Destroy(anchorItem);
  328. }
  329. }
  330. anchorItemList.Clear();
  331. }
  332. //RT渲染画面显示
  333. private Transform compositeRoot;
  334. private RawImage imgCompositeCamColorRT;
  335. private void InitComposite()
  336. {
  337. imgCompositeCamColorRT = transform.Find("Composite/ImgColorRT").GetComponent<RawImage>();
  338. compositeRoot = transform.Find("Composite");
  339. compositeRoot.gameObject.SetActive(MRCDriver.Instance.ShowCompositeUI);
  340. imgCompositeCamColorRT.gameObject.SetActive(MRCDriver.Instance.ShowCompositeUI);
  341. if(MRCDriver.Instance.ShowCompositeUI)
  342. {
  343. MessageCenter.AddMsgListener(GlobalDefine.CameraParamFinished, SetRenderTextureImage);
  344. }
  345. }
  346. public void SetRenderTextureImage(object val)
  347. {
  348. RenderTexture renderRT = (RenderTexture)val;
  349. //设置显示纹理
  350. imgCompositeCamColorRT.texture = renderRT;
  351. //设置Image大小
  352. RectTransform colorRectTransform = imgCompositeCamColorRT.GetComponent<RectTransform>();
  353. //colorRectTransform.rect.Set(colorRectTransform.rect.x, colorRectTransform.rect.y, renderRT.width, renderRT.height);
  354. SetSize(colorRectTransform, new Vector2(renderRT.width, renderRT.height));
  355. }
  356. public void SetSize(RectTransform rectTrans, Vector2 newSize)
  357. {
  358. Vector2 oldSize = rectTrans.rect.size;
  359. Vector2 offset = newSize - oldSize;
  360. rectTrans.sizeDelta += offset;
  361. }
  362. }
  363. }