ItemUserView.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. using ShadowStudio.Mgr;
  2. using ShadowStudio.Model;
  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 Vuforia;
  10. using XRTool.Util;
  11. using XRTool.WorldUI;
  12. public class ItemUserView : MonoBehaviour
  13. {
  14. public Peer _peer;
  15. public MeshRenderer videoBG;
  16. public Text nametext;
  17. public XRIcon touxiang;
  18. public Button bigBtn;
  19. // Start is called before the first frame update
  20. void Start()
  21. {
  22. bigBtn.onClick.AddListener(OnClickBigBtn);
  23. if (_peer.IsSlef)
  24. // AgoraVideoAudioManager.Instance.JoinChannel(CommonMethod.roomConfig.Id);
  25. AgoraVideoAudioManager.Instance.OpenAgoraAudio();
  26. }
  27. private void OnClickBigBtn()
  28. {
  29. if (ShowViewMgr.Instance)
  30. {
  31. PlayerViewComponent.CreateViewComponent(this._peer.PeerId, ShowViewMgr.Instance.Target);
  32. }
  33. }
  34. private void Update()
  35. {
  36. checkPeer();
  37. if(videoBG.gameObject.activeSelf&& cPeer!=null&&!_peer.IsSlef)
  38. {
  39. videoBG.material.mainTexture = cPeer.tex;
  40. }
  41. }
  42. void checkPeer()
  43. {
  44. if (cPeer == null&& SCRtcFactory.Instance!=null&& SCRtcFactory.Instance.mSCRtcPeers!=null&& _peer!=null)
  45. {
  46. cPeer = (CustomPeer)SCRtcFactory.Instance.mSCRtcPeers.getPeerName(_peer.PeerId);
  47. _peer.CustomPeer = cPeer;
  48. if (cPeer != null)
  49. {
  50. cPeer.onChangeInfo -= OnChangeInfo;
  51. cPeer.onChangeInfo += OnChangeInfo;
  52. if (!_peer.IsSlef)
  53. {
  54. if (cPeer.isAudio && cPeer.cIdA != "")
  55. {
  56. openAudioEffect();
  57. }
  58. else
  59. {
  60. closeAudioEffect();
  61. }
  62. if (cPeer.isVideo&& cPeer.cIdV!="")
  63. {
  64. openVideoEffect();
  65. }
  66. else
  67. {
  68. closeVideoEffect();
  69. }
  70. }
  71. }
  72. }
  73. }
  74. CustomPeer cPeer;
  75. public void Init(Peer peer)
  76. {
  77. Debug.Log("123213Init213213"+ peer.Avatar);
  78. _peer = peer;
  79. nametext.text = peer.NickName;
  80. textMic.text = "0.02";
  81. VolumnGo.SetActive(false);
  82. if (!string.IsNullOrEmpty(peer.Avatar))
  83. {
  84. for (int i = 0; i < CommonMethod.UserAvatarsList.Count; i++)
  85. {
  86. if (peer.Avatar == CommonMethod.UserAvatarsList[i].Url)
  87. {
  88. string path = "Avatar/" + CommonMethod.UserAvatarsList[i].Id + "Avatar";
  89. touxiang.icon = Resources.Load(path) as Texture2D;
  90. touxiang.AutoSetSprite();
  91. }
  92. }
  93. }
  94. if (_peer.IsSlef)
  95. {
  96. nametext.text = "<color=yellow>" + peer.NickName + "</color>";
  97. openAudio.GetComponent<UnityEngine.UI.Image>().color = new Color(255, 0, 0, 255);
  98. openVideo.GetComponent<UnityEngine.UI.Image>().color = new Color(255, 0, 0, 255);
  99. Invoke("checkAudio", 0.5f);
  100. // Invoke("checkVideo", 1f);
  101. }
  102. }
  103. private void checkVideo()
  104. {
  105. if (CustomInfo.isSendVideo&& (!CustomInfo.isSendAudio|| StudioRtc.Instance.me.cIdA != ""))
  106. {
  107. OpenVideo();
  108. }
  109. else if(CustomInfo.isSendAudio&& CustomInfo.isSendVideo)
  110. {
  111. Invoke("checkVideo", 0.5f);
  112. }
  113. }
  114. private void checkAudio()
  115. {
  116. if (CustomInfo.isSendAudio)
  117. {
  118. openMic();
  119. checkVideo();
  120. }
  121. else
  122. {
  123. if (CustomInfo.isSendVideo)
  124. {
  125. OpenVideo();
  126. }
  127. }
  128. }
  129. private void OnEnable()
  130. {
  131. if (_peer!=null&&_peer.IsSlef)
  132. {
  133. if (StudioRtc.Instance != null && StudioRtc.Instance.me != null && StudioRtc.Instance.me.cIdA != "")
  134. {
  135. openAudioEffect();
  136. }
  137. else
  138. {
  139. closeAudioEffect();
  140. }
  141. if (StudioRtc.Instance != null && StudioRtc.Instance.me != null && StudioRtc.Instance.me.cIdV != "")
  142. {
  143. openVideoEffect();
  144. }
  145. else
  146. {
  147. closeVideoEffect();
  148. }
  149. }
  150. }
  151. private void closeAudioEffect()
  152. {
  153. pauseCerAudio.SetActive(true);
  154. pauseAudio.SetActive(false);
  155. openAudio.SetActive(true);
  156. closeAudio.SetActive(false);
  157. VolumnGo.SetActive(false);
  158. if (this._peer.PeerId == CommonMethod.MyPeer.PeerId)
  159. AgoraVideoAudioManager.Instance.EnableLoacalAudio(false);
  160. //else
  161. // AgoraVideoAudioManager.Instance.MuteRemoteAudioStream(_peer.PeerId, false);
  162. }
  163. private void openAudioEffect()
  164. {
  165. openAudio.SetActive(false);
  166. closeAudio.SetActive(true);
  167. VolumnGo.SetActive(true);
  168. if (cPeer != null)
  169. {
  170. volume = cPeer.volumn;
  171. if (cPeer.isCloseAudio)
  172. {
  173. pauseCerAudio.SetActive(false);
  174. pauseAudio.SetActive(true);
  175. }
  176. }
  177. textMic.text = volume.ToString("F2");
  178. if (this._peer.PeerId == CommonMethod.MyPeer.PeerId)
  179. AgoraVideoAudioManager.Instance.EnableLoacalAudio(true);
  180. //else
  181. // AgoraVideoAudioManager.Instance.MuteRemoteAudioStream(_peer.PeerId, true);
  182. }
  183. private void OnChangeInfo(string type, string id)
  184. {
  185. if (type == "video")
  186. {
  187. if (cPeer.isVideo)
  188. {
  189. openVideoEffect();
  190. if (cPeer.isCloseVideo)
  191. {
  192. StudioRtc.Instance.closeRev(cPeer.cIdV);
  193. }
  194. }
  195. else
  196. {
  197. closeVideoEffect();
  198. }
  199. }
  200. else
  201. {
  202. if (cPeer.isAudio)
  203. {
  204. openAudioEffect();
  205. if (cPeer.isCloseAudio)
  206. {
  207. StudioRtc.Instance.closeRev(cPeer.cIdA);
  208. }
  209. }
  210. else
  211. {
  212. closeAudioEffect();
  213. }
  214. }
  215. }
  216. public GameObject openAudio;
  217. public GameObject closeAudio;
  218. public GameObject VolumnGo;
  219. public void openMic()
  220. {
  221. if (_peer.IsSlef)
  222. {
  223. StudioRtc.Instance.sendMic(true);
  224. volume = StudioRtc.Instance.me.volumn;
  225. openAudioEffect();
  226. }
  227. else
  228. {
  229. if (cPeer != null&&cPeer.isAudio&&cPeer.isCloseAudio)
  230. {
  231. pauseCerAudio.SetActive(true);
  232. pauseAudio.SetActive(false);
  233. StudioRtc.Instance.startRev(cPeer.cIdA);
  234. cPeer.isCloseAudio = false;
  235. // cPeer.setInfo("audio", cPeer.cIdA,true);
  236. AgoraVideoAudioManager.Instance.MuteRemoteAudioStream(_peer.PeerId, false);
  237. }
  238. }
  239. }
  240. public float volume = 0.2f;
  241. public Text textMic;
  242. public void addMic()
  243. {
  244. volume += 0.1f;
  245. if (_peer.IsSlef)
  246. {
  247. StudioRtc.Instance.setVolume(volume);
  248. }
  249. else
  250. {
  251. if (cPeer != null&& cPeer.cIdA!="")
  252. {
  253. StudioRtc.Instance.setVolume(cPeer.cIdA,volume);
  254. }
  255. }
  256. textMic.text = volume.ToString("F2");
  257. }
  258. public void redMic()
  259. {
  260. volume -= 0.1f;
  261. if (_peer.IsSlef)
  262. {
  263. StudioRtc.Instance.setVolume(volume);
  264. }
  265. else
  266. {
  267. if (cPeer != null && cPeer.cIdA != "")
  268. {
  269. StudioRtc.Instance.setVolume(cPeer.cIdA, volume);
  270. }
  271. }
  272. textMic.text = volume.ToString("F2");
  273. }
  274. public void closeMic()
  275. {
  276. if (_peer.IsSlef)
  277. {
  278. if(StudioRtc.Instance.me.cIdA!="")
  279. {
  280. WSHandler.Rtc.closeProducer(StudioRtc.Instance.me.cIdA);
  281. StudioRtc.Instance.sendMic(false);
  282. closeAudioEffect();
  283. StudioRtc.Instance.me.cIdA = "";
  284. }
  285. closeAudioEffect();
  286. }
  287. else
  288. {
  289. if (cPeer != null && cPeer.isAudio && !cPeer.isCloseAudio)
  290. {
  291. pauseCerAudio.SetActive(false);
  292. pauseAudio.SetActive(true);
  293. StudioRtc.Instance.closeRev(cPeer.cIdA);
  294. cPeer.isCloseAudio = true;
  295. AgoraVideoAudioManager.Instance.MuteRemoteAudioStream(_peer.PeerId, true);
  296. // cPeer.setInfo("audio", cPeer.cIdA, false);
  297. }
  298. }
  299. }
  300. public GameObject pauseCerAudio;
  301. public GameObject pauseCerVideo;
  302. public GameObject pauseAudio;
  303. public GameObject pauseVideo;
  304. public GameObject openVideo;
  305. public GameObject closeVideo;
  306. public void OpenVideo()
  307. {
  308. if (_peer.IsSlef)
  309. {
  310. Debug.Log("OpenVideo==>" + StudioRtc.Instance.me.cIdV);
  311. StudioRtc.Instance.startCamera();
  312. // videoBG.gameObject.SetActive(true);
  313. videoBG.material.mainTexture = StudioRtc.Instance.rt;
  314. StudioRtc.Instance.sendVideo(true);
  315. openVideoEffect();
  316. }
  317. else
  318. {
  319. if (cPeer != null && cPeer.isVideo && cPeer.isCloseVideo)
  320. {
  321. pauseCerVideo.SetActive(true);
  322. pauseVideo.SetActive(false);
  323. StudioRtc.Instance.startRev(cPeer.cIdV);
  324. cPeer.isCloseVideo = false;
  325. // cPeer.setInfo("audio", cPeer.cIdA,true);
  326. }
  327. }
  328. }
  329. public void CloseVideo()
  330. {
  331. if (_peer.IsSlef)
  332. {
  333. if (StudioRtc.Instance.me.cIdV != "")
  334. {
  335. Debug.Log("CloseVideo==>" + StudioRtc.Instance.me.cIdV);
  336. WSHandler.Rtc.closeProducer(StudioRtc.Instance.me.cIdV);
  337. StudioRtc.Instance.stopCamera();
  338. StudioRtc.Instance.sendVideo(false);
  339. closeVideoEffect();
  340. StudioRtc.Instance.me.cIdV = "";
  341. }
  342. closeVideoEffect();
  343. }
  344. else
  345. {
  346. if (cPeer != null && cPeer.isVideo && cPeer.cIdV != "" && !cPeer.isCloseVideo)
  347. {
  348. pauseCerVideo.SetActive(false);
  349. pauseVideo.SetActive(true);
  350. StudioRtc.Instance.closeRev(cPeer.cIdV);
  351. cPeer.isCloseVideo = true;
  352. // cPeer.setInfo("audio", cPeer.cIdA,true);
  353. }
  354. }
  355. }
  356. private void closeVideoEffect()
  357. {
  358. openVideo.SetActive(true);
  359. closeVideo.SetActive(false);
  360. // videoBG.gameObject.SetActive(false);
  361. if (cPeer != null)
  362. {
  363. pauseCerVideo.SetActive(true);
  364. pauseVideo.SetActive(false);
  365. }
  366. if (this._peer.PeerId == CommonMethod.MyPeer.PeerId)
  367. AgoraVideoAudioManager.Instance.EnableLocalVideo(false);
  368. }
  369. private void openVideoEffect()
  370. {
  371. openVideo.SetActive(false);
  372. closeVideo.SetActive(true);
  373. if (cPeer != null)
  374. {
  375. // videoBG.gameObject.SetActive(true);
  376. if (cPeer.isCloseVideo)
  377. {
  378. pauseCerVideo.SetActive(false);
  379. pauseVideo.SetActive(true);
  380. }
  381. }
  382. if (this._peer.PeerId == CommonMethod.MyPeer.PeerId)
  383. AgoraVideoAudioManager.Instance.EnableLocalVideo(true);
  384. }
  385. }