XRVideoPlayer.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. 
  2. using DG.Tweening;
  3. using RenderHeads.Media.AVProVideo;
  4. using System;
  5. using System.Collections;
  6. using UnityEngine;
  7. using UnityEngine.EventSystems;
  8. using UnityEngine.UI;
  9. using UnityEngine.Video;
  10. using XRTool.Util;
  11. namespace XRTool.WorldUI
  12. {
  13. /// <summary>
  14. /// 基本的3D图形
  15. /// 与UI Image进行结合转化,自动换算
  16. /// </summary>
  17. public class XRVideoPlayer : MonoBehaviour
  18. {
  19. private XRSlider xRSlider;
  20. private XRImage3D bG;
  21. private XRImage3D xRImage3D;
  22. private XRImage3D pause;
  23. private RenderTexture videoTexture;
  24. public RenderTexture VideoTexture { get => videoTexture; set => videoTexture = value; }
  25. private RectTransform rectTransform;
  26. private Vector2 videoSize;
  27. public bool isDown = false;
  28. private int hour, mint;
  29. private TextMesh timeText;
  30. private VideoPlayer videoPlayer;
  31. private Transform root;
  32. private MeshFilter body;
  33. private Renderer bodyRender;
  34. public bool isAutoScale = true;
  35. public float thickness = 100;
  36. // public float scale = 0.5f;
  37. public VideoClip video;
  38. private XRButton xRButton;
  39. private BoxCollider box;
  40. private AVProVideoPlayer avProVideoPlayer;
  41. void Start()
  42. {
  43. //if (video)
  44. //{
  45. // SetSimple();
  46. //}
  47. StartCoroutine(videoUpdate());
  48. TimeText.text = "0:00";
  49. // XRImage3D.SetColor(Color.black);
  50. initVideo();
  51. }
  52. private IEnumerator InitVideoPlayer()
  53. {
  54. yield return AVProVideoPlayer;
  55. while (AVProVideoPlayer.GetUrl()!=null)
  56. {
  57. yield return new WaitForFixedUpdate();
  58. }
  59. Vector2 size = Vector2.one;
  60. size.x = AVProVideoPlayer.GetVideoWidth();
  61. size.y = AVProVideoPlayer.GetVideoHeight();
  62. // Box.size = new Vector3(size.x/RectTransform.localScale.x, size.y / RectTransform.localScale.y, 1);
  63. if (!VideoTexture)
  64. {
  65. VideoTexture = new RenderTexture((int)size.x, (int)size.y, 0);
  66. }
  67. else
  68. {
  69. VideoTexture.width = (int)size.x;
  70. VideoTexture.width = (int)size.y;
  71. }
  72. XRImage3D.SetColor(Color.white);
  73. // VideoPlayer.targetTexture = VideoTexture;
  74. //XRImage3D.SetSimple(AVProVideoPlayer.Control.GetTexture());
  75. }
  76. public void SetSimple(VideoClip tex)
  77. {
  78. //this.video = tex;
  79. //if (this.gameObject!=null)
  80. //{
  81. // UnityUtil.ChangeMateVideo(VideoPlayer.gameObject, tex);
  82. //}
  83. }
  84. public void SetColor(Color color) {
  85. if (BodyRender && BodyRender.enabled)
  86. {
  87. try
  88. {
  89. UnityUtil.ChangeMateColor(BodyRender, color);
  90. }
  91. catch (Exception ex)
  92. {
  93. UnityLog.Instance.LogError(BodyRender.material + " have no mainTexture");
  94. }
  95. }
  96. }
  97. public void SetSimple(Texture tex)
  98. {
  99. if (BodyRender && tex && BodyRender.enabled)
  100. {
  101. try
  102. {
  103. UnityUtil.ChangeMateTexture(BodyRender, tex);
  104. }
  105. catch (Exception ex)
  106. {
  107. UnityLog.Instance.LogError(BodyRender.material + " have no mainTexture");
  108. }
  109. }
  110. }
  111. public void SetSimple()
  112. {
  113. if (video)
  114. {
  115. SetSimple(video);
  116. }
  117. }
  118. /// <summary>
  119. /// 3D物体
  120. /// </summary>
  121. public MeshFilter Body
  122. {
  123. get
  124. {
  125. if (!body)
  126. {
  127. body = UnityUtil.GetBreadthChild<MeshFilter>(transform, "Body");
  128. }
  129. return body;
  130. }
  131. }
  132. public XRButton XRButton
  133. {
  134. get
  135. {
  136. if (!xRButton)
  137. {
  138. xRButton = UnityUtil.GetBreadthChild<XRButton>(transform, "XRButton");
  139. }
  140. return xRButton;
  141. }
  142. }
  143. public BoxCollider Box
  144. {
  145. get
  146. {
  147. if (!box)
  148. {
  149. box = this.GetComponent<BoxCollider>();
  150. }
  151. return box;
  152. }
  153. }
  154. public Transform Root
  155. {
  156. get
  157. {
  158. if (!root)
  159. {
  160. root = UnityUtil.GetBreadthChild<Transform>(transform, "Root");
  161. }
  162. return root;
  163. }
  164. set => root = value;
  165. }
  166. public RectTransform RectTransform
  167. {
  168. get
  169. {
  170. if (!rectTransform)
  171. {
  172. rectTransform = this.GetComponent<RectTransform>();
  173. }
  174. return rectTransform;
  175. }
  176. set => rectTransform = value;
  177. }
  178. public Vector2 VideoSize
  179. {
  180. get
  181. {
  182. if (videoSize == Vector2.zero)
  183. {
  184. videoSize = new Vector2(AVProVideoPlayer.GetVideoWidth(), AVProVideoPlayer.GetVideoHeight());
  185. }
  186. return videoSize;
  187. }
  188. }
  189. public XRImage3D XRImage3D
  190. {
  191. get
  192. {
  193. if (!xRImage3D)
  194. {
  195. xRImage3D = UnityUtil.GetBreadthChild<XRImage3D>(transform, "XRImage3D");
  196. }
  197. return xRImage3D;
  198. }
  199. }
  200. public XRImage3D BG
  201. {
  202. get
  203. {
  204. if (!bG)
  205. {
  206. bG = UnityUtil.GetBreadthChild<XRImage3D>(transform, "BG");
  207. }
  208. return bG;
  209. }
  210. }
  211. public XRImage3D Pause
  212. {
  213. get
  214. {
  215. if (!pause)
  216. {
  217. pause = UnityUtil.GetBreadthChild<XRImage3D>(transform, "Pause");
  218. }
  219. return pause;
  220. }
  221. }
  222. public VideoPlayer VideoPlayer
  223. {
  224. get
  225. {
  226. if (!videoPlayer)
  227. {
  228. videoPlayer = this.GetComponent<VideoPlayer>();
  229. }
  230. return videoPlayer;
  231. }
  232. }
  233. public AVProVideoPlayer AVProVideoPlayer
  234. {
  235. get
  236. {
  237. if (!avProVideoPlayer)
  238. {
  239. avProVideoPlayer = this.GetComponent<AVProVideoPlayer>();
  240. }
  241. return avProVideoPlayer;
  242. }
  243. }
  244. public TextMesh TimeText
  245. {
  246. get
  247. {
  248. if (!timeText)
  249. {
  250. timeText = UnityUtil.GetBreadthChild<TextMesh>(transform, "TimeText");
  251. }
  252. return timeText;
  253. }
  254. }
  255. public Renderer BodyRender
  256. {
  257. get
  258. {
  259. if (!bodyRender)
  260. {
  261. bodyRender = UnityUtil.GetBreadthChild<Renderer>(transform, "Body");
  262. }
  263. return bodyRender;
  264. }
  265. }
  266. /// <summary>
  267. /// 如果不是全等缩放,则设置厚度
  268. /// </summary>
  269. public void SetThickness()
  270. {
  271. SetThickness(thickness);
  272. }
  273. public void SetRenderMesh(Mesh mesh)
  274. {
  275. if (Body && mesh && Body.sharedMesh != mesh)
  276. {
  277. Body.sharedMesh = mesh;
  278. Body.transform.localPosition = Vector3.zero;
  279. Body.transform.localRotation = Quaternion.identity;
  280. Vector3 center = mesh.bounds.center;
  281. Vector3 pos = -center;
  282. pos.z = -(mesh.bounds.center.z + mesh.bounds.size.z) / 2;
  283. Body.transform.localPosition = pos;
  284. }
  285. }
  286. public void SetRenderMate(Material mate)
  287. {
  288. if (BodyRender && mate)
  289. {
  290. BodyRender.material = mate;
  291. }
  292. }
  293. public void AutoSetSize()
  294. {
  295. if (Root)
  296. {
  297. Vector3 size = RectTransform.rect.size;
  298. if (!isAutoScale)
  299. {
  300. size.z = thickness;
  301. }
  302. else
  303. {
  304. size.z = RectTransform.rect.size.x > RectTransform.rect.size.y ?
  305. RectTransform.rect.size.y : RectTransform.rect.size.x;
  306. }
  307. // Root.localScale = size * scale;
  308. }
  309. }
  310. public void SetThickness(float thickness)
  311. {
  312. this.thickness = thickness;
  313. if (!isAutoScale && Root)
  314. {
  315. Vector3 tmp = Root.localScale;
  316. // tmp.z = thickness * scale;
  317. // Root.localScale = tmp;
  318. }
  319. }
  320. /// <summary>
  321. /// 厚度变化动效
  322. /// </summary>
  323. /// <param name="pressDis">比例,相对于物体厚度的本身比例</param>
  324. /// <param name="pressTime">变化事件</param>
  325. public void DoThickness(float pressDis, float pressTime)
  326. {
  327. if (Root)
  328. {
  329. Root.DOKill();
  330. float target = thickness * pressDis;
  331. if (isAutoScale)
  332. {
  333. // target = (RectTransform.rect.size.x > RectTransform.rect.size.y ?
  334. // RectTransform.rect.size.y : RectTransform.rect.size.x) * scale;
  335. }
  336. // Root.DOScaleZ(target, pressTime);
  337. }
  338. }
  339. void OnRectTransformDimensionsChange()
  340. {
  341. AutoSetSize();
  342. }
  343. public XRSlider XRSlider
  344. {
  345. get
  346. {
  347. if (!xRSlider)
  348. {
  349. xRSlider = UnityUtil.GetBreadthChild<XRSlider>(transform, "XRSlider");
  350. }
  351. return xRSlider;
  352. }
  353. }
  354. IEnumerator videoUpdate()
  355. {
  356. while (true)
  357. {
  358. if (XRSlider && AVProVideoPlayer && isDown == false)
  359. {
  360. if (AVProVideoPlayer.IsPlaying())
  361. {
  362. //XRSlider.value = float.Parse(VideoPlayer.frame.ToString()) / float.Parse(VideoPlayer.frameCount.ToString());
  363. // Debug.Log("videoUpdate " + AVProVideoPlayer.GetNowTimer() + " "+ AVProVideoPlayer.GetMaxTimer());
  364. float nowTimer = AVProVideoPlayer.GetNowTimer();
  365. float maxTimer = AVProVideoPlayer.GetMaxTimer();
  366. XRSlider.value = nowTimer / maxTimer;
  367. TimeText.text = (((int)nowTimer)/60).ToString()+":"+(((int)nowTimer) %60).ToString() + "/" + (((int)maxTimer) / 60).ToString() + ":" + (((int)maxTimer) % 60).ToString();
  368. XRImage3D.transform.localScale = new Vector3(XRImage3D.transform.localScale.x
  369. , 1/(AVProVideoPlayer.GetVideoWidth()/ AVProVideoPlayer.GetVideoHeight()), XRImage3D.transform.localScale.z);
  370. // Debug.Log(VideoSize.ToString());
  371. XRSlider.gameObject.SetActive(true);
  372. }
  373. else
  374. {
  375. XRSlider.gameObject.SetActive(false);
  376. }
  377. }
  378. if (!AVProVideoPlayer.IsPlaying()&&!isShowControl)
  379. {
  380. showControl();
  381. }
  382. yield return new WaitForSeconds(0.035f);
  383. }
  384. }
  385. public string GetNowTime()
  386. {
  387. //double f = VideoPlayer.time;
  388. //hour = (int)f / 60;
  389. //mint = (int)f % 60;
  390. //return string.Format(" {0:D2}:{1:D2}", hour.ToString(), mint.ToString());
  391. return AVProVideoPlayer.GetNowTimer().ToString();
  392. }
  393. public string GetMaxTime()
  394. {
  395. //float f = VideoPlayer.frameCount / VideoPlayer.frameRate;
  396. //hour = (int)f / 60;
  397. //mint = (int)f % 60;
  398. //return string.Format(" {0:D2}:{1:D2}", hour.ToString(), mint.ToString());
  399. return AVProVideoPlayer.GetMaxTimer().ToString();
  400. }
  401. public void sliderChanged(float per)
  402. {
  403. if (XRSlider && AVProVideoPlayer)
  404. {
  405. //float targetFrame = VideoPlayer.frameCount * per;
  406. //VideoPlayer.frame = (long)targetFrame;
  407. AVProVideoPlayer.SetSeek(per);
  408. }
  409. }
  410. public void initVideo()
  411. {
  412. StartCoroutine(InitVideoPlayer());
  413. }
  414. bool isShowControl = true;
  415. public void showControl()
  416. {
  417. if(!isShowControl)
  418. {
  419. GameEffect.setAlpha(Pause.gameObject, 1, 0.5f);
  420. GameEffect.setAlpha(XRSlider.gameObject, 1, 0.5f);
  421. GameEffect.setAlpha(BG.gameObject, 1, 0.5f);
  422. isShowControl = true;
  423. }
  424. }
  425. public void hideControl()
  426. {
  427. if (AVProVideoPlayer.IsPlaying()&& isShowControl)
  428. {
  429. GameEffect.setAlpha(Pause.gameObject, 0, 0.5f, (GameObject obj) => {
  430. obj.SetActive(false);
  431. });
  432. GameEffect.setAlpha(XRSlider.gameObject, 0, 0.5f, (GameObject obj) => {
  433. obj.SetActive(false);
  434. });
  435. GameEffect.setAlpha(BG.gameObject, 0, 0.5f,(GameObject obj)=> {
  436. obj.SetActive(false);
  437. });
  438. isShowControl = false;
  439. }
  440. }
  441. }
  442. }