Unity 3D视频播放器场景C#脚本
2014-01-13 19:07
483 查看
实现视频的播放和暂停、快进、音量滚动条:
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");
}
}
}
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");
}
}
}
相关文章推荐
- unity 3D学习日记:创建一个小场景并编写简单C#移动脚本
- Unity 3d中导入c#脚本时出现 can't add script 如何解决
- Unity 3d C#脚本(1)
- Unity 3D开发-C#脚本语言的一些基础用法
- unity 3D登录界面C#脚本
- Unity 3D Coroutine&yield(C#脚本)
- Unity 3D功能主界面C#脚本
- Unity 3d C#和Javascript脚本互相调用 解决方案(非原创、整理资料,并经过实践得来)
- Unity 3D学习笔记(三)——关于脚本
- Unity 3D 动态创建对象,并渲染贴图,动态添加删除脚本
- Unity 3D - UI摄像机、场景摄像机协同工作原理
- Unity摄像机 向指定位置移动旋转 C#脚本
- Unity中XLua开发之使用C#加载lua脚本
- unity 3d 射击游戏 (一)场景和飞机
- unity 第三人称控制对象平移转向C#脚本(亲测有效)
- UNITY技巧-查找脚本被哪个场景引用
- 【Unity 3D】学习笔记二十七:unity游戏脚本(七)
- unity 3D 场景缩放代码
- 【Unity基础知识②】创建C#脚本、Debug.log输出语句
- Unity3D实用脚本(一)—— 导出场景中的3D资源到本地为.obj格式