您的位置:首页 > 移动开发 > Unity3D

Unity3D学习 愤怒的小鸟之Play界面(五)

2012-07-10 00:02 706 查看
目标:给Play界面添加一个退出界面和背景音乐



1. 添加一个退出按钮,方法和第四讲添加Play按钮一样

if (GUI.Button(Rect(650, 450, 100, 100), "", "ExitButton")) {

}


2. 添加一个弹出的窗口,用来给用户选择退出还是不退出。

#pragma strict

var customSkin : GUISkin;
var OKButton : Texture2D;
var cancelButton : Texture2D;
private var showExitWindow : boolean = false;

function Start () {

}

function Update () {

}

function OnGUI () {
GUI.matrix = Matrix4x4.TRS(Vector3.zero, Quaternion.identity,
Vector3(Screen.width / 800.0, Screen.height / 600.0, 1));

GUI.skin = customSkin;

if (showExitWindow) {
GUI.Window(0, Rect(200, 200, 400, 200), DoMyWindow, "");
}
else {
if (GUI.Button(Rect(250, 225, 300, 150), "", "PlayButton")) {
Application.LoadLevel(2);
}

if (GUI.Button(Rect(650, 450, 100, 100), "", "ExitButton")) {
showExitWindow = true;
}
}
}

function DoMyWindow () {
if (GUI.Button(Rect(32, 90, 100, 100), cancelButton)) {
showExitWindow = false;
}
if (GUI.Button(Rect(267, 90, 100, 100), OKButton)) {
}
}
这个代码里涉及到一个新的GUI,那就是Window。使用方法也简单,创建时跟其他GUI控件类似,只不过这里面多一个参数是函数,在这个函数里用户可以绘画自己想要的窗口,以及做自己想做的事。详见:http://docs.unity3d.com/Documentation/ScriptReference/GUI.Window.html

注意:a. 在DoMyWindow函数里绘制GUI时,窗口的大小不能以整个GUI的窗口大小进行绘制,要以你创建的window的大小来绘制。
b. 创建完window后,你会发现鼠标在window的后面,这时你需要调整下鼠标的深度,在绘制鼠标的那个脚本的OnGUI函数里添加一句:GUI.depth = 0.2f;,这样鼠标就始终在Window之上了

3. 添加背景音乐
点击camera,然后选择Component--->Audio--->Audio Source,这样就给camera添加了一个音乐属性,然后将背景音乐文件赋给这个audio source,在选项里将循环播放勾选上,就搞定了。

OK,大事告成,运行下试试!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: