using LitJson; using SUIFW; using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using UnityEngine.Video; using XRTool.Util; public class RoomMainForms : BaseUIForms { public static RoomMainForms Instance; public static string NOUSER = "nouser"; public static string userId; public bool isSendAudio = true; public bool isSendVideo = true; public bool isCloseView = true; public RawImage bigView; public RawImage selfVideo; public RawImage jieTuBG; public RectTransform TempObj; public Button selfVideoBtn; public Button uploadBtn; public Button switchBtn; public Button guaDuanBtn; public Button inviteBtn; public Button audioOpenBtn; public Button audioCloseBtn; public Button videoOpenBtn; public Button videoCloseBtn; public Text titleRoomName; public Text titleRoomNum; public Text titleRoomPeopleCount; public Button openCoordinateBtn; public Button closeCoordinateBtn; public GameObject AllUserBtn; public GameObject AllNeedHideUI; public GameObject AllNeedShowUI; public bool isOpen = false; public Text userNumText; public bool isSwithVideo = false; public GameObject UpFilePop; public Button upImageBtn; public Button upVideoBtn; public Button cancelBtn; public GameObject Invite; private void Awake() { Instance = this; WSHandler.Rtc.onRtcState += onRtcState; WSHandler.Rtc.OnChangeOwner += ChangeOwner; guaDuanBtn.onClick.AddListener(ClickOnGuaDuan); audioOpenBtn.onClick.AddListener(()=> { sendAudio(true); } ); audioCloseBtn.onClick.AddListener(()=> { sendAudio(false); }); videoOpenBtn.onClick.AddListener(()=> { sendVideo(true); }); videoCloseBtn.onClick.AddListener(() => { sendVideo(false); }); openCoordinateBtn.onClick.AddListener(ClickOnOpenCoordinate); closeCoordinateBtn.onClick.AddListener(ClickOnCloseCoordinate); switchBtn.onClick.AddListener(Swith); selfVideoBtn.onClick.AddListener(CkickOnSelfVideoBtn); upImageBtn.onClick.AddListener(CallAndroidImage); upVideoBtn.onClick.AddListener(CallAndroidVideo); cancelBtn.onClick.AddListener(ClickOnCanel); uploadBtn.onClick.AddListener(ClickOnUpFileBtn); inviteBtn.onClick.AddListener(ClickInviteBtn); } private void Start() { OpenBlack(); } private void Update() { if (userId != NOUSER) { if (isOpen) { if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Ended) { Debug.Log(Input.GetTouch(0).position.x + "AAA" + Input.GetTouch(0).position.y); Debug.Log(Screen.width + "BBB" + Screen.height); string x = (Input.GetTouch(0).position.x / Screen.width).ToString(); string y = ((Screen.height - Input.GetTouch(0).position.y) / Screen.height).ToString(); Debug.Log(x + "CCC" + y); WSHandler.Rtc.coordinate(10000, x, y, userId); } } else { if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Ended) { Debug.Log(Input.GetTouch(0).position.x + "DDD" + Input.GetTouch(0).position.y); Debug.Log(Screen.width + "EEE" + Screen.height); string x = (Input.GetTouch(0).position.x / Screen.width).ToString(); string y = ((Screen.height - Input.GetTouch(0).position.y) / Screen.height).ToString(); Debug.Log(x + "FFF" + y); WSHandler.Rtc.coordinate(10001, x, y, userId); } } } } private void OnEnable() { Screen.orientation = ScreenOrientation.LandscapeLeft; sendAudio(UserInfo.systemDatas.Mic == 1 ? true : false); sendVideo(UserInfo.systemDatas.Camera == 1 ? true : false); } private void OnDisable() { Screen.orientation = ScreenOrientation.Portrait; } #region 窗体生命周期 public override void Display() { base.Display(); Init(); } public override void Redisplay() { base.Redisplay(); } public override void Freeze() { base.Freeze(); } public override void Hiding() { base.Hiding(); } #endregion public void Init() { AllNeedHideUI.SetActive(true); isSwithVideo = false; setBigView(RemoteRtc.Instance.NoUser, NOUSER); AllUserBtn.SetActive(false); if (!string.IsNullOrEmpty(RoomMainInfo.RoomOwner)) { titleRoomName.text = RoomMainInfo.RoomOwner; } titleRoomPeopleCount.text = "房间人数:" + RoomMainInfo.roomPeopleNum + "/" + RoomMainInfo.roomMaxPeopleNum; userNumText.text = (RoomMainInfo.roomPeopleNum - 1).ToString(); titleRoomNum.text = "房间号:" + RoomMainInfo.roomNum; AgoraVideoAudioManager.Instance.SetChinnelName(RoomMainInfo.roomNum); AgoraVideoAudioManager.Instance.JoinChannel(); isSendAudio = CustomInfo.isSendAudio; isSendVideo = CustomInfo.isSendVideo; isCloseView = CustomInfo.isCloseView; if (isSendAudio) { TimerMgr.Instance.CreateTimer(() => { RemoteRtc.Instance.sendMic(true); }, 1f); } if (isSendVideo) { TimerMgr.Instance.CreateTimer(() => { RemoteRtc.Instance.startCamera(); RemoteRtc.Instance.sendVideo(true); }, 1f); } change(); isOpen = false; openCoordinateBtn.gameObject.SetActive(true); closeCoordinateBtn.gameObject.SetActive(false); if (RoomOtherUser.Instance) { RoomOtherUser.Instance.UpdateList(); } if (RoomFile.Instance) { RoomFile.Instance.Init(); } if (UserInfo.systemDatas.Camera==1) StartCoroutine(ShowLoacalVideo(2f)); } IEnumerator ShowLoacalVideo( float times) { yield return new WaitForSeconds(times); AgoraVideoAudioManager.Instance.ShowLocalView(selfVideo); } public void ShowOneVideo() { // AgoraVideoAudioManager.Instance.ShowOneView(bigView); } public void ShowUserBtns() { AllUserBtn.SetActive(true); } public void HideUserBtns() { AllUserBtn.SetActive(false); } private void ChangeOwner(JsonData data) { string peerId = data["data"]["peerId"].ToString(); string nickName = data["data"]["nickName"].ToString(); string isCreator = data["data"]["isCreator"].ToString(); if (peerId == UserInfo.UnionId && isCreator == "1") { RoomMainInfo.isCreator = "0"; RoomMainInfo.RoomOwner = "用户" + nickName + "的房间"; //if (FileList.Instance && RoomFile.Instance) //{ // if (FileList.Instance.transform.childCount > 0) // { // RoomFile.Instance.ShowClearBtn(); // } //} } titleRoomName.text = "用户" + nickName + "的房间"; titleRoomPeopleCount.text = "房间人数:" + RoomMainInfo.roomPeopleNum + "/" + 10; RoomOtherUser.Instance.UpdateList(); } public void setBigView(Texture tex, string id) { if (id != NOUSER) { CustomPeer cPeer = (CustomPeer)RemoteRtc.Instance.customPeerList.getPeer(id); if (cPeer.isCloseVideo || !cPeer.isVideo) { ClickOnCloseCoordinate(); tex = RemoteRtc.Instance.NoView; } RoomOtherUser.ShowAction?.Invoke(id); } userId = id; // bigView.texture = tex; } private string dcetype; private void onRtcState(string str) { if (!string.IsNullOrEmpty(RoomMainInfo.RoomOwner)) { titleRoomName.text = RoomMainInfo.RoomOwner; } titleRoomPeopleCount.text = "房间人数:" + RoomMainInfo.roomPeopleNum + "/" + RoomMainInfo.roomMaxPeopleNum; userNumText.text = (RoomMainInfo.roomPeopleNum - 1).ToString(); titleRoomNum.text = "房间号:" + RoomMainInfo.roomNum; switch (str) { case "closeProducer": if (dcetype == "video") { RemoteRtc.Instance.me.cIdV = ""; } else { RemoteRtc.Instance.me.cIdA = ""; } break; } } void change() { if (isSendVideo) { videoOpenBtn.gameObject.SetActive(false); videoCloseBtn.gameObject.SetActive(true); } else { videoOpenBtn.gameObject.SetActive(true); videoCloseBtn.gameObject.SetActive(false); } if (isSendAudio) { audioOpenBtn.gameObject.SetActive(false); audioCloseBtn.gameObject.SetActive(true); } else { audioOpenBtn.gameObject.SetActive(true); audioCloseBtn.gameObject.SetActive(false); } } public void LeaveRoom() { WSHandler.roomRtcCloes(); WSHandler.Office.ChangeUserType(UserInfo.BusyType); CloseOrReturnUIForms(); UIManager.GetInstance().ShowUIForms(SysConst.MainPanelForms); if (isswitch) { OpenBlack(); } //if (InviteDlg.Instance) //{ // InviteDlg.Instance.InvitingUnionIdList.Clear(); //} if (RoomFile.Instance) { RoomFile.Instance.ClearAllFileItem(); } AgoraVideoAudioManager.Instance.LeaveChannel(); } public void CkickOnSelfVideoBtn() { isSwithVideo = !isSwithVideo; if (isSwithVideo) { SwitchVideoBig(); } else { SwitchVideoSmall(); } } private void SwitchVideoBig() { bigView.rectTransform.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Right, 47.51f, 889f); bigView.rectTransform.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Top, 169.6f, 500f); selfVideo.rectTransform.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Left, 0, 0); selfVideo.rectTransform.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Top, 0, 0); selfVideo.rectTransform.anchorMin = Vector2.zero; selfVideo.rectTransform.anchorMax = Vector2.one; selfVideo.transform.SetAsFirstSibling(); } private void SwitchVideoSmall() { selfVideo.rectTransform.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Right, 47.51f, 889f); selfVideo.rectTransform.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Top, 169.6f, 500f); bigView.rectTransform.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Left, 0, 0); bigView.rectTransform.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Top, 0, 0); bigView.rectTransform.anchorMin = Vector2.zero; bigView.rectTransform.anchorMax = Vector2.one; bigView.transform.SetAsFirstSibling(); } private void ClickOnOpenCoordinate() { //if (userId == NOUSER || bigView.texture == RemoteRtc.Instance.NoUser || bigView.texture == RemoteRtc.Instance.NoView) //{ // return; //} if(userId == NOUSER || bigView.texture== AgoraVideoAudioManager.Instance.bg) { return; } else { isOpen = true; openCoordinateBtn.gameObject.SetActive(false); closeCoordinateBtn.gameObject.SetActive(true); } } public void ClickOnCloseCoordinate() { isOpen = false; openCoordinateBtn.gameObject.SetActive(true); closeCoordinateBtn.gameObject.SetActive(false); } private void ClickOnGuaDuan() { ShowUIForms(SysConst.PopForms); PopForms.Instance.ShowPublic(PopType.PopTwo, RtcStrConfig.roomPeopleLeave, "确定", () => { LeaveRoom(); }, "取消"); } private void CallAndroidImage() { NativeGallery.GetVideoFromGallery((string path) => { Debug.Log("Video path: " + path); test.path = path; WSHandler.Rtc.uploadCert(); if (RoomMainForms.Instance) { RoomMainForms.Instance.ClickOnCanel(); } }); /* if (test.Instance) { test.Instance.CallAndroidImage(); }*/ } private void CallAndroidVideo() { NativeGallery.GetImageFromGallery((string path) => { Debug.Log("Image path: " + path); test.path = path; WSHandler.Rtc.uploadCert(); if (RoomMainForms.Instance) { RoomMainForms.Instance.ClickOnCanel(); } }); /* if (test.Instance) { test.Instance.CallAndroidVideo(); }*/ } private void ClickOnUpFileBtn() { UpFilePop.SetActive(true); } public void ClickOnCanel() { UpFilePop.SetActive(false); } public void ClickInviteBtn() { Invite.SetActive(true); InviteDlg.Instance.ShowHistory(); } public WebCamTexture webTex; private string deviceName; private bool isswitch = true; IEnumerator CallCamera() { isswitch = true; yield return Application.RequestUserAuthorization(UserAuthorization.WebCam); if (Application.HasUserAuthorization(UserAuthorization.WebCam)) { WebCamDevice[] devices = WebCamTexture.devices; deviceName = devices[1].name; if (webTex != null) { webTex.Stop(); webTex = null; } //设置摄像机摄像的区域 webTex = new WebCamTexture(deviceName, 1920, 1080, CustomInfo.FPS); RemoteRtc.Instance.meshRender.texture = webTex; selfVideo.texture = webTex; webTex.Play();//开始摄像 } } IEnumerator CallCamera1() { isswitch = false; yield return Application.RequestUserAuthorization(UserAuthorization.WebCam); if (Application.HasUserAuthorization(UserAuthorization.WebCam)) { WebCamDevice[] devices = WebCamTexture.devices; deviceName = devices[0].name; if (webTex != null) { webTex.Stop(); webTex = null; } //设置摄像机摄像的区域 webTex = new WebCamTexture(deviceName, 1920, 1080, CustomInfo.FPS); RemoteRtc.Instance.meshRender.texture = webTex; selfVideo.texture = webTex; webTex.Play();//开始摄像 } } public void Swith() { AgoraVideoAudioManager.Instance.SwitchCamera(isswitch); isswitch = !isswitch; if (isswitch) { OpenBlack(); } else { OpenFront(); } } public void OpenFront() { //StopCoroutine(CallCamera1()); //StartCoroutine(CallCamera()); //RemoteRtc.Instance.meshRender.transform.localEulerAngles = new Vector3(180.0f, 0.0f, -180.0f); //selfVideo.transform.localEulerAngles = new Vector3(180.0f, 0.0f, -180.0f); } public void OpenBlack() { //StopCoroutine(CallCamera()); //StartCoroutine(CallCamera1()); //RemoteRtc.Instance.meshRender.transform.localEulerAngles = new Vector3(0.0f, 0.0f, 0.0f); //selfVideo.transform.localEulerAngles = new Vector3(0.0f, 0.0f, 0.0f); } public void ShowRoomissolvePop() { ShowUIForms(SysConst.PopForms); PopForms.Instance.ShowPublic(PopType.PopOne, RtcStrConfig.roomdissolve, "知道了"); } public void sendVideo( bool state) { if(state) openVideoEffect(); else closeVideoEffect(); AgoraVideoAudioManager.Instance.EnableLocalVideo(state); //if (RemoteRtc.Instance.me.cIdV == "") //{ // dcetype = "video"; // RemoteRtc.Instance.startCamera(); // RemoteRtc.Instance.sendVideo(true); // RemoteRtc.Instance.me.cIdV = ""; // openVideoEffect(); // AgoraVideoAudioManager.Instance.EnableLocalVideo(true); //} //else //{ // dcetype = "video"; // WSHandler.Rtc.closeProducer(RemoteRtc.Instance.me.cIdV); // RemoteRtc.Instance.stopCamera(); // RemoteRtc.Instance.sendVideo(false); // closeVideoEffect(); // AgoraVideoAudioManager.Instance.EnableLocalVideo(false); //} /* * 时间的跨度再次越过了半年 */ } public void sendAudio( bool state) { if(state) { openAudioEffect(); } else { closeAudioEffect(); } AgoraVideoAudioManager.Instance.EnableLoacalAudio(state); //if (RemoteRtc.Instance.me.cIdA != "") //{ // dcetype = "audio"; // WSHandler.Rtc.closeProducer(RemoteRtc.Instance.me.cIdA); // RemoteRtc.Instance.sendMic(false); // RemoteRtc.Instance.me.cIdA = ""; // closeAudioEffect(); // AgoraVideoAudioManager.Instance.EnableLoacalAudio(false); //} //else //{ // dcetype = "audio"; // RemoteRtc.Instance.sendMic(true); // openAudioEffect(); // AgoraVideoAudioManager.Instance.EnableLoacalAudio(true); //} } private void closeVideoEffect() { isSendVideo = false; videoOpenBtn.gameObject.SetActive(true); videoCloseBtn.gameObject.SetActive(false); } private void openVideoEffect() { isSendVideo = true; videoOpenBtn.gameObject.SetActive(false); videoCloseBtn.gameObject.SetActive(true); } private void closeAudioEffect() { isSendAudio = false; audioOpenBtn.gameObject.SetActive(true); audioCloseBtn.gameObject.SetActive(false); } private void openAudioEffect() { isSendAudio = true; audioOpenBtn.gameObject.SetActive(false); audioCloseBtn.gameObject.SetActive(true); } public void CkickOnJieTuBtn() { AllNeedHideUI.SetActive(false); //if (isSwithVideo) //{ // // selfVideo.gameObject.SetActive(false); // jieTuBG.texture = selfVideo.texture; //} //else //{ // bigView.gameObject.SetActive(false); // // jieTuBG.texture = bigView.texture; //} Texture2D texture; if (isSwithVideo) { texture = selfVideo.texture as Texture2D; jieTuBG.rectTransform.localEulerAngles = selfVideo.rectTransform.localEulerAngles; } else { texture = bigView.texture as Texture2D; jieTuBG.rectTransform.localEulerAngles = bigView.rectTransform.localEulerAngles; } // 创建一个新的2D纹理 Texture2D newTexture = new Texture2D(texture.width, texture.height, TextureFormat.RGB24, false); // 将截图复制到新的2D纹理中 newTexture.SetPixels(texture.GetPixels()); newTexture.Apply(); jieTuBG.texture = newTexture; bigView.gameObject.SetActive(false); selfVideo.gameObject.SetActive(false); jieTuBG.gameObject.SetActive(true); } public void ShowShotsAllBtn() { AllNeedShowUI.SetActive(true); } public void JieTuWancheng() { AllNeedHideUI.SetActive(true); AllNeedShowUI.SetActive(false); if (!bigView.gameObject.activeSelf) { bigView.gameObject.SetActive(true); } if (!selfVideo.gameObject.activeSelf) { selfVideo.gameObject.SetActive(true); } jieTuBG.gameObject.SetActive(false); } private void OnDestroy() { WSHandler.Rtc.onRtcState -= onRtcState; } }