前言
一、導(dǎo)入AVpro插件
AVpro插件是一款很強(qiáng)大的視頻播放插件,配合Unity使用有意想不到的效果,他的各項(xiàng)功能網(wǎng)上都有,我就不進(jìn)行展開討論了。
該插件的獲取方式為AssetStore中購(gòu)買下載,該插件有一個(gè)免費(fèi)版本可供使用,付費(fèi)版本的功能強(qiáng)大一點(diǎn)。
有需要的也可以私信我,我分享給你,你只能用于學(xué)習(xí),不可用于商用。
將下載好的AVpro導(dǎo)入U(xiǎn)nity。
二、插件對(duì)應(yīng)的UI界面
圖中顯示視頻的GUI主要是導(dǎo)入AVPro之后創(chuàng)建的,右鍵UI/AVproVideouGUI
在該GUI的Inspector窗口有一個(gè)Display uGui的組件,該組件中有一個(gè)MediaPlayer的選項(xiàng),該選項(xiàng)是一個(gè)播放器,右鍵添加該播放器并且拉入到該位置。文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-402701.html
三、創(chuàng)建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);//監(jiān)聽按鈕是否點(diǎn)擊,如果點(diǎn)擊就執(zhí)行打開窗口
}
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('/', '\\');
//默認(rèn)路徑
ofn.initialDir = path;
ofn.title = "選擇需要替換的視頻";
ofn.defExt = "mp4";//顯示文件的類型
//注意 一下項(xiàng)目不一定要全選 但是0x00000008項(xiàng)不要缺少
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("當(dāng)前的下載發(fā)生錯(cuò)誤" + request.error);
yield break;
}
while (!request.isDone)
{
//獲取視頻讀取進(jìn)度,有需要可以加入
//print("當(dāng)前的下載進(jìn)度為:" + 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();
}
}
四、創(chuàng)建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);
}
}
五、掛載腳本并且運(yùn)行
文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-402701.html
到了這里,關(guān)于Unity打開本地文件夾替換視頻的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!