Unity3D视频播放
Unity能够直接识别的视频格式.mov,.mpg,.mped,.mp4,.avi,.asf
旧版本(MovieTexture)导入的文件不是.ogv通过Unity中的QuickTIme进行转码。.ogv会直接识别
视频播放:
代码语言:javascript复制旧版本通过一个Panel,设置MeshRender的Material的MainTexture为一个MovieTexture。
public class TestVideo : MonoBehaviour {
private MeshRenderer meshRenderer;
private AudioSource audioSource;
public MovieTexture videoTexture;
private void Awake()
{
audioSource = GetComponent<AudioSource>();
GetComponent<MeshRenderer>().material.mainTexture = videoTexture;
}
private void OnGUI()
{
if (GUILayout.Button("play"))
{
if (!videoTexture.isPlaying)
{
videoTexture.Play();
audioSource.Play();
}
}
if (GUILayout.Button("Pause"))
{
if (videoTexture.isPlaying)
{
videoTexture.Pause();
audioSource.Pause();
}
}
if (GUILayout.Button("Stop"))
{
if (videoTexture.isPlaying)
{
videoTexture.Stop();
audioSource.Stop();
}
}
}
}
通过OnGUI绘制MovieTexture进行播放
代码语言:javascript复制public class TestVideo : MonoBehaviour {
//private MeshRenderer meshRenderer;
private AudioSource audioSource;
public MovieTexture videoTexture;
private void Awake()
{
audioSource = GetComponent<AudioSource>();
//GetComponent<MeshRenderer>().material.mainTexture = videoTexture;
}
private void OnGUI()
{
GUI.DrawTexture(new Rect(0, 0, Screen.width, Screen.height), videoTexture, ScaleMode.StretchToFill);
if (GUILayout.Button("play"))
{
if (!videoTexture.isPlaying)
{
videoTexture.Play();
audioSource.Play();
}
}
if (GUILayout.Button("Pause"))
{
if (videoTexture.isPlaying)
{
videoTexture.Pause();
audioSource.Pause();
}
}
if (GUILayout.Button("Stop"))
{
if (videoTexture.isPlaying)
{
videoTexture.Stop();
audioSource.Stop();
}
}
}
}
新版动画系统
代码语言:javascript复制private VideoPlayer videoPlayer;
private void Awake()
{
videoPlayer = GetComponent<VideoPlayer>();
}
private void OnGUI()
{
if (GUILayout.Button("Play", GUILayout.Width(100), GUILayout.Height(100)))
{
videoPlayer.Play();
}
}