PlayerViewComponent.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. using BeinLab.Util;
  2. using ShadowStudio.Mgr;
  3. using ShadowStudio.UI;
  4. using System;
  5. using System.Collections;
  6. using System.Collections.Generic;
  7. using UnityEngine;
  8. using UnityEngine.UI;
  9. using XRTool.Util;
  10. using XRTool.WorldUI;
  11. using static BoundingBox;
  12. namespace ShadowStudio.Model
  13. {
  14. /// <summary>
  15. /// 用户的视频通话窗口
  16. /// </summary>
  17. public class PlayerViewComponent : UIComponent
  18. {
  19. public static string viewId = "ArtId_PlayerView";
  20. public static Dictionary<string, ArtContainerHandler> viewDic = new Dictionary<string, ArtContainerHandler>();
  21. private string peerid;
  22. private bool isInit = false;
  23. private Peer _peer;
  24. public static event Action<string, bool> ViewChange;
  25. /// <summary>
  26. /// 创建一个视频通话窗口
  27. /// 即时同步
  28. /// </summary>
  29. /// <param name="peerid">点击的用户的id</param>
  30. /// <param name="target"></param>
  31. public static void CreateViewComponent(string peerid, Transform target, bool isDel = false)
  32. {
  33. if (viewDic.ContainsKey(peerid))
  34. {
  35. return;
  36. }
  37. GoodsInfo data = new GoodsInfo();
  38. var info = ArtInfoMgr.Instance.GetArtInfo(viewId);
  39. data.art_id = viewId;
  40. data.id = UnityEngine.Random.Range(100000, 999999999);
  41. if (target)
  42. {
  43. data.goods_info = UnityUtil.TransferToString(target, 2);
  44. }
  45. else
  46. {
  47. data.goods_info = UnityUtil.ArtTransferInfo(info);
  48. }
  49. data.goods_name = peerid;
  50. data.rid = int.Parse(UserInfoMgr.Instance.userConfig.Id);
  51. data.scene_id = -1;
  52. ///创建本地窗口
  53. ArtInfoMgr.Instance.SyncGoods(data);
  54. ///
  55. ///ArtInfoMgr.Instance.SendTransfer(data);
  56. if (target && isDel)
  57. {
  58. Destroy(target.gameObject);
  59. }
  60. }
  61. private bool checkRtc()
  62. {
  63. if (_peer != null)
  64. {
  65. if (cPeer != null)
  66. {
  67. return true;
  68. }
  69. else
  70. {
  71. cPeer = AgoraRTCManager.Instance.GetCustomPeer(_peer.PeerId);
  72. return true;
  73. }
  74. }
  75. return false;
  76. }
  77. void StateUpdata()
  78. {
  79. if (cPeer.isVideo)
  80. {
  81. if (cPeer.isOpenVideo)
  82. {
  83. openVideo.SetActive(false);
  84. closeVideo.SetActive(false);
  85. pauseVideo.SetActive(true);
  86. VideoEmpty.gameObject.SetActive(true);
  87. agoraShowVideo.gameObject.SetActive(false);
  88. }
  89. else
  90. {
  91. openVideo.SetActive(false);
  92. closeVideo.SetActive(true);
  93. pauseVideo.SetActive(false);
  94. VideoEmpty.gameObject.SetActive(false);
  95. agoraShowVideo.gameObject.SetActive(true);
  96. }
  97. }
  98. else
  99. {
  100. openVideo.SetActive(true);
  101. closeVideo.SetActive(false);
  102. pauseVideo.SetActive(false);
  103. VideoEmpty.gameObject.SetActive(true);
  104. agoraShowVideo.gameObject.SetActive(false);
  105. }
  106. if (cPeer.isAudio)
  107. {
  108. if (cPeer.isOpenAduio)
  109. {
  110. openAudio.SetActive(false);
  111. closeAudio.SetActive(false);
  112. pauseAudio.SetActive(true);
  113. }
  114. else
  115. {
  116. openAudio.SetActive(false);
  117. closeAudio.SetActive(true);
  118. pauseAudio.SetActive(false);
  119. }
  120. }
  121. else
  122. {
  123. openAudio.SetActive(true);
  124. closeAudio.SetActive(false);
  125. pauseAudio.SetActive(false);
  126. }
  127. }
  128. public float volume = 0.2f;
  129. public Text textMic;
  130. public GameObject pauseCerAudio;
  131. public GameObject pauseCerVideo;
  132. public GameObject pauseAudio;
  133. public GameObject pauseVideo;
  134. public GameObject openVideo;
  135. public GameObject closeVideo;
  136. public MeshRenderer videoBG;
  137. public Text nametext;
  138. public XRIcon touxiang;
  139. public GameObject openAudio;
  140. public GameObject closeAudio;
  141. public GameObject VolumnGo;
  142. public RawImage agoraShowVideo;
  143. private void Update()
  144. {
  145. checkRtc();
  146. StateUpdata();
  147. }
  148. bool isRtcInit = false;
  149. AgoraCustomPeer cPeer;
  150. public void setVideo(string peerid)
  151. {
  152. _peer = UserItemList.Instance.GetPeer(peerid);
  153. nametext.text = _peer.NickName;
  154. if (peerid == CommonMethod.MyPeer.PeerId)
  155. {
  156. isRtcInit = true;
  157. return;
  158. }
  159. else
  160. {
  161. Debug.Log(" Peerid != MyPeer" + peerid);
  162. }
  163. }
  164. public void addMic()
  165. {
  166. volume += 0.1f;
  167. }
  168. public void redMic()
  169. {
  170. volume -= 0.1f;
  171. }
  172. public void openMic()
  173. {
  174. if (_peer.IsSlef)
  175. {
  176. AgoraRTCManager.Instance.MuteLocalAudioStream(true);
  177. }
  178. else
  179. {
  180. AgoraRTCManager.Instance.MuteRemoteAudioStream(cPeer.peerId, true);
  181. }
  182. }
  183. public void closeMic()
  184. {
  185. if (_peer.IsSlef)
  186. {
  187. AgoraRTCManager.Instance.MuteLocalAudioStream(false);
  188. }
  189. else
  190. {
  191. AgoraRTCManager.Instance.MuteRemoteAudioStream(cPeer.peerId, false);
  192. }
  193. }
  194. public void OpenVideo()
  195. {
  196. if (_peer.IsSlef)
  197. {
  198. AgoraRTCManager.Instance.MuteLocalVideoStream(true);
  199. }
  200. else
  201. {
  202. AgoraRTCManager.Instance.MuteRemoteVideoStream(cPeer.peerId, true);
  203. }
  204. }
  205. public void CloseVideo()
  206. {
  207. if (_peer.IsSlef)
  208. {
  209. AgoraRTCManager.Instance.MuteLocalVideoStream(false);
  210. }
  211. else
  212. {
  213. AgoraRTCManager.Instance.MuteRemoteVideoStream(cPeer.peerId, false);
  214. }
  215. }
  216. private Button closeBtn;
  217. public Button CloseBtn
  218. {
  219. get
  220. {
  221. if (!closeBtn)
  222. {
  223. closeBtn = UnityUtil.GetBreadthChild<Button>(transform, "CloseBtn");
  224. }
  225. return closeBtn;
  226. }
  227. }
  228. private MeshRenderer showVideo;
  229. public MeshRenderer ShowVideo
  230. {
  231. get
  232. {
  233. if (!showVideo)
  234. {
  235. showVideo = UnityUtil.GetBreadthChild<MeshRenderer>(transform, "ShowVideo");
  236. }
  237. return showVideo;
  238. }
  239. }
  240. private MeshRenderer videoEmpty;
  241. public MeshRenderer VideoEmpty
  242. {
  243. get
  244. {
  245. if (!videoEmpty)
  246. {
  247. videoEmpty = UnityUtil.GetBreadthChild<MeshRenderer>(transform, "VideoEmpty");
  248. }
  249. return videoEmpty;
  250. }
  251. }
  252. private void OnDestroy()
  253. {
  254. ViewChange?.Invoke(peerid, false);
  255. if (!string.IsNullOrEmpty(peerid) && viewDic.ContainsKey(peerid))
  256. {
  257. viewDic.Remove(peerid);
  258. }
  259. //if (cPeer != null)
  260. // cPeer.onChangeInfo -= OnChangeInfo;
  261. }
  262. private void Start()
  263. {
  264. if (CloseBtn)
  265. {
  266. //CloseBtn.onClick.AddListener(OnClickClose);
  267. }
  268. if (ArtInfoMgr.Instance != null)
  269. {
  270. ArtInfoMgr.Instance.UserChange += OnUserChange;
  271. }
  272. XBoundingBox boxb= this.GetComponent<XBoundingBox>();
  273. if(boxb)
  274. boxb.ActiveAxis= AxisType.None;
  275. AgoraRTCManager.Instance.AgoarRawImageVideoView(peerid, agoraShowVideo);
  276. }
  277. /// <summary>
  278. /// 用户状态发生改变的时候
  279. /// </summary>
  280. /// <param name="peerId"></param>
  281. /// <param name="isJoin"></param>
  282. private void OnUserChange(string peerId, bool isJoin)
  283. {
  284. ///用户视频列表被创建,但用户退出
  285. ///删除用户视频列表
  286. if (viewDic.ContainsKey(peerId))
  287. {
  288. if (peerId == this.peerid && !isJoin)
  289. {
  290. OnClickClose();
  291. }
  292. }
  293. else
  294. {
  295. ///用户加入
  296. if (isJoin)
  297. {
  298. base.SendTransfer(2, 0);
  299. TimerMgr.Instance.CreateTimer(() =>
  300. {
  301. base.SendTransfer(2, 0);
  302. }, 3f);
  303. }
  304. }
  305. }
  306. /// <summary>
  307. /// 点击关闭按钮
  308. /// 删除此视频通话窗口
  309. /// 如何删除?发送额外消息
  310. /// 删除本地
  311. /// </summary>
  312. public void OnClickClose()
  313. {
  314. if (Container)
  315. {
  316. Container.data.goods_extended = "DES";
  317. base.SendTransfer(2, 0);
  318. }
  319. //if (cPeer != null)
  320. // cPeer.onChangeInfo -= OnChangeInfo;
  321. DestroyArt();
  322. }
  323. public override void DestroyArt()
  324. {
  325. base.DestroyArt();
  326. }
  327. public override void SendTransfer(int state, int status)
  328. {
  329. base.SendTransfer(state, status);
  330. //if (Container is WorldDlgContainer)
  331. //{
  332. // TimerMgr.Instance.CreateTimer(()=>
  333. // {
  334. // (Container as WorldDlgContainer).OnTransferSelect(false);
  335. // },0.02f);
  336. //}
  337. }
  338. /// <summary>
  339. /// 设置容器
  340. /// </summary>
  341. /// <param name="container"></param>
  342. /// <param name="art"></param>
  343. /// <param name="info"></param>
  344. public override void SetContainer(ArtContainerHandler container, UnityEngine.Object art, ArtInfo info)
  345. {
  346. base.SetContainer(container, art, info);
  347. RectTransform rt = GetComponent<RectTransform>();
  348. rt.offsetMax = Vector2.zero;
  349. rt.offsetMin = Vector2.zero;
  350. }
  351. public override void TransferSyn(GoodsInfo goods, bool smooth = true)
  352. {
  353. base.TransferSyn(goods, smooth);
  354. if (!isInit)
  355. {
  356. if (!viewDic.ContainsKey(goods.goods_name))
  357. {
  358. this.peerid = goods.goods_name;
  359. viewDic.Add(goods.goods_name, Container);
  360. setVideo(peerid);
  361. ///自身点击的,需要发送同步一次
  362. if (goods.rid.ToString() == UserInfoMgr.Instance.userConfig.Id)
  363. {
  364. SendTransfer(2, 0);
  365. }
  366. ViewChange?.Invoke(goods.goods_name, true);
  367. isInit = true;
  368. }
  369. }
  370. if (!string.IsNullOrEmpty(goods.goods_extended))
  371. {
  372. if (goods.goods_extended == "DES")
  373. {
  374. DestroyArt();
  375. }
  376. }
  377. }
  378. }
  379. }