using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using DG.Tweening; using UnityEngine.Video; using UnityEngine.UI; public enum EState { none, right, left, } public class TestVideoItem : MonoBehaviour { public Action OnClickVideoItem; public int ID; //public List VtdeoPos = new List(); private EState State; public VideoPlayer VideoPlayer; public VideoClip clip; public RawImage image; private Transform m_Image; private BoxCollider Collider; private void Start() { m_Image = transform.Find("Canvas/RawImage"); Collider = transform.Find("Canvas/Video").GetComponent(); if (ID == 3) { PLayerVideo(); //Collider.enabled = false; } } /// /// 点击UI后判断移动方向 /// public void OnClickVideo() { State = ID < 3 ? EState.right : EState.left; State = ID == 3 ? EState.none : State; if (OnClickVideoItem != null) { OnClickVideoItem(State, ID); } Debug.Log(State); } /// /// 播放旋转动画 /// /// /// public void Play(Vector3 vector, EState state) { //播放旋转动画时不可点击 Collider.enabled = false; transform.DOLocalMove(vector, 3f); //根据ID显示不同的缩放,画面效果 if (ID == 3) { transform.DOScale(new Vector3(0.16f, 0.1f, 0.1f), 3f); } else if (ID == 2 || ID == 4) { transform.DOScale(new Vector3(0.1f, 0.1f, 0.1f), 3f); } else { transform.DOScale(new Vector3(0.1f, 0.09f, 0.1f), 3f); } StartCoroutine(DoRotate(state)); StopVideo(); } /// /// 开始旋转动画 /// /// /// IEnumerator DoRotate(EState state) { switch (state) { case EState.none: break; case EState.right: transform.DOLocalRotate(new Vector3(0, -60, 0), 1f); break; case EState.left: transform.DOLocalRotate(new Vector3(0, 60, 0), 1f); break; default: break; } //1.5秒后转回初始角度 yield return new WaitForSeconds(1.5f); transform.DOLocalRotate(new Vector3(0, 0, 0), 1f); //1.5秒后播放视频 yield return new WaitForSeconds(1.5f); if (ID == 3) { PLayerVideo(); } //1.2秒后可重新点击 yield return new WaitForSeconds(1.2f); //if (ID != 3) //{ Collider.enabled = true; //} } private void PLayerVideo() { VideoPlayer.gameObject.SetActive(true); VideoPlayer.clip = clip; VideoPlayer.Play(); StartCoroutine(WaitTime()); } private void StopVideo() { VideoPlayer.Stop(); VideoPlayer.clip = null; VideoPlayer.transform.localPosition = new Vector3(-0.008f, -0.058f, 0.02f); VideoPlayer.gameObject.SetActive(false); m_Image.gameObject.SetActive(true); } /// /// 旋转动画完成后1秒显示视频播放界面 /// /// IEnumerator WaitTime() { yield return new WaitForSeconds(1f); m_Image.gameObject.SetActive(false); VideoPlayer.transform.localPosition = new Vector3(-0.008f, -0.058f, 0f); } #region 获取视频某一帧图片 VideoPlayer video; Texture2D videoTextrue; RenderTexture renderTexture; Sprite sprite; void InitVideoToImage()//Start { videoTextrue = new Texture2D(2, 2); video = GetComponent(); // video.url = path; video.playOnAwake = false; video.waitForFirstFrame = true; video.sendFrameReadyEvents = true; video.frameReady += GetNumTextrue; video.Play(); } int framesValue = 0;//获得视频第几帧的图片 /// /// 获取到framesValue帧的Textrue /// void GetNumTextrue(VideoPlayer source, long frameIdx) { framesValue++; if (framesValue == 1) { renderTexture = source.texture as RenderTexture; if (videoTextrue.width != renderTexture.width || videoTextrue.height != renderTexture.height) { videoTextrue.Resize(renderTexture.width, renderTexture.height); } RenderTexture.active = renderTexture; videoTextrue.ReadPixels(new Rect(0, 0, renderTexture.width, renderTexture.height), 0, 0); videoTextrue.Apply(); RenderTexture.active = null; video.frameReady -= GetNumTextrue; video.sendFrameReadyEvents = false; } StartCoroutine(ScaleTexture(videoTextrue, 1920, 1080)); } /// /// 生成缩略图 /// IEnumerator ScaleTexture(Texture2D source, int targetWidth, int targetHeight) { yield return new WaitForSeconds(2); Texture2D result = new Texture2D(targetWidth, targetHeight, TextureFormat.ARGB32, false); for (int i = 0; i < result.height; ++i) { for (int j = 0; j < result.width; ++j) { Color newColor = source.GetPixelBilinear((float)j / (float)result.width, (float)i / (float)result.height); result.SetPixel(j, i, newColor); } } result.Apply(); sprite = Sprite.Create(result, new Rect(0, 0, targetWidth, targetHeight), new Vector2(0.5f, 0.5f)); image.texture = sprite.texture; } #endregion }