VideoComponent.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. using BeinLab.Util;
  2. using DG.Tweening;
  3. using LitJson;
  4. using ShadowStudio.Tool;
  5. using System.Collections;
  6. using System.Collections.Generic;
  7. using UnityEngine;
  8. using UnityEngine.EventSystems;
  9. using UnityEngine.Video;
  10. using XRTool.Util;
  11. using XRTool.WorldUI;
  12. namespace ShadowStudio.Model
  13. {
  14. public class VideoComponent : UIComponent
  15. {
  16. private bool isMove;
  17. private Vector3 imageScale;
  18. private RenderTexture videoTexture;
  19. private XRVideoPlayer moviesPlayer;
  20. public float clickTime = 0.2f;
  21. private float pressTime;
  22. // 临时局域网版本用 PlayeBtn PauseBtn
  23. public SCButton PlayeBtn;
  24. public SCButton PauseBtn;
  25. public RenderTexture VideoTexture { get => videoTexture; set => videoTexture = value; }
  26. public XRVideoPlayer MoviesPlayer
  27. {
  28. get
  29. {
  30. if (!moviesPlayer)
  31. {
  32. //moviesPlayer = UnityUtil.GetBreadthChild<XRVideoPlayer>(Dlg.FullUIRoot, "XRVideoPlayer");
  33. moviesPlayer = GetComponent<XRVideoPlayer>();
  34. }
  35. return moviesPlayer;
  36. }
  37. }
  38. private void OnTransferSelect(bool isSelect)
  39. {
  40. if (!isSelect)
  41. {
  42. MoviesPlayer.hideControl();
  43. }
  44. else
  45. {
  46. MoviesPlayer.showControl();
  47. }
  48. }
  49. public override void SetContainer(ArtContainerHandler container, Object art, ArtInfo info)
  50. {
  51. base.SetContainer(container, art, info);
  52. if (art == null)
  53. {
  54. string targetUrl = info.Url;
  55. if (!targetUrl.StartsWith("http"))
  56. {
  57. targetUrl = ResourcesManager.ArtServerPath + "/" + targetUrl;
  58. }
  59. MoviesPlayer.VideoPlayer.url = targetUrl;
  60. }
  61. else if (art is VideoClip)
  62. {
  63. MoviesPlayer.VideoPlayer.clip = art as VideoClip;
  64. }
  65. MoviesPlayer.XRSlider.onValueChanged.AddListener(setVidoPlayerTime);
  66. MoviesPlayer.XRSlider.OnSliderDown.AddListener(OnSliderDown);
  67. MoviesPlayer.XRSlider.OnSliderUp.AddListener(OnSliderUp);
  68. imageScale = MoviesPlayer.Pause.transform.localScale;
  69. if (container && container.Transfer)
  70. {
  71. container.Transfer.EditTransfer += OnEditTransfer;
  72. }
  73. this.GetComponent<RectTransform>().offsetMax = Vector2.zero;
  74. this.GetComponent<RectTransform>().offsetMin = Vector2.zero;
  75. }
  76. private void OnEditTransfer(TransferState state)
  77. {
  78. if (state == TransferState.Stop)
  79. {
  80. OnTransferSelect(true);
  81. }
  82. else if (state == TransferState.Over)
  83. {
  84. OnTransferSelect(false);
  85. }
  86. }
  87. void setVidoPlayerTime(float f)
  88. {
  89. }
  90. public void OnSliderDown(PointerEventData eventData)
  91. {
  92. MoviesPlayer.isDown = true;
  93. }
  94. public void OnSliderUp(PointerEventData eventData)
  95. {
  96. MoviesPlayer.sliderChanged(MoviesPlayer.XRSlider.value);
  97. StopCoroutine(pointerUp(false));
  98. MoviesPlayer.isDown = true;
  99. StartCoroutine(pointerUp(true));
  100. MoviesPlayer.isDown = true;
  101. }
  102. IEnumerator pointerUp(bool isSend = true)
  103. {
  104. yield return new WaitForSeconds(1f);
  105. MoviesPlayer.isDown = false;
  106. if (isSend)
  107. {
  108. VideoState("vdFrame", MoviesPlayer.XRSlider.value.ToString());
  109. SendTransfer(0, (int)TransferState.Stop);
  110. //sendMessage();
  111. }
  112. }
  113. public void playVideoManager()
  114. {
  115. isMove = Time.time - pressTime < clickTime;
  116. if (!isMove)
  117. {
  118. if (MoviesPlayer.VideoPlayer.isPlaying)
  119. {
  120. PauseVideo();
  121. }
  122. else
  123. {
  124. PlayVideo();
  125. }
  126. }
  127. }
  128. public void PlayVideo(bool isSend = true)
  129. {
  130. if (!MoviesPlayer.VideoPlayer.isPlaying)
  131. {
  132. MoviesPlayer.Pause.transform.DOScale(imageScale * 1.1f, 0.3f).OnComplete<Tween>(() =>
  133. {
  134. MoviesPlayer.Pause.transform.localScale = Vector3.zero;
  135. }).OnKill<Tween>(() =>
  136. {
  137. MoviesPlayer.Pause.transform.localScale = Vector3.zero;
  138. });
  139. MoviesPlayer.VideoPlayer.Play();
  140. VideoState("vdFrame", MoviesPlayer.XRSlider.value.ToString());
  141. // MoviesPlayer.hideControl();
  142. }
  143. if (isSend)
  144. {
  145. VideoState("vdPlay", "1");
  146. //sendMessage();
  147. // SendTransfer(0, (int)TransferState.Stop);
  148. }
  149. }
  150. public void PauseVideo(bool isSend = true)
  151. {
  152. if (MoviesPlayer.VideoPlayer.isPlaying)
  153. {
  154. MoviesPlayer.Pause.transform.localScale = imageScale * 1.1f;
  155. MoviesPlayer.Pause.transform.DOScale(imageScale, 0.3f).OnComplete<Tween>(() =>
  156. {
  157. MoviesPlayer.Pause.transform.localScale = imageScale;
  158. }).OnKill<Tween>(() =>
  159. {
  160. MoviesPlayer.Pause.transform.localScale = imageScale;
  161. });
  162. MoviesPlayer.VideoPlayer.Pause();
  163. // VideoState("vdFrame", MoviesPlayer.XRSlider.value.ToString());
  164. }
  165. if (isSend)
  166. {
  167. VideoState("vdPlay", "0");
  168. //sendMessage();
  169. // SendTransfer(0, (int)TransferState.Stop);
  170. }
  171. }
  172. //public override void SendTransfer(int state, bool isEditting)
  173. //{
  174. // base.SendTransfer(state, isEditting);
  175. //}
  176. bool isPlay;
  177. public override void TransferSyn(GoodsInfo goods, bool smooth = true)
  178. {
  179. base.TransferSyn(goods, smooth);
  180. // Debug.Log(goods.goods_extended);
  181. CheckVideoState(goods.goods_extended);
  182. // goods.goods_extended
  183. }
  184. private void CheckVideoState(string goods_extended)
  185. {
  186. try
  187. {
  188. JsonData dataInfo = JsonMapper.ToObject(goods_extended);
  189. if (dataInfo != null)
  190. {
  191. if (dataInfo.Keys.Contains("vdPlay"))
  192. {
  193. if (dataInfo["vdPlay"].ToString() == "1")
  194. {
  195. isPlay = true;
  196. PlayVideo(false);
  197. }
  198. else
  199. {
  200. isPlay = false;
  201. PauseVideo(false);
  202. }
  203. }
  204. if (dataInfo.Keys.Contains("vdFrame"))
  205. {
  206. if (Mathf.Abs(MoviesPlayer.XRSlider.value - float.Parse(dataInfo["vdFrame"].ToString())) > 0.01f)
  207. {
  208. StartCoroutine(pointerUp(false));
  209. MoviesPlayer.isDown = true;
  210. MoviesPlayer.XRSlider.value = float.Parse(dataInfo["vdFrame"].ToString());
  211. MoviesPlayer.sliderChanged(MoviesPlayer.XRSlider.value);
  212. }
  213. }
  214. }
  215. }
  216. catch
  217. {
  218. }
  219. }
  220. void VideoState(string type, string info)
  221. {
  222. JsonData dataInfo = new JsonData();
  223. try
  224. {
  225. dataInfo = JsonMapper.ToObject(Container.data.goods_extended);
  226. }
  227. catch
  228. {
  229. }
  230. dataInfo[type] = info;
  231. Container.data.goods_extended = dataInfo.ToJson();
  232. }
  233. }
  234. }