Unity 待机视频图片轮播功能

原先写过一次这个功能,后来因为有点bug就删除了,现在补回来,虽然重新写了一次吧,但是还是感觉代码有点多=。=,如果大佬们有优化的方法希望能告诉我下Thanks♪(・ω・)ノ

 

先说明下,视频是通过AVProVideo插件加载的,所以你们可能需要个这个插件(*^▽^*),图片就是通过WWW加载赋值在RawImage上的,这个比较简单。总体功能呢,就是当你的程序无人操作多长时间后开始图片和视频的循环播放(PC版),代码如下:

这个一开始StandVideoCanvas和RawImage是隐藏的

就是名字旁边的那个勾是不勾上的

using RenderHeads.Media.AVProVideo;
using System.IO;
using UnityEngine;
using UnityEngine.UI;

public class Loading : MonoBehaviour 
	{

		public GameObject StandByVideoCanvas;//待机界面,加载vedio和rawimage的父物体
		public GameObject Video;//加载Vedio的界面
		public GameObject RawImage;//加载图片的界面
		public MediaPlayer MP;
		private DirectoryInfo dir;
		public float CountTimer = 0f;//计时时间,时间到了就开始播放
		private string path;
		private FileInfo[] videoFile;
		private int VideoIndex = 0;
		private FileInfo[] ImageFile;
		private int ImageIndex = 0;
		public float PictureTimer = 0;//图片播放时间
		private bool isPlayedVideo = false;
		private bool isStartPlay = false;
		private void Start()
		{
			MP.Events.AddListener(OnMediaPlayerEvent);
			path = "E:/TeaTable/图片视频轮播";
			dir = new DirectoryInfo(path);
			//加载文件并存储
			videoFile = dir.GetFiles("*.mp4", SearchOption.TopDirectoryOnly);
			ImageFile = dir.GetFiles("*.jpg", SearchOption.TopDirectoryOnly);
			if (ImageFile.Length != 0)
			{
				RawImage.GetComponent().texture = LoadPicture(0);
			}
			


		}

		/// 
		/// AVProVideo自带回调
		/// 
		/// 
		/// 
		/// 
		private void OnMediaPlayerEvent(MediaPlayer arg0, MediaPlayerEvent.EventType arg1, ErrorCode arg2)
		{
			switch (arg1)
			{
				case MediaPlayerEvent.EventType.FinishedPlaying:
					VideoIndex++;
					if (VideoIndex > videoFile.Length - 1)
					{
						VideoIndex = 0;
						if (ImageFile.Length != 0)
						{
							Video.SetActive(false);
							MP.gameObject.SetActive(false);
							RawImage.SetActive(true);
							isPlayedVideo = true;
							Debug.Log(123);
						}
					}
					MP.OpenVideoFromFile(MediaPlayer.FileLocation.AbsolutePathOrURL, path+"/"+videoFile[VideoIndex].Name);
					break;
				default:
					break;
			}
		}
		private Vector3 Pos1;//判断是否有用户操作的一个存储位置
		private void Update()
		{

			#region 视频图片轮播
			Vector3 Pos2 = Input.mousePosition;
			//判断鼠标是否在移动
			if (Pos1 != Pos2)
			{
				CountTimer = 0;
				if(StandByVideoCanvas.activeInHierarchy == true)
				{
					CloseStandByVideoCanvas();
				}
			}
			//待机计时
			if (!isStartPlay)
			{
				CountTimer += Time.deltaTime;
				if (CountTimer > 20f)
				{
					CountTimer = 0;
					isStartPlay = true;
					InitStandByVideoCanvas();
				}
				
			}
			//循环图片
			if (isPlayedVideo||(isStartPlay&& videoFile.Length == 0))
			{
				PictureTimer += Time.deltaTime;
				if (PictureTimer > 3f)
				{
					PictureTimer = 0;
					ImageIndex++;
					if (ImageIndex > ImageFile.Length - 1)
					{
						if(videoFile.Length != 0)
						{
							isPlayedVideo = false;
							InitStandByVideoCanvas();
						}
						else
						{
							ImageIndex = 0;
						}
						
					}
					RawImage.GetComponent().texture = LoadPicture(ImageIndex);

				}
			}
			Pos1 = Input.mousePosition;
			#endregion


		}
		
		
		public void InitStandByVideoCanvas()
		{
			VideoIndex = 0;
			ImageIndex = 0;
			StandByVideoCanvas.SetActive(true);
			if (videoFile.Length != 0)
			{
				MP.OpenVideoFromFile(MediaPlayer.FileLocation.AbsolutePathOrURL, path + "/" + videoFile[VideoIndex].Name);
				MP.gameObject.SetActive(true);
				Video.SetActive(true);
				RawImage.SetActive(false);
				isPlayedVideo = false;
			}
			else
			{
				MP.gameObject.SetActive(false);
				Video.SetActive(false);
				RawImage.SetActive(true);
				isPlayedVideo = true;
			}
			
		}
		/// 
		/// 加载图片
		/// 
		/// 
		/// 
		public Texture LoadPicture(int index)
		{
			WWW www = new WWW("file:///" + path + "/" + ImageFile[index].FullName);
			return www.texture;
		}
		/// 
		/// 关闭
		/// 
		public void CloseStandByVideoCanvas()
		{
			VideoIndex = 0;
			ImageIndex = 0;
			StandByVideoCanvas.SetActive(false);
			MP.gameObject.SetActive(true);
			MP.Stop();
			Video.SetActive(true);
			RawImage.SetActive(false);
			isPlayedVideo = false;
		    isStartPlay = false;
	}
	}

 

你可能感兴趣的:(Unity,功能)