123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414 |
- 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)
- {
-
-
- if (cPeer != null)
- {
- return true;
- }
- else
- {
- cPeer = AgoraRTCManager.Instance.GetCustomPeer(_peer.PeerId);
- return true;
- }
- }
- return false;
- }
- void StateUpdata()
- {
- if (cPeer.isVideo)
- {
- if (cPeer.isOpenVideo)
- {
- openVideo.SetActive(false);
- closeVideo.SetActive(false);
- pauseVideo.SetActive(true);
- VideoEmpty.gameObject.SetActive(true);
- agoraShowVideo.gameObject.SetActive(false);
- }
- else
- {
- openVideo.SetActive(false);
- closeVideo.SetActive(true);
- pauseVideo.SetActive(false);
- VideoEmpty.gameObject.SetActive(false);
- agoraShowVideo.gameObject.SetActive(true);
- }
- }
- else
- {
- openVideo.SetActive(true);
- closeVideo.SetActive(false);
- pauseVideo.SetActive(false);
- VideoEmpty.gameObject.SetActive(true);
- agoraShowVideo.gameObject.SetActive(false);
- }
- if (cPeer.isAudio)
- {
- if (cPeer.isOpenAduio)
- {
- 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 Update()
- {
- checkRtc();
- StateUpdata();
- }
- bool isRtcInit = false;
- AgoraCustomPeer cPeer;
- public void setVideo(string peerid)
- {
- _peer = UserItemList.Instance.GetPeer(peerid);
- nametext.text = _peer.NickName;
- if (peerid == CommonMethod.MyPeer.PeerId)
- {
-
- isRtcInit = true;
- return;
- }
- else
- {
- Debug.Log(" Peerid != MyPeer" + peerid);
- }
-
- }
- public void addMic()
- {
- volume += 0.1f;
-
- }
- public void redMic()
- {
- volume -= 0.1f;
-
- }
- public void openMic()
- {
- if (_peer.IsSlef)
- {
- AgoraRTCManager.Instance.MuteLocalAudioStream(true);
- }
- else
- {
- AgoraRTCManager.Instance.MuteRemoteAudioStream(cPeer.peerId, true);
- }
- }
- public void closeMic()
- {
- if (_peer.IsSlef)
- {
- AgoraRTCManager.Instance.MuteLocalAudioStream(false);
- }
- else
- {
- AgoraRTCManager.Instance.MuteRemoteAudioStream(cPeer.peerId, false);
- }
- }
- public void OpenVideo()
- {
- if (_peer.IsSlef)
- {
- AgoraRTCManager.Instance.MuteLocalVideoStream(true);
- }
- else
- {
- AgoraRTCManager.Instance.MuteRemoteVideoStream(cPeer.peerId, true);
- }
- }
- public void CloseVideo()
- {
- if (_peer.IsSlef)
- {
- AgoraRTCManager.Instance.MuteLocalVideoStream(false);
- }
- else
- {
- AgoraRTCManager.Instance.MuteRemoteVideoStream(cPeer.peerId, 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;
- AgoraRTCManager.Instance.AgoarRawImageVideoView(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();
- }
- }
- }
- }
- }
|