PeerSamllView.cs 12 KB

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