前言
一、导入AVpro插件
AVpro插件是一款很强大的视频播放插件,配合Unity使用有意想不到的效果,他的各项功能网上都有,我就不进行展开讨论了。
该插件的获取方式为AssetStore中购买下载,该插件有一个免费版本可供使用,付费版本的功能强大一点。
有需要的也可以私信我,我分享给你,你只能用于学习,不可用于商用。
将下载好的AVpro导入Unity。
二、插件对应的UI界面
图中显示视频的GUI主要是导入AVPro之后创建的,右键UI/AVproVideouGUI
在该GUI的Inspector窗口有一个Display uGui的组件,该组件中有一个MediaPlayer的选项,该选项是一个播放器,右键添加该播放器并且拉入到该位置。
文章来源:https://www.toymoban.com/news/detail-402701.html
三、创建ChangeVideo脚本
/****************************************************
文件:ChangeVideo.cs
作者:Mark
日期:#CreateTime#
功能:替换视频
*****************************************************/
using RenderHeads.Media.AVProVideo;
using System.Collections;
using System.IO;
using System.Runtime.InteropServices;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;
public class ChangeVideo : MonoBehaviour
{
public Button changeVideoBtn; //替换视频的按钮
public MediaPlayer mediaPlayer;//AVpro的播放器
public DisplayUGUI displayUGUI;//AVpro的UGUI
private string savePath = Application.streamingAssetsPath + "/1.mp4";//视频加载后的保存位置
private void Start()
{
changeVideoBtn.onClick.AddListener(onRead);//监听按钮是否点击,如果点击就执行打开窗口
}
private void onRead()
{
OpenFileName ofn = new OpenFileName();
ofn.structSize = Marshal.SizeOf(ofn);
ofn.filter = "视频文件(*.mp4*.mov)\0*.mp4;*.mov";
ofn.file = new string(new char[256]);
ofn.maxFile = ofn.file.Length;
ofn.fileTitle = new string(new char[64]);
ofn.maxFileTitle = ofn.fileTitle.Length;
string path = Application.streamingAssetsPath;
path = path.Replace('/', '\\');
//默认路径
ofn.initialDir = path;
ofn.title = "选择需要替换的视频";
ofn.defExt = "mp4";//显示文件的类型
//注意 一下项目不一定要全选 但是0x00000008项不要缺少
ofn.flags = 0x00080000 | 0x00001000 | 0x00000800 | 0x00000200 | 0x00000008;//OFN_EXPLORER|OFN_FILEMUSTEXIST|OFN_PATHMUSTEXIST| OFN_ALLOWMULTISELECT|OFN_NOCHANGEDIR
if (WindowDll.GetOpenFileName(ofn))
{
StartCoroutine(Download(ofn.file));
}
}
IEnumerator Download(string url)
{
mediaPlayer.OpenVideoFromFile(MediaPlayer.FileLocation.RelativeToStreamingAssetsFolder, null, false);
UnityWebRequest request = UnityWebRequest.Get(url);
request.SendWebRequest();
if (request.isHttpError || request.isNetworkError)
{
//print("当前的下载发生错误" + request.error);
yield break;
}
while (!request.isDone)
{
//获取视频读取进度,有需要可以加入
//print("当前的下载进度为:" + request.downloadProgress);
//downloadProgress.text = (request.downloadProgress * 100).ToString() + "%";
yield return 0;
}
if (request.isDone)
{
//downloadProgress.text = "100%";
using (FileStream fs = new FileStream(savePath, FileMode.Create))
{
byte[] results = request.downloadHandler.data;
fs.Write(results, 0, results.Length);
fs.Flush();
fs.Close();
}
}
mediaPlayer.OpenVideoFromFile(MediaPlayer.FileLocation.RelativeToStreamingAssetsFolder, savePath, false);
//加载成功后打开并且播放
mediaPlayer.m_AutoOpen = true;
mediaPlayer.Play();
}
}
四、创建OpenFileName类用于打开窗口
/****************************************************
文件:OpenFileName.cs
作者:Mark
日期:#CreateTime#
功能:打开文件夹
*****************************************************/
using System;
using System.Runtime.InteropServices;
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public class OpenFileName
{
public int structSize = 0;
public IntPtr dlgOwner = IntPtr.Zero;
public IntPtr instance = IntPtr.Zero;
public String filter = null;
public String customFilter = null;
public int maxCustFilter = 0;
public int filterIndex = 0;
public String file = null;
public int maxFile = 0;
public String fileTitle = null;
public int maxFileTitle = 0;
public String initialDir = null;
public String title = null;
public int flags = 0;
public short fileOffset = 0;
public short fileExtension = 0;
public String defExt = null;
public IntPtr custData = IntPtr.Zero;
public IntPtr hook = IntPtr.Zero;
public String templateName = null;
public IntPtr reservedPtr = IntPtr.Zero;
public int reservedInt = 0;
public int flagsEx = 0;
}
public class WindowDll
{
[DllImport("Comdlg32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)]
public static extern bool GetOpenFileName([In, Out] OpenFileName ofn);
public static bool GetOpenFileName1([In, Out] OpenFileName ofn)
{
return GetOpenFileName(ofn);
}
}
五、挂载脚本并且运行
文章来源地址https://www.toymoban.com/news/detail-402701.html
到了这里,关于Unity打开本地文件夹替换视频的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!