XRVideoPlayer.cs 13 KB

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