123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660 |
- using BeinLab.Util;
- using ShadowStudio.Mgr;
- using ShadowStudio.UI;
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- using XRTool.Util;
- using XRTool.WorldUI;
- using static BoundingBox;
- namespace ShadowStudio.Model
- {
- /// <summary>
- /// 用户的视频通话窗口
- /// </summary>
- public class PlayerViewComponent : UIComponent
- {
- public static string viewId = "ArtId_PlayerView";
- public static Dictionary<string, ArtContainerHandler> viewDic = new Dictionary<string, ArtContainerHandler>();
- private string peerid;
- private bool isInit = false;
- private Peer _peer;
- public static event Action<string, bool> ViewChange;
- /// <summary>
- /// 创建一个视频通话窗口
- /// 即时同步
- /// </summary>
- /// <param name="peerid">点击的用户的id</param>
- /// <param name="target"></param>
- public static void CreateViewComponent(string peerid, Transform target, bool isDel = false)
- {
- if (viewDic.ContainsKey(peerid))
- {
- return;
- }
- GoodsInfo data = new GoodsInfo();
- var info = ArtInfoMgr.Instance.GetArtInfo(viewId);
- data.art_id = viewId;
- data.id = UnityEngine.Random.Range(100000, 999999999);
- if (target)
- {
- data.goods_info = UnityUtil.TransferToString(target, 2);
- }
- else
- {
- data.goods_info = UnityUtil.ArtTransferInfo(info);
- }
- data.goods_name = peerid;
- data.rid = int.Parse(UserInfoMgr.Instance.userConfig.Id);
- data.scene_id = -1;
- ///创建本地窗口
- ArtInfoMgr.Instance.SyncGoods(data);
- ///
- ///ArtInfoMgr.Instance.SendTransfer(data);
- if (target && isDel)
- {
- Destroy(target.gameObject);
- }
-
- }
- private bool checkRtc()
- {
- if (_peer != null && StudioRtc.Instance != null && StudioRtc.Instance.customPeerList != null)
- {
- cPeer = (CustomPeer)StudioRtc.Instance.customPeerList.getPeerName(_peer.PeerId);
- if (cPeer != null)
- {
- // Debug.Log("cPeer Not NULL" + _peer.PeerId);
- cPeer.onChangeInfo -= OnChangeInfo;
- cPeer.onChangeInfo += OnChangeInfo;
- if (!_peer.IsSlef)
- {
- if (cPeer.isAudio && cPeer.cIdA != "")
- {
- openAudioEffect();
- }
- else
- {
- closeAudioEffect();
- }
- if (cPeer.isVideo && cPeer.cIdV != "")
- {
- openVideoEffect();
- }
- else
- {
- closeVideoEffect();
- }
- }
- }
- if (cPeer != null)
- {
- return true;
- }
- }
- // Debug.Log("cPeer is NULL" + _peer.PeerId);
- return false;
- }
- void StateUpdata()
- {
- if (cPeer == null)
- return;
- if (cPeer.isVideo)
- {
- if (cPeer.isCloseVideo)
- {
- openVideo.SetActive(false);
- closeVideo.SetActive(false);
- pauseVideo.SetActive(true);
- VideoEmpty.gameObject.SetActive(true);
- }
- else
- {
- openVideo.SetActive(false);
- closeVideo.SetActive(true);
- pauseVideo.SetActive(false);
- VideoEmpty.gameObject.SetActive(false);
- }
- }
- else
- {
- openVideo.SetActive(true);
- closeVideo.SetActive(false);
- pauseVideo.SetActive(false);
- VideoEmpty.gameObject.SetActive(true);
- }
- if (cPeer.isAudio)
- {
- if (cPeer.isCloseAudio)
- {
- openAudio.SetActive(false);
- closeAudio.SetActive(false);
- pauseAudio.SetActive(true);
- }
- else
- {
- openAudio.SetActive(false);
- closeAudio.SetActive(true);
- pauseAudio.SetActive(false);
- }
- }
- else
- {
- openAudio.SetActive(true);
- closeAudio.SetActive(false);
- pauseAudio.SetActive(false);
- }
- }
- public float volume = 0.2f;
- public Text textMic;
- public GameObject pauseCerAudio;
- public GameObject pauseCerVideo;
- public GameObject pauseAudio;
- public GameObject pauseVideo;
- public GameObject openVideo;
- public GameObject closeVideo;
- public MeshRenderer videoBG;
- public Text nametext;
- public XRIcon touxiang;
- public GameObject openAudio;
- public GameObject closeAudio;
- public GameObject VolumnGo;
- public RawImage agoraShowVideo;
- private void closeVideoEffect()
- {
- if(openVideo)
- {
- openVideo.SetActive(true);
- closeVideo.SetActive(false);
- VideoEmpty.gameObject.SetActive(true);
- if (cPeer != null)
- {
- pauseCerVideo.SetActive(true);
- pauseVideo.SetActive(false);
- }
- if (peerid == CommonMethod.MyPeer.PeerId)
- AgoraVideoAudioManager.Instance.EnableLocalVideo(false);
- }
- }
- private void openVideoEffect()
- {
- openVideo.SetActive(false);
- closeVideo.SetActive(true);
- if (cPeer != null)
- {
- ShowVideo.material.mainTexture = cPeer.tex;
- if (cPeer.isCloseVideo)
- {
- pauseCerVideo.SetActive(false);
- pauseVideo.SetActive(true);
- }
- }
- VideoEmpty.gameObject.SetActive(false);
- if (peerid == CommonMethod.MyPeer.PeerId)
- AgoraVideoAudioManager.Instance.EnableLocalVideo(true);
- }
- private void closeAudioEffect()
- {
- pauseCerAudio.SetActive(true);
- pauseAudio.SetActive(false);
- openAudio.SetActive(true);
- closeAudio.SetActive(false);
- VolumnGo.SetActive(false);
- if (peerid == CommonMethod.MyPeer.PeerId)
- AgoraVideoAudioManager.Instance.EnableLoacalAudio(false);
- //else
- // AgoraVideoAudioManager.Instance.MuteRemoteAudioStream(peerid, false);
- }
- private void openAudioEffect()
- {
- openAudio.SetActive(false);
- closeAudio.SetActive(true);
- VolumnGo.SetActive(true);
- if (cPeer != null)
- {
- volume = cPeer.volumn;
- if (cPeer.isCloseAudio)
- {
- pauseCerAudio.SetActive(false);
- pauseAudio.SetActive(true);
- }
- }
- textMic.text = volume.ToString("F2");
- if (peerid == CommonMethod.MyPeer.PeerId)
- AgoraVideoAudioManager.Instance.EnableLoacalAudio(true);
- //else
- // AgoraVideoAudioManager.Instance.MuteRemoteAudioStream(peerid, true);
- }
- private void OnChangeInfo(string type, string id)
- {
- if (type == "video")
- {
- if (cPeer.isVideo)
- {
- openVideoEffect();
- if (cPeer.isCloseVideo)
- {
- StudioRtc.Instance.closeRev(cPeer.cIdV);
- }
- }
- else
- {
- closeVideoEffect();
- }
- }
- else
- {
- if (cPeer.isAudio)
- {
- openAudioEffect();
- if (cPeer.isCloseAudio)
- {
- StudioRtc.Instance.closeRev(cPeer.cIdA);
- }
- }
- else
- {
- closeAudioEffect();
- }
- }
- }
- private void Update()
- {
- if (checkRtc() && !isRtcInit)
- {
- ShowVideo.material.mainTexture = cPeer.tex;
- isRtcInit = true;
- }
- StateUpdata();
- }
- bool isRtcInit = false;
- CustomPeer cPeer;
- public void setVideo(string peerid)
- {
- _peer = UserItemList.Instance.GetPeer(peerid);
- nametext.text = _peer.NickName;
- if (peerid == CommonMethod.MyPeer.PeerId)
- {
- if (StudioRtc.Instance.me.cIdA != "")
- {
- openAudioEffect();
- }
- else
- {
- closeAudioEffect();
- }
- if (StudioRtc.Instance.me.cIdV != "")
- {
- openVideoEffect();
- }
- else
- {
- closeVideoEffect();
- }
- ShowVideo.material.mainTexture = StudioRtc.Instance.rt;
- isRtcInit = true;
- return;
- }
- else
- {
- Debug.Log(" Peerid != MyPeer" + peerid);
- }
- if (checkRtc())
- {
- ShowVideo.material.mainTexture = cPeer.tex;
- }
- /*
- if (peerid == CommonMethod.MyPeer.PeerId)
- {
- ShowVideo.material.mainTexture = ConsoleDlg.Instance.vufora_camera.GetComponent<Camera>().targetTexture;
- return;
- }
- peer = UserItemList.Instance.GetPeer(peerid);
- if (peer != null)
- {
- ShowVideo.material.mainTexture = peer.CustomPeer.tex;
- }
- else
- {
- UnityLog.Instance.LogError("Cant Find peerid" + peerid);
- }
- */
- }
- public void addMic()
- {
- volume += 0.1f;
- if (_peer.IsSlef)
- {
- StudioRtc.Instance.setVolume(volume);
- }
- else
- {
- if (cPeer != null && cPeer.cIdA != "")
- {
- StudioRtc.Instance.setVolume(cPeer.cIdA, volume);
- }
- }
- textMic.text = volume.ToString("F2");
- }
- public void redMic()
- {
- volume -= 0.1f;
- if (_peer.IsSlef)
- {
- StudioRtc.Instance.setVolume(volume);
- }
- else
- {
- if (cPeer != null && cPeer.cIdA != "")
- {
- StudioRtc.Instance.setVolume(cPeer.cIdA, volume);
- }
- }
- textMic.text = volume.ToString("F2");
- }
- public void closeMic()
- {
- if (_peer.IsSlef)
- {
- if (StudioRtc.Instance.me.cIdA != "")
- {
- WSHandler.Rtc.closeProducer(StudioRtc.Instance.me.cIdA);
- StudioRtc.Instance.sendMic(false);
- closeAudioEffect();
- StudioRtc.Instance.me.cIdA = "";
- }
- }
- else
- {
- if (cPeer != null && cPeer.isAudio && cPeer.cIdA != "" && !cPeer.isCloseAudio)
- {
- pauseCerAudio.SetActive(false);
- pauseAudio.SetActive(true);
- StudioRtc.Instance.closeRev(cPeer.cIdA);
- cPeer.isCloseAudio = true;
- // cPeer.setInfo("audio", cPeer.cIdA, false);
- //AgoraVideoAudioManager.Instance.MuteRemoteAudioStream(peerid, true);
- }
- AgoraVideoAudioManager.Instance.MuteRemoteAudioStream(cPeer.name, false);
- }
- }
- public void openMic()
- {
- if (_peer.IsSlef)
- {
- StudioRtc.Instance.sendMic(true);
- volume = StudioRtc.Instance.me.volumn;
- openAudioEffect();
- }
- else
- {
- if (cPeer != null && cPeer.isAudio && cPeer.cIdA != "" && cPeer.isCloseAudio)
- {
- pauseCerAudio.SetActive(true);
- pauseAudio.SetActive(false);
- StudioRtc.Instance.startRev(cPeer.cIdA);
- cPeer.isCloseAudio = false;
- // cPeer.setInfo("audio", cPeer.cIdA,true);
- // AgoraVideoAudioManager.Instance.MuteRemoteAudioStream(peerid, false);
- }
- AgoraVideoAudioManager.Instance.MuteRemoteAudioStream(cPeer.name, true);
- }
- }
- public void OpenVideo()
- {
- agoraShowVideo.gameObject.SetActive(true);
- if (_peer.IsSlef)
- {
- StudioRtc.Instance.startCamera();
- // videoBG.gameObject.SetActive(true);
- //videoBG.material.mainTexture = StudioRtc.Instance.rt;
- StudioRtc.Instance.sendVideo(true);
- openVideoEffect();
- }
- else
- {
- if (cPeer != null && cPeer.isVideo && cPeer.cIdV != "" && cPeer.isCloseVideo)
- {
- pauseCerVideo.SetActive(true);
- pauseVideo.SetActive(false);
- StudioRtc.Instance.startRev(cPeer.cIdV);
- cPeer.isCloseVideo = false;
- // cPeer.setInfo("audio", cPeer.cIdA,true);
- }
- AgoraVideoAudioManager.Instance.MuteRemoteVideoStream(cPeer.name, true);
- }
- }
- public void CloseVideo()
- {
- agoraShowVideo.gameObject.SetActive(false);
- if (_peer.IsSlef)
- {
- if (StudioRtc.Instance.me.cIdV != "")
- {
- WSHandler.Rtc.closeProducer(StudioRtc.Instance.me.cIdV);
- StudioRtc.Instance.stopCamera();
- StudioRtc.Instance.sendVideo(false);
- closeVideoEffect();
- StudioRtc.Instance.me.cIdV = "";
- }
- }
- else
- {
- if (cPeer != null && cPeer.isVideo && cPeer.cIdV != "" && !cPeer.isCloseVideo)
- {
- pauseCerVideo.SetActive(false);
- pauseVideo.SetActive(true);
- StudioRtc.Instance.closeRev(cPeer.cIdV);
- cPeer.isCloseVideo = true;
- // cPeer.setInfo("audio", cPeer.cIdA,true);
- }
- AgoraVideoAudioManager.Instance.MuteRemoteVideoStream(cPeer.name, false);
- }
- }
- private Button closeBtn;
- public Button CloseBtn
- {
- get
- {
- if (!closeBtn)
- {
- closeBtn = UnityUtil.GetBreadthChild<Button>(transform, "CloseBtn");
- }
- return closeBtn;
- }
- }
- private MeshRenderer showVideo;
- public MeshRenderer ShowVideo
- {
- get
- {
- if (!showVideo)
- {
- showVideo = UnityUtil.GetBreadthChild<MeshRenderer>(transform, "ShowVideo");
- }
- return showVideo;
- }
- }
- private MeshRenderer videoEmpty;
- public MeshRenderer VideoEmpty
- {
- get
- {
- if (!videoEmpty)
- {
- videoEmpty = UnityUtil.GetBreadthChild<MeshRenderer>(transform, "VideoEmpty");
- }
- return videoEmpty;
- }
- }
- private void OnDestroy()
- {
- ViewChange?.Invoke(peerid, false);
- if (!string.IsNullOrEmpty(peerid) && viewDic.ContainsKey(peerid))
- {
- viewDic.Remove(peerid);
- }
- if (cPeer != null)
- cPeer.onChangeInfo -= OnChangeInfo;
- }
- private void Start()
- {
- if (CloseBtn)
- {
- //CloseBtn.onClick.AddListener(OnClickClose);
- }
- if (ArtInfoMgr.Instance != null)
- {
- ArtInfoMgr.Instance.UserChange += OnUserChange;
- }
- XBoundingBox boxb= this.GetComponent<XBoundingBox>();
- if(boxb)
- boxb.ActiveAxis= AxisType.None;
- AgoraVideoAudioManager.Instance.AddListShowView(peerid, agoraShowVideo);
- }
- /// <summary>
- /// 用户状态发生改变的时候
- /// </summary>
- /// <param name="peerId"></param>
- /// <param name="isJoin"></param>
- private void OnUserChange(string peerId, bool isJoin)
- {
- ///用户视频列表被创建,但用户退出
- ///删除用户视频列表
- if (viewDic.ContainsKey(peerId))
- {
- if (peerId == this.peerid && !isJoin)
- {
- OnClickClose();
- }
- }
- else
- {
- ///用户加入
- if (isJoin)
- {
- base.SendTransfer(2, 0);
- TimerMgr.Instance.CreateTimer(() =>
- {
- base.SendTransfer(2, 0);
- }, 3f);
- }
- }
- }
- /// <summary>
- /// 点击关闭按钮
- /// 删除此视频通话窗口
- /// 如何删除?发送额外消息
- /// 删除本地
- /// </summary>
- public void OnClickClose()
- {
- if (Container)
- {
- Container.data.goods_extended = "DES";
- base.SendTransfer(2, 0);
- }
- if (cPeer != null)
- cPeer.onChangeInfo -= OnChangeInfo;
- DestroyArt();
-
- }
- public override void DestroyArt()
- {
- base.DestroyArt();
- }
- public override void SendTransfer(int state, int status)
- {
- base.SendTransfer(state, status);
- //if (Container is WorldDlgContainer)
- //{
- // TimerMgr.Instance.CreateTimer(()=>
- // {
- // (Container as WorldDlgContainer).OnTransferSelect(false);
- // },0.02f);
- //}
- }
- /// <summary>
- /// 设置容器
- /// </summary>
- /// <param name="container"></param>
- /// <param name="art"></param>
- /// <param name="info"></param>
- public override void SetContainer(ArtContainerHandler container, UnityEngine.Object art, ArtInfo info)
- {
- base.SetContainer(container, art, info);
- RectTransform rt = GetComponent<RectTransform>();
- rt.offsetMax = Vector2.zero;
- rt.offsetMin = Vector2.zero;
- }
- public override void TransferSyn(GoodsInfo goods, bool smooth = true)
- {
- base.TransferSyn(goods, smooth);
- if (!isInit)
- {
- if (!viewDic.ContainsKey(goods.goods_name))
- {
- this.peerid = goods.goods_name;
- viewDic.Add(goods.goods_name, Container);
- setVideo(peerid);
- ///自身点击的,需要发送同步一次
- if (goods.rid.ToString() == UserInfoMgr.Instance.userConfig.Id)
- {
- SendTransfer(2, 0);
- }
- ViewChange?.Invoke(goods.goods_name, true);
- isInit = true;
-
- }
- }
- if (!string.IsNullOrEmpty(goods.goods_extended))
- {
- if (goods.goods_extended == "DES")
- {
- DestroyArt();
- }
- }
- }
- }
- }
|