Unity 3D功能主界面C#脚本
2014-01-13 19:05
507 查看
实现点击按钮进入该功能场景中,点击音乐播放器自动弹出window窗体,并实现音乐的播放和停止的切换、音量大小滚动条、音乐播放上一曲和下一曲的切换。
using UnityEngine;
using System.Collections;
public class HomePage : MonoBehaviour {
//功能按钮
public Texture2D angryBird;
public Texture2D musicBtn;
public Texture2D DrawBool;
public Texture2D vediobtn;
public Texture2D quitbtn;
//歌手图片
public Texture[] photos;
//定义播放器按钮点击次数
protected int i = 0;
//定义是否点击音频播放器按钮,显示音乐播放器winform
public bool result=false;
//音乐播放器功能按钮
public Texture2D musicVolumeSize;
public Texture2D MusicStartorStop;
public Texture2D MusicStart;
public Texture2D MusicStop;
public Texture2D MusicPause;
public Texture2D MusicUp;
public Texture2D MusicNext;
public Texture2D MusicLoop;
//音量
public float musicVolume;
//中文字体
public GUISkin ChineseSkin;
//音频文件
public AudioSource[] MyAudio;
//歌曲名称
public string[] SongNames;
//当前歌曲编号
public string[] SongerNames;
//当前歌曲编号
int SongIndex = 0;
//设定是否循环
bool LoopPlay = true;
//Use this for initialization
voidStart () {
//设置默认音量
musicVolume = 0.5F;
}
//Update is called once per frame
voidUpdate () {
}
void OnGUI() {
//游戏按钮
GUI.TextArea(new Rect(Screen.width * 0.13f, Screen.height * 0.15f,Screen.width * 0.12f, Screen.height * 0.04f), "奋斗的小鸟");
if (GUI.Button(new Rect(Screen.width * 0.1f, Screen.height * 0.2f,Screen.width * 0.18f, Screen.height * 0.18f), angryBird))
{
Application.LoadLevel("welcome");
}
//播放器按钮
GUI.TextArea(new Rect(Screen.width
4000
* 0.33f, Screen.height * 0.15f,Screen.width * 0.12f, Screen.height * 0.04f), "音频播放器");
if (GUI.Button(new Rect(Screen.width * 0.3f, Screen.height * 0.2f,Screen.width * 0.18f, Screen.height * 0.18f), musicBtn))
{
result = true;
//Application.LoadLevel("vedio");
}
//window播放器出现
if (result==true)
{
GUI.Window(0, new Rect(Screen.width *0.3f, Screen.height * 0.4f, Screen.width * 0.37f, Screen.height * 0.57f),function1, "我的音乐盒");
}
//涂鸦按钮
GUI.TextArea(new Rect(Screen.width * 0.73f, Screen.height * 0.15f, Screen.width* 0.12f, Screen.height * 0.04f), "涂鸦工具");
if (GUI.Button(new Rect(Screen.width * 0.7f, Screen.height * 0.2f,Screen.width * 0.18f, Screen.height * 0.18f), DrawBool))
{
Application.LoadLevel("DrawTool");
}
//视频播放器按钮
GUI.TextArea(new Rect(Screen.width * 0.53f, Screen.height * 0.15f,Screen.width * 0.12f, Screen.height * 0.04f), "视频播放器");
if (GUI.Button(new Rect(Screen.width * 0.5f, Screen.height * 0.2f,Screen.width * 0.18f, Screen.height * 0.18f), vediobtn))
{
Application.LoadLevel("vedioPlayer");
}
//返回按钮
//GUI.TextArea(new Rect(Screen.width * 0.84f, Screen.height * 0.3f,Screen.width * 0.05f, Screen.height * 0.04f), "返回");
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");
}
}
void function1(int mywindow){
GUI.skin = ChineseSkin;
//创建一个用于动态修改音乐音量的横向滑动条
musicVolume = GUI.VerticalScrollbar(new Rect(Screen.width * 0.04f,Screen.height * 0.21f, Screen.width * 0.18f, Screen.height * 0.18f),musicVolume, 0.01F, 1.0F, 0.0F);
//音量图标
GUI.Label(new Rect(Screen.width * 0.04f, Screen.height * 0.395f,Screen.width * 0.05f, Screen.height * 0.05f), musicVolumeSize);
//将音量的百分比显示出来
GUI.Label(new Rect(Screen.width * 0.005f, Screen.height * 0.16f,Screen.width * 0.3f, Screen.height * 0.05f), "当前音量:" +(int)(musicVolume * 100) + "%");
if (MyAudio[SongIndex].isPlaying)
{
MyAudio[SongIndex].volume = musicVolume;
}
//歌手图片
GUI.DrawTexture(new Rect(Screen.width * 0.14f, Screen.height * 0.04f,Screen.width * 0.225f, Screen.height * 0.455f), photos[SongIndex]);
if (GUI.Button(new Rect(Screen.width * 0.08f, Screen.height * 0.5f,Screen.width * 0.05f, Screen.height * 0.05f), MusicStartorStop))
{
i++;
if (i%2==1)
{
MyAudio[SongIndex].Play();
MusicStartorStop = MusicPause;
}
else
{
MyAudio[SongIndex].Stop();
MusicStartorStop =MusicStart;
}
}
if (GUI.Button(new Rect(Screen.width * 0.02f, Screen.height * 0.5f,Screen.width * 0.05f, Screen.height * 0.05f), MusicUp))
{
///方法一:停止播放当前音乐
//for (int i = 0; i < MyAudio.Length; i++)
//{
// if (MyAudio[i].isPlaying)
// {
// MyAudio[i].Stop();
// }
//}
//方法二:停止当前播放音乐
if (MyAudio[SongIndex].isPlaying)
{
MyAudio[SongIndex].Stop();
//上一曲
if (SongIndex > 0)
SongIndex--;
else if (LoopPlay)
SongIndex = MyAudio.Length- 1;
//播放当前音乐
MyAudio[SongIndex].Play();
}
else
{
if (SongIndex > 0)
SongIndex--;
else if (LoopPlay)
SongIndex = MyAudio.Length- 1;
//播放当前音乐
MyAudio[SongIndex].Stop();
}
}
if (GUI.Button(new Rect(Screen.width * 0.14f, Screen.height * 0.5f,Screen.width * 0.05f, Screen.height * 0.05f), MusicNext))
{
//方法一:停止播放当前音乐
//for (int i = 0; i < MyAudio.Length; i++)
//{
// if (MyAudio[i].isPlaying)
// {
// MyAudio[i].Stop();
// }
//}
//方法二:停止当前播放音乐
if (MyAudio[SongIndex].isPlaying)
{
MyAudio[SongIndex].Stop();
//下一曲
if (SongIndex <MyAudio.Length - 1)
SongIndex++;
else if (LoopPlay)
SongIndex = 0;
//播放当前音乐
MyAudio[SongIndex].Play();
}
else
{
//下一曲
if (SongIndex < MyAudio.Length- 1)
SongIndex++;
else if (LoopPlay)
SongIndex = 0;
//播放当前音乐
MyAudio[SongIndex].Stop();
}
}
//选择是否进行循环
GUI.contentColor = Color.white;
LoopPlay = GUI.Toggle(new Rect(Screen.width * 0.015f, Screen.height *0.45f, Screen.width * 0.05f, Screen.height * 0.05f), LoopPlay, MusicLoop);
GUI.Label(new Rect(Screen.width * 0.065f, Screen.height * 0.46f,Screen.width * 0.1f, Screen.height * 0.1f),"循环");
string[] mymusic = MyAudio[SongIndex].name.Split('-');
//显示当前歌手和歌曲名称
GUI.Label(new Rect(Screen.width * 0.04f, Screen.height * 0.06f,Screen.width * 0.1f, Screen.height * 0.2f), mymusic[0]);
GUI.Label(new Rect(Screen.width * 0.03f, Screen.height * 0.1f,Screen.width * 0.1f, Screen.height * 0.2f), mymusic[1]);
}
}
using UnityEngine;
using System.Collections;
public class HomePage : MonoBehaviour {
//功能按钮
public Texture2D angryBird;
public Texture2D musicBtn;
public Texture2D DrawBool;
public Texture2D vediobtn;
public Texture2D quitbtn;
//歌手图片
public Texture[] photos;
//定义播放器按钮点击次数
protected int i = 0;
//定义是否点击音频播放器按钮,显示音乐播放器winform
public bool result=false;
//音乐播放器功能按钮
public Texture2D musicVolumeSize;
public Texture2D MusicStartorStop;
public Texture2D MusicStart;
public Texture2D MusicStop;
public Texture2D MusicPause;
public Texture2D MusicUp;
public Texture2D MusicNext;
public Texture2D MusicLoop;
//音量
public float musicVolume;
//中文字体
public GUISkin ChineseSkin;
//音频文件
public AudioSource[] MyAudio;
//歌曲名称
public string[] SongNames;
//当前歌曲编号
public string[] SongerNames;
//当前歌曲编号
int SongIndex = 0;
//设定是否循环
bool LoopPlay = true;
//Use this for initialization
voidStart () {
//设置默认音量
musicVolume = 0.5F;
}
//Update is called once per frame
voidUpdate () {
}
void OnGUI() {
//游戏按钮
GUI.TextArea(new Rect(Screen.width * 0.13f, Screen.height * 0.15f,Screen.width * 0.12f, Screen.height * 0.04f), "奋斗的小鸟");
if (GUI.Button(new Rect(Screen.width * 0.1f, Screen.height * 0.2f,Screen.width * 0.18f, Screen.height * 0.18f), angryBird))
{
Application.LoadLevel("welcome");
}
//播放器按钮
GUI.TextArea(new Rect(Screen.width
4000
* 0.33f, Screen.height * 0.15f,Screen.width * 0.12f, Screen.height * 0.04f), "音频播放器");
if (GUI.Button(new Rect(Screen.width * 0.3f, Screen.height * 0.2f,Screen.width * 0.18f, Screen.height * 0.18f), musicBtn))
{
result = true;
//Application.LoadLevel("vedio");
}
//window播放器出现
if (result==true)
{
GUI.Window(0, new Rect(Screen.width *0.3f, Screen.height * 0.4f, Screen.width * 0.37f, Screen.height * 0.57f),function1, "我的音乐盒");
}
//涂鸦按钮
GUI.TextArea(new Rect(Screen.width * 0.73f, Screen.height * 0.15f, Screen.width* 0.12f, Screen.height * 0.04f), "涂鸦工具");
if (GUI.Button(new Rect(Screen.width * 0.7f, Screen.height * 0.2f,Screen.width * 0.18f, Screen.height * 0.18f), DrawBool))
{
Application.LoadLevel("DrawTool");
}
//视频播放器按钮
GUI.TextArea(new Rect(Screen.width * 0.53f, Screen.height * 0.15f,Screen.width * 0.12f, Screen.height * 0.04f), "视频播放器");
if (GUI.Button(new Rect(Screen.width * 0.5f, Screen.height * 0.2f,Screen.width * 0.18f, Screen.height * 0.18f), vediobtn))
{
Application.LoadLevel("vedioPlayer");
}
//返回按钮
//GUI.TextArea(new Rect(Screen.width * 0.84f, Screen.height * 0.3f,Screen.width * 0.05f, Screen.height * 0.04f), "返回");
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");
}
}
void function1(int mywindow){
GUI.skin = ChineseSkin;
//创建一个用于动态修改音乐音量的横向滑动条
musicVolume = GUI.VerticalScrollbar(new Rect(Screen.width * 0.04f,Screen.height * 0.21f, Screen.width * 0.18f, Screen.height * 0.18f),musicVolume, 0.01F, 1.0F, 0.0F);
//音量图标
GUI.Label(new Rect(Screen.width * 0.04f, Screen.height * 0.395f,Screen.width * 0.05f, Screen.height * 0.05f), musicVolumeSize);
//将音量的百分比显示出来
GUI.Label(new Rect(Screen.width * 0.005f, Screen.height * 0.16f,Screen.width * 0.3f, Screen.height * 0.05f), "当前音量:" +(int)(musicVolume * 100) + "%");
if (MyAudio[SongIndex].isPlaying)
{
MyAudio[SongIndex].volume = musicVolume;
}
//歌手图片
GUI.DrawTexture(new Rect(Screen.width * 0.14f, Screen.height * 0.04f,Screen.width * 0.225f, Screen.height * 0.455f), photos[SongIndex]);
if (GUI.Button(new Rect(Screen.width * 0.08f, Screen.height * 0.5f,Screen.width * 0.05f, Screen.height * 0.05f), MusicStartorStop))
{
i++;
if (i%2==1)
{
MyAudio[SongIndex].Play();
MusicStartorStop = MusicPause;
}
else
{
MyAudio[SongIndex].Stop();
MusicStartorStop =MusicStart;
}
}
if (GUI.Button(new Rect(Screen.width * 0.02f, Screen.height * 0.5f,Screen.width * 0.05f, Screen.height * 0.05f), MusicUp))
{
///方法一:停止播放当前音乐
//for (int i = 0; i < MyAudio.Length; i++)
//{
// if (MyAudio[i].isPlaying)
// {
// MyAudio[i].Stop();
// }
//}
//方法二:停止当前播放音乐
if (MyAudio[SongIndex].isPlaying)
{
MyAudio[SongIndex].Stop();
//上一曲
if (SongIndex > 0)
SongIndex--;
else if (LoopPlay)
SongIndex = MyAudio.Length- 1;
//播放当前音乐
MyAudio[SongIndex].Play();
}
else
{
if (SongIndex > 0)
SongIndex--;
else if (LoopPlay)
SongIndex = MyAudio.Length- 1;
//播放当前音乐
MyAudio[SongIndex].Stop();
}
}
if (GUI.Button(new Rect(Screen.width * 0.14f, Screen.height * 0.5f,Screen.width * 0.05f, Screen.height * 0.05f), MusicNext))
{
//方法一:停止播放当前音乐
//for (int i = 0; i < MyAudio.Length; i++)
//{
// if (MyAudio[i].isPlaying)
// {
// MyAudio[i].Stop();
// }
//}
//方法二:停止当前播放音乐
if (MyAudio[SongIndex].isPlaying)
{
MyAudio[SongIndex].Stop();
//下一曲
if (SongIndex <MyAudio.Length - 1)
SongIndex++;
else if (LoopPlay)
SongIndex = 0;
//播放当前音乐
MyAudio[SongIndex].Play();
}
else
{
//下一曲
if (SongIndex < MyAudio.Length- 1)
SongIndex++;
else if (LoopPlay)
SongIndex = 0;
//播放当前音乐
MyAudio[SongIndex].Stop();
}
}
//选择是否进行循环
GUI.contentColor = Color.white;
LoopPlay = GUI.Toggle(new Rect(Screen.width * 0.015f, Screen.height *0.45f, Screen.width * 0.05f, Screen.height * 0.05f), LoopPlay, MusicLoop);
GUI.Label(new Rect(Screen.width * 0.065f, Screen.height * 0.46f,Screen.width * 0.1f, Screen.height * 0.1f),"循环");
string[] mymusic = MyAudio[SongIndex].name.Split('-');
//显示当前歌手和歌曲名称
GUI.Label(new Rect(Screen.width * 0.04f, Screen.height * 0.06f,Screen.width * 0.1f, Screen.height * 0.2f), mymusic[0]);
GUI.Label(new Rect(Screen.width * 0.03f, Screen.height * 0.1f,Screen.width * 0.1f, Screen.height * 0.2f), mymusic[1]);
}
}
相关文章推荐
- Unity 3D开发-C#脚本语言的一些基础用法
- Unity 3D Coroutine&yield(C#脚本)
- Unity 3d C#和Javascript脚本互相调用 解决方案(非原创、整理资料,并经过实践得来)
- unity 3D学习日记:创建一个小场景并编写简单C#移动脚本
- Unity 3D视频播放器场景C#脚本
- C#Light(包括unity一切C#环境可用嵌入式脚本)0.10B稳定版发布,功能已定型
- unity 3D登录界面C#脚本
- Unity 3d中导入c#脚本时出现 can't add script 如何解决
- Unity 3d C#脚本(1)
- Unity 3D 控制物体上下左右均匀移动脚本
- C#程序员整理的Unity 3D笔记(十):Unity3D的位移、旋转的3D数学模型
- Unity C# 脚本教程
- Unity 2D角色控制脚本C#直接上代码
- 【Unity 3D】学习笔记二十四:unity游戏脚本(四)
- C#程序员整理的Unity 3D笔记(二十):2D Toolkit之官方教程《Whack a Mole》
- Unity 3d脚本加密方案
- unity JavaScript 与 c# 脚本之间的通信
- Unity中多个C#脚本中的Awake执行顺序
- C#程序员整理的Unity 3D笔记(十三):Unity 3D基于组件的思想
- 用unity来写小球转向后向目标移动 +c#脚本