123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214 |
- 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<EState, int> OnClickVideoItem;
- public int ID;
- //public List<Vector3> VtdeoPos = new List<Vector3>();
- 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<BoxCollider>();
- if (ID == 3)
- {
- PLayerVideo();
- //Collider.enabled = false;
- }
- }
- /// <summary>
- /// 点击UI后判断移动方向
- /// </summary>
- 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);
- }
- /// <summary>
- /// 播放旋转动画
- /// </summary>
- /// <param name="vector"></param>
- /// <param name="state"></param>
- 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();
- }
- /// <summary>
- /// 开始旋转动画
- /// </summary>
- /// <param name="state"></param>
- /// <returns></returns>
- 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);
- }
- /// <summary>
- /// 旋转动画完成后1秒显示视频播放界面
- /// </summary>
- /// <returns></returns>
- 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<VideoPlayer>();
- // video.url = path;
- video.playOnAwake = false;
- video.waitForFirstFrame = true;
- video.sendFrameReadyEvents = true;
- video.frameReady += GetNumTextrue;
- video.Play();
- }
- int framesValue = 0;//获得视频第几帧的图片
- /// <summary>
- /// 获取到framesValue帧的Textrue
- /// </summary>
- 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));
- }
- /// <summary>
- /// 生成缩略图
- /// </summary>
- 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
- }
|