创建canvas,canvas下创建rawimage,对大小进行参数调整,再在资源中创建渲染纹理Tex,将Tex放入rawimage中组件的纹理,在rawimage添加videoplayer组件,Tex也放目标纹理,在rawimage下创建按钮,更改text。新建C#脚本Open URL Manager,代码如下
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Video;
public class OpenURLManager : MonoBehaviour
{
public Button button1;
public Button button2;
public Button button3;
public Button button4;
public Button button5;
public Button button6;
public Button button_PlayOrPause;
public VideoPlayer videoPlayer;
private void Start()
{
button1.onClick.AddListener(OpenURL1);
button2.onClick.AddListener(OpenURL2);
button3.onClick.AddListener(OpenURL3);
button4.onClick.AddListener(OpenURL4);
button5.onClick.AddListener(OpenURL5);
button6.onClick.AddListener(OpenURL6);
}
private void OpenURL1()
{
string videoURL = "file://D:/CloudMusic/MV/Blue - Haven’t Found You Yet.mp4";
PlayVideo(videoURL);
}
private void OpenURL2()
{
string videoURL = "file://D:/CloudMusic/MV/Martin Garrix,Dean Lewis - Used To Love (Acoustic Version).mp4";
PlayVideo(videoURL);
}
private void OpenURL3()
{
string videoURL = "file://C:/Users/46309/Videos/Counter-strike Global Offensive/shuai.mp4";
PlayVideo(videoURL);
}
private void OpenURL4()
{
string videoURL = "file://D:/CloudMusic/MV/Martin Garrix,Dean Lewis - Used To Love (Acoustic Version).mp4";
PlayVideo(videoURL);
}
private void OpenURL5()
{
string videoURL = "file://D:/CloudMusic/MV/Martin Garrix,Dean Lewis - Used To Love (Acoustic Version).mp4";
PlayVideo(videoURL);
}
private void OpenURL6()
{
string videoURL = "file://D:/CloudMusic/MV/Martin Garrix,Dean Lewis - Used To Love (Acoustic Version).mp4";
PlayVideo(videoURL);
}
private void PlayVideo(string videoURL)
{
videoPlayer.url = videoURL;
videoPlayer.Play();
}
}
将脚本拖入rawimage,在填上相对应的button
ps:本地视频路径要填写以 file:开头的路径,网络以https开头
有些视频不够撑满画布的,可以在canavs中加rawimage组件,把背景改为黑色
button颜色和透明度(材质)等可以在组件中调整
其他参数按需调整。
最终效果