有好几个办法,这边选择最简单的办法:
创建一个游戏物体,啥都行。组件就是孙悟空的技能。我让游戏物体有啥技能就加啥组件。
这里创建了个Quad平面,直接给他添加了组件叫Video Player(任何组件都有对应的类来存放它),直接把放到Unity Assets中的视频拖到Video Clip的地方即可;
按钮控制办法:
代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Video;
public class PlayVideo : MonoBehaviour
{
Button PlayButton;//存放按钮组件
VideoPlayer Myvideo;//存放视频播放组件
GameObject OneVideoPlan;//存放Video组件所在的游戏物体
bool isPlaying = false;
void Start()
{
PlayButton = GameObject.FindGameObjectWithTag("Playbutton").GetComponent<Button>();
Myvideo = GameObject.FindGameObjectWithTag("OneVideoPlan").GetComponent<VideoPlayer>();
if (Myvideo != null)
{
Debug.Log("Myvideo组件拿到");
Myvideo.playOnAwake = false;//拿到要找到的视频播放组建后关闭视频
}
else
{
Debug.Log("Myvideo组件mei拿到");
}
}
void Update()
{
do
{
if (PlayButton == null)
{
break;
}
else
{
PlayButton.onClick.AddListener(PlayVideoMoth);
}
} while (false);
}
void PlayVideoMoth()
{
//播放视频
if (!Myvideo.isPlaying)
{
Myvideo.Play();
isPlaying = true;
}
else if(Myvideo.isPlaying)
{
//Myvideo.Stop(); // 停止播放视频(视频回到起点)
Myvideo.Pause();//视频暂停
}
}
}