unity仿抖音滑动
作者:互联网
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.Video;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class VideoControl : MonoBehaviour {
public GameObject tip;
public Image LoadingBar;
public RectTransform VideoPanel;
/// <summary>
/// 视频预制体
/// </summary>
public GameObject VideoPrefab;
bool OpenInput;
/// <summary>
/// 当前的视频索引
/// </summary>
int CurrentIndex;
public float LerpSpeed = 20;
/// <summary>
/// 是否滑动
/// </summary>
bool isDrag = false;
bool isInput = false;
/// <summary>
/// 读取的文件夹名称
/// </summary>
public string FolderName = "videos";
/// <summary>
/// 当前播放列表是否为空
/// </summary>
bool isExit;
/// <summary>
/// 文件长度
/// </summary>
int FileLength = 0;
/// <summary>
/// 是否设置帧率
/// </summary>
public bool SetFrame;
/// <summary>
/// 设置的帧率值
/// </summary>
public int FrameRate = 60;
/// <summary>
/// 渐变的临界值
/// </summary>
float slider_parent = 1300;
/// <summary>
/// 渐变的范围
/// </summary>
float slider_child = 200;
/// <summary>
/// 渐变速度
/// </summary>
float Color_lerp = 1;
float LerpValue;
/// <summary>
/// 滑动判断临界值
/// </summary>
public float LerpTime;
float OldTime;
float NewTime;
float OldPoint;
float NewPoint;
int TempIndex = 0;
/// <summary>
/// 当前滑动方向
/// </summary>
public Movement Movement;
/// <summary>
/// 当前视频播放进度
/// </summary>
[Range(0, 1)]
public float frame;
/// <summary>
/// 支持的视频文件格式拓展名
/// </summary>
public List<string> FilesExtension = new List<string>();
/// <summary>
/// 视频文件路径
/// </summary>
public List<string> path = new List<string>();
/// <summary>
/// 视频预制体对象池
/// </summary>
public List<GameObject> VideoData = new List<GameObject>();
//public RenderTexture renderTexture;
private void Awake()
{
if (SetFrame)
{
Application.targetFrameRate = FrameRate;
}
Screen.SetResolution(1080, 1920, true);
Screen.fullScreen = true;
GetFileInfo();
SetVideos();
TempIndex = FileLength;
VideoPanel.anchoredPosition = new Vector2(VideoPanel.anchoredPosition.x, -FileLength * 1920);
}
void FixedUpdate()
{
if (isExit)
{
LoadingBar.fillAmount = frame;
CurrentIndex = (int)Mathf.Abs(VideoPanel.anchoredPosition.y + 900) / 1920;
if (OpenInput)
{
PanelLerp();
if (!isInput)
{
UpdataVideo();
}
}
if (Input.GetMouseButton(0))
{
if (Mathf.Abs(VideoPanel.anchoredPosition.y % 1920) > 900 && Mathf.Abs(VideoPanel.anchoredPosition.y % 1920) < slider_parent + 400)
{
VideoData[FileLength - 1 - CurrentIndex].GetComponent<RawImage>().color =
new Color((Mathf.Abs(VideoPanel.anchoredPosition.y % 1920) - slider_parent) / slider_child, (Mathf.Abs(VideoPanel.anchoredPosition.y % 1920) - slider_parent) / slider_child,
(Mathf.Abs(VideoPanel.anchoredPosition.y % 1920) - slider_parent) / slider_child, (Mathf.Abs(VideoPanel.anchoredPosition.y % 1920) - slider_parent) / slider_child);
}
}
}
if (Input.GetKeyDown(KeyCode.Escape))
{
Application.Quit();
}
}
IEnumerator toOpenInput()
{
yield return new WaitForSeconds(2f);
OpenInput = true;
tip.SetActive(false);
}
/// <summary>
/// 获取StreamingAssets目录下所有的视频文件
/// </summary>
public void GetFileInfo()
{
string path = Application.persistentDataPath + "/" + FolderName + "/";
isExit = Directory.Exists(path);
if (Directory.Exists(path))
{
DirectoryInfo direction = new DirectoryInfo(path);
FileInfo[] files = direction.GetFiles("*", SearchOption.AllDirectories);
print(files.Length);
for (int i = 0; i < files.Length; i++)
{
if (files[i].Name.EndsWith(".meta"))
{
continue;
}
print(files[i].Extension);
for (int j = 0; j < FilesExtension.Count; j++)
{
if (files[i].Extension.ToLower().Contains(FilesExtension[j]))
{
string name = Path.GetFileNameWithoutExtension(files[i].ToString());
print(name);
this.path.Add(path + name + files[i].Extension);
FileLength++;
}
}
}
if (FileLength > 0)
{
StartCoroutine(toOpenInput());
}
else
{
tip.GetComponent<Text>().text = "videos文件夹下mp4视频为空!";
}
}
else
{
tip.GetComponent<Text>().text = "videos文件夹不存在!";
}
}
/// <summary>
/// 视频初始化
/// </summary>
void SetVideos()
{
path = ListControl.RandomSortList<string>(path);
for (int i = 0; i < FileLength; i++)
{
GameObject TempObj = Instantiate(VideoPrefab);
VideoData.Add(TempObj);
AddVideo(TempObj, i);
//if (i < 3)
//{
// AddVideo(TempObj,i);
//}
//RenderTexture renderTexture = new RenderTexture(1080/2, 1920/2,0);
TempObj.transform.parent = VideoPanel;
//TempObj.GetComponent<RawImage>().texture = renderTexture;
//TempObj.GetComponent<VideoPlayer>().targetTexture = renderTexture;
//TempObj.GetComponent<VideoPlayer>().url = path[i];
TempObj.transform.localScale = Vector3.one;
}
}
/// <summary>
/// 设置视频
/// </summary>
void UpdataVideo()
{
for (int i = 0; i < FileLength; i++)
{
if (CurrentIndex == FileLength - 1 - i)
{
if (!VideoData[i].GetComponent<VideoPlayer>().isPlaying)
{
//VideoData[i].GetComponent<VideoPlayer>().SetTargetAudioSource(0, GetComponent<AudioSource>());
//VideoData[i].GetComponent<VideoPlayer>().Stop();
VideoData[i].GetComponent<VideoPlayer>().Stop();
VideoData[i].GetComponent<VideoPlayer>().Play();
}
frame = VideoData[i].GetComponent<VideoPlayer>().frame / float.Parse(VideoData[i].GetComponent<VideoPlayer>().frameCount.ToString());
if (long.Parse(VideoData[i].GetComponent<VideoPlayer>().frameCount.ToString()) - VideoData[i].GetComponent<VideoPlayer>().frame < 3)
{
if (CurrentIndex == 0)
{
SceneManager.LoadScene(0);
}
else
{
VideoData[FileLength - 1 - CurrentIndex].GetComponent<RawImage>().color = Color.clear;
isDrag = true;
Movement = Movement.up;
TempIndex = CurrentIndex - 1;
isInput = false;
}
}
else
{
VideoData[i].GetComponent<RawImage>().color = Color.Lerp(VideoData[i].GetComponent<RawImage>().color, Color.white, Time.deltaTime * Color_lerp);
}
}
else
{
//if (!VideoData[i].GetComponent<VideoPlayer>().isPlaying)
//{
// VideoData[i].GetComponent<VideoPlayer>().targetTexture.Release();
//}
//VideoData[i].GetComponent<VideoPlayer>().targetTexture.Release();
VideoData[i].GetComponent<VideoPlayer>().Stop();
VideoData[i].GetComponent<VideoPlayer>().targetTexture.Release();
VideoData[i].GetComponent<RawImage>().color = Color.black;
}
#region MyRegion
//if (CurrentIndex == FileLength - 1 - i)
//{
// if (VideoData[i].GetComponent<VideoPlayer>() != null)
// {
// if (!VideoData[i].GetComponent<VideoPlayer>().isPlaying)
// {
// VideoData[i].GetComponent<VideoPlayer>().Play();
// VideoData[i].GetComponent<RawImage>().color = Color.white;
// }
// VideoData[i].GetComponent<RawImage>().color = Color.white;
// }
// else
// {
// AddVideo(VideoData[i], i);
// }
//}
//else if (CurrentIndex - 1 == FileLength-1 - i)
//{
// if (VideoData[i].GetComponent<VideoPlayer>() == null)
// {
// AddVideo(VideoData[i], i);
// }
// else
// {
// VideoData[i].GetComponent<VideoPlayer>().Stop();
// VideoData[i].GetComponent<VideoPlayer>().frame = 2;
// VideoData[i].GetComponent<RawImage>().color = Color.black;
// }
//}
//else if (CurrentIndex + 1 == FileLength-1 - i)
//{
// if (VideoData[i].GetComponent<VideoPlayer>() == null)
// {
// AddVideo(VideoData[i], i);
// }
// else
// {
// VideoData[i].GetComponent<VideoPlayer>().Stop();
// VideoData[i].GetComponent<VideoPlayer>().frame = 2;
// VideoData[i].GetComponent<RawImage>().color = Color.black;
// }
//}
//else
//{
// //if (VideoData[i].GetComponent<VideoPlayer>() != null)
// //{
// // Destroy(VideoData[i].GetComponent<VideoPlayer>());
// // Destroy(VideoData[i].GetComponent<AudioSource>());
// //}
// //VideoData[i].GetComponent<RawImage>().color = Color.black;
//}
#endregion
}
}
/// <summary>
/// 添加视频
/// </summary>
void AddVideo(GameObject TempObj, int index)
{
//TempObj.AddComponent<AudioSource>();
TempObj.AddComponent<VideoPlayer>();
//TempObj.GetComponent<AudioSource>().playOnAwake = false;
TempObj.GetComponent<VideoPlayer>().playOnAwake = false;
TempObj.GetComponent<VideoPlayer>().waitForFirstFrame = false;
TempObj.GetComponent<VideoPlayer>().isLooping = false;
TempObj.GetComponent<VideoPlayer>().url = path[index];
TempObj.GetComponent<VideoPlayer>().audioOutputMode = VideoAudioOutputMode.AudioSource;
TempObj.GetComponent<VideoPlayer>().controlledAudioTrackCount = 1;
TempObj.GetComponent<VideoPlayer>().SetTargetAudioSource(0, GetComponent<AudioSource>());
RenderTexture renderTexture = new RenderTexture(1080, 1920, 0);
TempObj.GetComponent<VideoPlayer>().targetTexture = renderTexture;
TempObj.GetComponent<RawImage>().texture = renderTexture;
//if (!TempObj.GetComponent<VideoPlayer>().isPlaying)
//{
// TempObj.GetComponent<VideoPlayer>().Play();
//}
}
#region Screen control
/// <summary>
/// 滑动动画
/// </summary>
void PanelLerp()
{
if (!isInput)
{
if (isDrag)
{
if (CurrentIndex == 0)
{
VideoPanel.anchoredPosition = new Vector2(-540, Mathf.Lerp(VideoPanel.anchoredPosition.y, -1920, Time.deltaTime * LerpSpeed));
}
else
{
if (Movement == Movement.up)
{
VideoPanel.anchoredPosition = Vector2.Lerp(VideoPanel.anchoredPosition, new Vector2(-540, -(TempIndex + 1) * 1920), Time.deltaTime * LerpSpeed);
//VideoPanel.anchoredPosition = new Vector2(-540, Mathf.Lerp(VideoPanel.anchoredPosition.y, -(TempIndex) * 1920, Time.deltaTime * LerpSpeed));
}
if (Movement == Movement.down)
{
VideoPanel.anchoredPosition = new Vector2(-540, Mathf.Lerp(VideoPanel.anchoredPosition.y, -(TempIndex + 1) * 1920, Time.deltaTime * LerpSpeed));
}
}
}
else
{
if (CurrentIndex == 0)
{
VideoPanel.anchoredPosition = new Vector2(-540, Mathf.Lerp(VideoPanel.anchoredPosition.y, -1920, Time.deltaTime * LerpSpeed));
}
else
{
if (Movement == Movement.idle)
{
VideoPanel.anchoredPosition = new Vector2(-540, Mathf.Lerp(VideoPanel.anchoredPosition.y, -(TempIndex + 1) * 1920, Time.deltaTime * LerpSpeed));
}
}
}
}
if (VideoPanel.anchoredPosition.y < -FileLength * 1920)
{
VideoPanel.anchoredPosition = new Vector2(VideoPanel.anchoredPosition.x, -FileLength * 1920);
}
}
private void OnGUI()
{
if (EventType.MouseDown == Event.current.type)
{
OldTime = Time.time;
OldPoint = VideoPanel.anchoredPosition.y;
isInput = true;
}
if (EventType.MouseUp == Event.current.type)
{
print("Mouse up");
NewTime = Time.time;
NewPoint = VideoPanel.anchoredPosition.y;
LerpValue = NewTime - OldTime;
isDrag = LerpValue < LerpTime ? true : false;
Movement = isDrag ? (NewPoint < OldPoint ? Movement.down : Movement.up) : Movement.idle;
TempIndex = CurrentIndex;
isInput = false;
}
}
#endregion
}
/// <summary>
/// List随机排序
/// </summary>
public static class ListControl
{
public static List<T> RandomSortList<T>(List<T> ListT)
{
System.Random random = new System.Random();
List<T> newList = new List<T>();
foreach (T item in ListT)
{
newList.Insert(random.Next(newList.Count + 1), item);
}
return newList;
}
}
/// <summary>
/// 滑动方向
/// </summary>
public enum Movement
{
idle = 0,
up = 1,
down = 2
}
标签:VideoData,TempObj,anchoredPosition,仿抖音,unity,VideoPanel,GetComponent,滑动,public 来源: https://blog.csdn.net/qq_33174548/article/details/90731336