PeerSamllView.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. using LitJson;
  2. using rtc;
  3. using System;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using TMPro;
  7. using UnityEngine;
  8. using UnityEngine.EventSystems;
  9. using UnityEngine.UI;
  10. using XRTool.Util;
  11. public class PeerSamllView : BaseView, IPointerEnterHandler, IPointerExitHandler
  12. {
  13. public Text tName;
  14. public TMP_Text name;
  15. public MeshRenderer mViewTexture;
  16. public GameObject user;
  17. public GameObject add;
  18. public GameObject enterBtn;
  19. public SysEnterManager sysEenter;
  20. public CustomPeer cPeer;
  21. public Button addBtn;
  22. private InviteUserConfig inviteUserConfig;
  23. private float alltime = 30f;
  24. private bool istime = false;
  25. public Texture texture;
  26. public GameObject ChooseKuang;
  27. public GameObject AudioEffect;
  28. private void Start()
  29. {
  30. addBtn.onClick.AddListener(ClickAdd);
  31. WSHandler.Rtc.OnActiveSpeaker += ActiveSpeaker;
  32. }
  33. private void OnDestroy()
  34. {
  35. WSHandler.Rtc.OnActiveSpeaker -= ActiveSpeaker;
  36. }
  37. SCRtcPeer p;
  38. private void ActiveSpeaker(JsonData data)
  39. {
  40. if (this.gameObject.activeSelf)
  41. {
  42. if (data["data"]["peerId"] != null)
  43. {
  44. string peerId = data["data"]["peerId"].ToString();
  45. if (!string.IsNullOrEmpty(peerId))
  46. {
  47. p = SCRtcFactory.Instance.mSCRtcPeers.getPeer(peerId);
  48. if (p != null)
  49. {
  50. p.changeVolumn(int.Parse(data["data"]["volume"].ToString()));
  51. }
  52. if (cPeer != null && peerId == cPeer.peerId)
  53. {
  54. AudioEffect.SetActive(true);
  55. }
  56. else
  57. {
  58. AudioEffect.SetActive(false);
  59. }
  60. }
  61. }
  62. }
  63. }
  64. private void ShowChooseKuang(string id)
  65. {
  66. if (id == cPeer.peerId)
  67. {
  68. ChooseKuang.SetActive(true);
  69. }
  70. else
  71. {
  72. ChooseKuang.SetActive(false);
  73. }
  74. }
  75. private void Update()
  76. {
  77. if (istime)
  78. {
  79. alltime -= Time.deltaTime;
  80. if (alltime <= 0)
  81. {
  82. istime = false;
  83. alltime = 30f;
  84. if (this.inviteUserConfig != null)
  85. {
  86. if (InviteDlg.Instance && InviteDlg.Instance.InvitingUnionIdList.Contains(this.inviteUserConfig))
  87. {
  88. PopUpInfo.Instance.showPublic(PopUpInfo.PopType.Tip, this.inviteUserConfig.NickName + "无应答");
  89. InviteDlg.Instance.InvitingUnionIdList.Remove(this.inviteUserConfig);
  90. RoomOtherUser.Instance.UpdateList();
  91. }
  92. }
  93. }
  94. }
  95. else
  96. {
  97. alltime = 30f;
  98. }
  99. }
  100. private void OnEnable()
  101. {
  102. alltime = 30f;
  103. istime = false;
  104. AudioEffect.SetActive(false);
  105. }
  106. private void OnDisable()
  107. {
  108. alltime = 30f;
  109. istime = false;
  110. AudioEffect.SetActive(false);
  111. }
  112. public override void init(int i, BaseConfig c, int vi)
  113. {
  114. base.init(i, c, vi);
  115. mViewTexture.material.mainTexture = texture;
  116. if (c is PeerConfig)
  117. {
  118. Debug.Log("initinitinitinitinitinit");
  119. if (this.inviteUserConfig != null)
  120. {
  121. this.inviteUserConfig = null;
  122. }
  123. user.SetActive(true);
  124. add.SetActive(false);
  125. enterBtn.SetActive(false);
  126. sysEenter.gameObject.SetActive(true);
  127. cPeer = (c as PeerConfig).peer;
  128. sysEenter.cPeer = cPeer;
  129. // tName.text = cPeer.name;
  130. name.text = cPeer.name;
  131. if (!cPeer.isSn)
  132. {
  133. if (!string.IsNullOrEmpty(cPeer.avater))
  134. {
  135. NetWorkHeaders.Instance.getNetTexture(cPeer.avater, null, (Texture tex) => {
  136. mViewTexture.material.mainTexture = tex;
  137. });
  138. }
  139. else
  140. {
  141. if (UserInfo.defaulttextIcon != null)
  142. {
  143. mViewTexture.material.mainTexture = UserInfo.defaulttextIcon;
  144. }
  145. }
  146. }
  147. else
  148. {
  149. NetWorkHeaders.GetUserAvater((aData) =>
  150. {
  151. for (int j = 0; j < aData["data"]["sn"].Count; j++)
  152. {
  153. NetWorkHeaders.Instance.getNetTexture(aData["data"]["sn"][j].ToString(), null, (Texture tex) => {
  154. mViewTexture.material.mainTexture = tex;
  155. });
  156. }
  157. });
  158. }
  159. cPeer.onChangeInfo -= OnChangeInfo;
  160. cPeer.onChangeInfo += OnChangeInfo;
  161. cPeer.onChangeTexture -= onChangeTexture;
  162. cPeer.onChangeTexture += onChangeTexture;
  163. cPeer.onChangeName -= onChangeName;
  164. cPeer.onChangeName += onChangeName;
  165. RoomOtherUser.ShowAction -= ShowChooseKuang;
  166. RoomOtherUser.ShowAction += ShowChooseKuang;
  167. if (RoomMain.Instance.userId == RoomMain.NOUSER)
  168. {
  169. RoomMain.Instance.setBigView(cPeer.tex, cPeer.peerId);
  170. }
  171. alltime = 30f;
  172. istime = false;
  173. bool isAdd =false;
  174. for (int j = 0; j < AgoraVideoAudioManager.Instance.listCustomPeer.Count; j++)
  175. {
  176. if(AgoraVideoAudioManager.Instance.listCustomPeer[j].peerId == cPeer.peerId)
  177. {
  178. isAdd = true;
  179. }
  180. }
  181. if(!isAdd)
  182. AgoraVideoAudioManager.Instance.listCustomPeer.Add(cPeer);
  183. }
  184. else if (c is InviteUserConfig)
  185. {
  186. if (cPeer != null)
  187. {
  188. cPeer = null;
  189. }
  190. this.inviteUserConfig = c as InviteUserConfig;
  191. user.SetActive(true);
  192. add.SetActive(false);
  193. // tName.text = "等待进入...";
  194. name.text = "等待进入...";
  195. sysEenter.gameObject.SetActive(false);
  196. if (this.inviteUserConfig != null && !string.IsNullOrEmpty(inviteUserConfig.Avatar))
  197. {
  198. NetWorkHeaders.Instance.getNetTexture(inviteUserConfig.Avatar, null, (Texture tex) => {
  199. mViewTexture.material.mainTexture = tex;
  200. });
  201. }
  202. else
  203. {
  204. if (UserInfo.defaulttextIcon != null)
  205. {
  206. mViewTexture.material.mainTexture = UserInfo.defaulttextIcon;
  207. }
  208. }
  209. enterBtn.SetActive(false);
  210. alltime = 30f;
  211. istime = true;
  212. }
  213. else
  214. {
  215. user.SetActive(false);
  216. // add.SetActive(true);
  217. alltime = 30f;
  218. istime = false;
  219. }
  220. }
  221. void onChangeTexture()
  222. {
  223. if(RoomMain.Instance.userId==cPeer.peerId)
  224. {
  225. RoomMain.Instance.setBigView(cPeer.tex, cPeer.peerId);
  226. }
  227. }
  228. void onChangeName(string cName)
  229. {
  230. // tName.text = cName;
  231. name.text = cName;
  232. }
  233. public void setBigView()
  234. {
  235. if (cPeer == null)
  236. {
  237. return;
  238. }
  239. RoomMain.Instance.setBigView(cPeer.tex,cPeer.peerId);
  240. RoomOtherUser.ShowAction?.Invoke(cPeer.peerId);
  241. AgoraVideoAudioManager.Instance.AddListShowView(cPeer.peerId, RoomMain.Instance.agoraRawImage, true);
  242. }
  243. public void showBigView()
  244. {
  245. RoomMain.Instance.setBigView(RemoteRtc.Instance.NoUser, RoomMain.NOUSER);
  246. PopPeerView.Instance.addBigView(cPeer);
  247. }
  248. private Timer t;
  249. public void OnEnterBtn()
  250. {
  251. }
  252. public void exitBtn()
  253. {
  254. enterBtn.SetActive(false);
  255. }
  256. private void ClickAdd()
  257. {
  258. if (ShowRoom.Instance)
  259. {
  260. ShowRoom.Instance.window[4].SetActive(true);
  261. if (InviteDlg.Instance)
  262. {
  263. InviteDlg.Instance.ShowHistory();
  264. }
  265. }
  266. }
  267. private void OnChangeInfo(string type, string id)
  268. {
  269. if (type == "video")
  270. {
  271. if (cPeer.isVideo)
  272. {
  273. if (cPeer.isCloseVideo)
  274. {
  275. RemoteRtc.Instance.closeRev(cPeer.cIdV);
  276. }
  277. }
  278. else
  279. {
  280. }
  281. if (RoomMain.Instance.userId == cPeer.peerId)
  282. {
  283. Debug.Log("开启大视频");
  284. RoomMain.Instance.setBigView(cPeer.tex, cPeer.peerId);
  285. }
  286. }
  287. else
  288. {
  289. if (cPeer.isAudio)
  290. {
  291. if (cPeer.isCloseAudio)
  292. {
  293. RemoteRtc.Instance.closeRev(cPeer.cIdA);
  294. }
  295. }
  296. else
  297. {
  298. }
  299. }
  300. }
  301. public void OpenVideo()
  302. {
  303. //if (cPeer != null && cPeer.isVideo && cPeer.cIdV != "" && cPeer.isCloseVideo)
  304. //{
  305. // RemoteRtc.Instance.startRev(cPeer.cIdV);
  306. // cPeer.isCloseVideo = false;
  307. // // cPeer.setInfo("audio", cPeer.cIdA,true);
  308. // // AgoraVideoAudioManager.Instance.EnableLocalVideo(true);
  309. //}
  310. // cPeer.setInfo("audio", cPeer.cIdA,true);
  311. AgoraVideoAudioManager.Instance.MuteRemoteVideoStream(cPeer.peerId, true);
  312. // RoomMain.Instance.agoraRawImage.gameObject.SetActive(true);
  313. }
  314. public void CloseVideo()
  315. {
  316. //if (cPeer != null && cPeer.isVideo && cPeer.cIdV != "" && !cPeer.isCloseVideo)
  317. //{
  318. // RemoteRtc.Instance.closeRev(cPeer.cIdV);
  319. // cPeer.isCloseVideo = true;
  320. // // cPeer.setInfo("audio", cPeer.cIdA,true);
  321. // //AgoraVideoAudioManager.Instance.EnableLocalVideo(false);
  322. //}
  323. // cPeer.setInfo("audio", cPeer.cIdA,true);
  324. AgoraVideoAudioManager.Instance.MuteRemoteVideoStream(cPeer.peerId, false);
  325. RoomMain.Instance.agoraRawImage.gameObject.SetActive(false);
  326. }
  327. public void openMic()
  328. {
  329. //if (cPeer != null && cPeer.isAudio && cPeer.cIdA != "" && cPeer.isCloseAudio)
  330. //{
  331. // RemoteRtc.Instance.startRev(cPeer.cIdA);
  332. // cPeer.isCloseAudio = true;
  333. // // cPeer.setInfo("audio", cPeer.cIdA,true);
  334. // // AgoraVideoAudioManager.Instance.MuteRemoteAudioStream(cPeer.peerId, true);
  335. //}
  336. // cPeer.setInfo("audio", cPeer.cIdA,true);
  337. AgoraVideoAudioManager.Instance.MuteRemoteAudioStream(cPeer.peerId, true);
  338. }
  339. public void closeMic()
  340. {
  341. //if (cPeer != null && cPeer.isAudio && cPeer.cIdA != "" && !cPeer.isCloseAudio)
  342. //{
  343. // RemoteRtc.Instance.closeRev(cPeer.cIdA);
  344. // cPeer.isCloseAudio = false;
  345. // // cPeer.setInfo("audio", cPeer.cIdA, false);
  346. // // AgoraVideoAudioManager.Instance.MuteRemoteAudioStream(cPeer.peerId, false);
  347. //}
  348. // cPeer.setInfo("audio", cPeer.cIdA, false);
  349. AgoraVideoAudioManager.Instance.MuteRemoteAudioStream(cPeer.peerId, false);
  350. }
  351. public void OnPointerEnter(PointerEventData eventData)
  352. {
  353. if (cPeer == null)
  354. {
  355. return;
  356. }
  357. if (t != null)
  358. {
  359. TimerMgr.Instance.DestroyTimer(t);
  360. }
  361. PeerList plist = this.transform.parent.GetComponent<PeerList>();
  362. plist.exitBtn();
  363. enterBtn.GetComponent<SysEnterManager>().cPeer = cPeer;
  364. // enterBtn.SetActive(true);
  365. BoxCollider bc = mViewTexture.GetComponent<BoxCollider>();
  366. bc.size = new Vector3(1.8f, 1.4f, bc.size.z);
  367. bc.center = new Vector3(0, -0.2f, 0);
  368. /*
  369. t = TimerMgr.Instance.CreateTimer(() => {
  370. exitBtn();
  371. }, 4f);*/
  372. }
  373. public void OnPointerExit(PointerEventData eventData)
  374. {
  375. t = TimerMgr.Instance.CreateTimer(() => {
  376. BoxCollider bc = mViewTexture.GetComponent<BoxCollider>();
  377. bc.size = new Vector3(1f, 1f, bc.size.z);
  378. bc.center = new Vector3(0, 0f, 0);
  379. exitBtn();
  380. }, 2f);
  381. }
  382. }