TestVideoItem.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using DG.Tweening;
  6. using UnityEngine.Video;
  7. using UnityEngine.UI;
  8. public enum EState
  9. {
  10. none,
  11. right,
  12. left,
  13. }
  14. public class TestVideoItem : MonoBehaviour
  15. {
  16. public Action<EState, int> OnClickVideoItem;
  17. public int ID;
  18. //public List<Vector3> VtdeoPos = new List<Vector3>();
  19. private EState State;
  20. public VideoPlayer VideoPlayer;
  21. public VideoClip clip;
  22. public RawImage image;
  23. private Transform m_Image;
  24. private BoxCollider Collider;
  25. private void Start()
  26. {
  27. m_Image = transform.Find("Canvas/RawImage");
  28. Collider = transform.Find("Canvas/Video").GetComponent<BoxCollider>();
  29. if (ID == 3)
  30. {
  31. PLayerVideo();
  32. //Collider.enabled = false;
  33. }
  34. }
  35. /// <summary>
  36. /// 点击UI后判断移动方向
  37. /// </summary>
  38. public void OnClickVideo()
  39. {
  40. State = ID < 3 ? EState.right : EState.left;
  41. State = ID == 3 ? EState.none : State;
  42. if (OnClickVideoItem != null)
  43. {
  44. OnClickVideoItem(State, ID);
  45. }
  46. Debug.Log(State);
  47. }
  48. /// <summary>
  49. /// 播放旋转动画
  50. /// </summary>
  51. /// <param name="vector"></param>
  52. /// <param name="state"></param>
  53. public void Play(Vector3 vector, EState state)
  54. {
  55. //播放旋转动画时不可点击
  56. Collider.enabled = false;
  57. transform.DOLocalMove(vector, 3f);
  58. //根据ID显示不同的缩放,画面效果
  59. if (ID == 3)
  60. {
  61. transform.DOScale(new Vector3(0.16f, 0.1f, 0.1f), 3f);
  62. }
  63. else if (ID == 2 || ID == 4)
  64. {
  65. transform.DOScale(new Vector3(0.1f, 0.1f, 0.1f), 3f);
  66. }
  67. else
  68. {
  69. transform.DOScale(new Vector3(0.1f, 0.09f, 0.1f), 3f);
  70. }
  71. StartCoroutine(DoRotate(state));
  72. StopVideo();
  73. }
  74. /// <summary>
  75. /// 开始旋转动画
  76. /// </summary>
  77. /// <param name="state"></param>
  78. /// <returns></returns>
  79. IEnumerator DoRotate(EState state)
  80. {
  81. switch (state)
  82. {
  83. case EState.none:
  84. break;
  85. case EState.right:
  86. transform.DOLocalRotate(new Vector3(0, -60, 0), 1f);
  87. break;
  88. case EState.left:
  89. transform.DOLocalRotate(new Vector3(0, 60, 0), 1f);
  90. break;
  91. default:
  92. break;
  93. }
  94. //1.5秒后转回初始角度
  95. yield return new WaitForSeconds(1.5f);
  96. transform.DOLocalRotate(new Vector3(0, 0, 0), 1f);
  97. //1.5秒后播放视频
  98. yield return new WaitForSeconds(1.5f);
  99. if (ID == 3)
  100. {
  101. PLayerVideo();
  102. }
  103. //1.2秒后可重新点击
  104. yield return new WaitForSeconds(1.2f);
  105. //if (ID != 3)
  106. //{
  107. Collider.enabled = true;
  108. //}
  109. }
  110. private void PLayerVideo()
  111. {
  112. VideoPlayer.gameObject.SetActive(true);
  113. VideoPlayer.clip = clip;
  114. VideoPlayer.Play();
  115. StartCoroutine(WaitTime());
  116. }
  117. private void StopVideo()
  118. {
  119. VideoPlayer.Stop();
  120. VideoPlayer.clip = null;
  121. VideoPlayer.transform.localPosition = new Vector3(-0.008f, -0.058f, 0.02f);
  122. VideoPlayer.gameObject.SetActive(false);
  123. m_Image.gameObject.SetActive(true);
  124. }
  125. /// <summary>
  126. /// 旋转动画完成后1秒显示视频播放界面
  127. /// </summary>
  128. /// <returns></returns>
  129. IEnumerator WaitTime()
  130. {
  131. yield return new WaitForSeconds(1f);
  132. m_Image.gameObject.SetActive(false);
  133. VideoPlayer.transform.localPosition = new Vector3(-0.008f, -0.058f, 0f);
  134. }
  135. #region 获取视频某一帧图片
  136. VideoPlayer video;
  137. Texture2D videoTextrue;
  138. RenderTexture renderTexture;
  139. Sprite sprite;
  140. void InitVideoToImage()//Start
  141. {
  142. videoTextrue = new Texture2D(2, 2);
  143. video = GetComponent<VideoPlayer>();
  144. // video.url = path;
  145. video.playOnAwake = false;
  146. video.waitForFirstFrame = true;
  147. video.sendFrameReadyEvents = true;
  148. video.frameReady += GetNumTextrue;
  149. video.Play();
  150. }
  151. int framesValue = 0;//获得视频第几帧的图片
  152. /// <summary>
  153. /// 获取到framesValue帧的Textrue
  154. /// </summary>
  155. void GetNumTextrue(VideoPlayer source, long frameIdx)
  156. {
  157. framesValue++;
  158. if (framesValue == 1)
  159. {
  160. renderTexture = source.texture as RenderTexture;
  161. if (videoTextrue.width != renderTexture.width || videoTextrue.height != renderTexture.height)
  162. {
  163. videoTextrue.Resize(renderTexture.width, renderTexture.height);
  164. }
  165. RenderTexture.active = renderTexture;
  166. videoTextrue.ReadPixels(new Rect(0, 0, renderTexture.width, renderTexture.height), 0, 0);
  167. videoTextrue.Apply();
  168. RenderTexture.active = null;
  169. video.frameReady -= GetNumTextrue;
  170. video.sendFrameReadyEvents = false;
  171. }
  172. StartCoroutine(ScaleTexture(videoTextrue, 1920, 1080));
  173. }
  174. /// <summary>
  175. /// 生成缩略图
  176. /// </summary>
  177. IEnumerator ScaleTexture(Texture2D source, int targetWidth, int targetHeight)
  178. {
  179. yield return new WaitForSeconds(2);
  180. Texture2D result = new Texture2D(targetWidth, targetHeight, TextureFormat.ARGB32, false);
  181. for (int i = 0; i < result.height; ++i)
  182. {
  183. for (int j = 0; j < result.width; ++j)
  184. {
  185. Color newColor = source.GetPixelBilinear((float)j / (float)result.width, (float)i / (float)result.height);
  186. result.SetPixel(j, i, newColor);
  187. }
  188. }
  189. result.Apply();
  190. sprite = Sprite.Create(result, new Rect(0, 0, targetWidth, targetHeight), new Vector2(0.5f, 0.5f));
  191. image.texture = sprite.texture;
  192. }
  193. #endregion
  194. }