实现视频的播放和暂停、快进、音量滚动条:
using UnityEngine;
using System.Collections;
public class vedioPlayer : MonoBehaviour {
//视频播放比按钮图片
public Texture2D vedioStartAndStop;
public Texture2D vedioStart;
public Texture2D vedioPause;
public Texture2D vedioStop;
public Texture2D vedioNextAndFast;
public Texture2D vedioFast;
public Texture2D vedioUp;
public Texture2D vedioNext;
public Texture2D musicVolumeSize;
public GUIStyle musicSIze;
//返回按钮
public Texture2D Backbtn;
//退出按钮
public Texture2D quitbtn;
public int i = 0;
public int j = 0;
//电影纹理
public MovieTexture movieTexture;
public AudioSource[] MyAudio;
//歌曲名称
//当前歌曲编号
int SongIndex = 0;
//音量
public float musicVolume;
//Use this for initialization
voidStart () {
//设置当前对象的主纹理为电影纹理
renderer.material.mainTexture = movieTexture;
//设置电影纹理播放模式为循环
movieTexture.loop = false;
//设置默认音量
musicVolume = 0.5F;
}
//Update is called once per frame
voidUpdate () {
}
void OnGUI() {
//视频播放按钮
if (GUI.Button(new Rect(Screen.width * 0.4f, Screen.height * 0.9f,Screen.width * 0.07f, Screen.height * 0.07f), vedioStartAndStop))
{
i++;
}
if (i % 2 == 1)
{
if (!movieTexture.isPlaying)
{
movieTexture.Play();
MyAudio[SongIndex].Play();
vedioStartAndStop = vedioPause;
}
}
if (i % 2 == 0)
{
if (movieTexture.isPlaying)
{
movieTexture.Pause();
MyAudio[SongIndex].Pause();
vedioStartAndStop = vedioStart;
}
}
//快进按钮
if (GUI.Button(new Rect(Screen.width * 0.5f, Screen.height * 0.9f,Screen.width * 0.07f, Screen.height * 0.07f), vedioNextAndFast))
{
j++;
}
if (j % 2 == 1 && movieTexture.isPlaying)
{
MyAudio[SongIndex].Play();
movieTexture.Play();
vedioNextAndFast = vedioFast;
}
if (j % 2 == 1 && !movieTexture.isPlaying)
{
vedioNextAndFast = vedioFast;
}
if (j % 2 == 0)
{
vedioNextAndFast = vedioNext;
}
//视频播放停止按钮
if (GUI.Button(new Rect(Screen.width * 0.3f, Screen.height * 0.9f,Screen.width * 0.07f, Screen.height * 0.07f), vedioStop))
{
movieTexture.Stop();
i = 0;
j = 0;
vedioStartAndStop = vedioStart;
vedioNextAndFast = vedioNext;
}
//创建一个用于动态修改音乐音量的横向滑动条
//GUISkin guistyle = Color.white;
musicVolume = GUI.HorizontalScrollbar(new Rect(Screen.width * 0.63f,Screen.height * 0.92f, Screen.width * 0.2f, Screen.height * 0.2f), musicVolume,0.01F, 0.0F, 1.0F);
//GUI.skin=Color.
//音量图标
GUI.Label(new Rect(Screen.width * 0.6f, Screen.height * 0.9f, Screen.width* 0.05f, Screen.height * 0.05f), musicVolumeSize);
//将音量的百分比显示出来
GUI.Label(new Rect(Screen.width * 0.005f, Screen.height * 0.03f,Screen.width * 0.3f, Screen.height * 0.05f), "当前音量:" +(int)(musicVolume * 100) + "%");
if (MyAudio[SongIndex].isPlaying)
{
MyAudio[SongIndex].volume = musicVolume;
}
//返回按钮
if (GUI.Button(new Rect(Screen.width * 0.87f, Screen.height * 0.05f,Screen.width * 0.05f, Screen.height * 0.08f), Backbtn))
{
Application.LoadLevel("Level_0");
}
//退出按钮
if (GUI.Button(new Rect(Screen.width * 0.94f, Screen.height * 0.05f,Screen.width * 0.05f, Screen.height * 0.08f), quitbtn))
{
Application.LoadLevel("Background");
}
}
}