前言
一、导入AVpro插件
AVpro插件是一款很强大的视频播放插件,配合Unity使用有意想不到的效果,他的各项功能网上都有,我就不进行展开讨论了。 该插件的获取方式为AssetStore中购买下载,该插件有一个免费版本可供使用,付费版本的功能强大一点。 有需要的也可以私信我,我分享给你,你只能用于学习,不可用于商用。
将下载好的AVpro导入Unity。
二、插件对应的UI界面
图中显示视频的GUI主要是导入AVPro之后创建的,右键UI/AVproVideouGUI
在该GUI的Inspector窗口有一个Display uGui的组件,该组件中有一个MediaPlayer的选项,该选项是一个播放器,右键添加该播放器并且拉入到该位置。
三、创建ChangeVideo脚本
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;
public DisplayUGUI displayUGUI;
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";
ofn.flags = 0x00080000 | 0x00001000 | 0x00000800 | 0x00000200 | 0x00000008;
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)
{
yield break;
}
while (!request.isDone)
{
yield return 0;
}
if (request.isDone)
{
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类用于打开窗口
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);
}
}
五、挂载脚本并且运行
|