一直跟着龚老师用js写,VS智能感应用习惯后发现这里用js对初学者比较蛋疼,MONO有提示但是还是无法和VS媲美就目前来看。所以这次还是换成熟悉的VS来开发。
目标:实现关卡页面
跑起来的效果如下:
方法和之前添加图片一样,先创建GUITexture背景图片,再创建GUISkin 用于创建上图中7个图片按钮。手型鼠标直接把上节的customCusor.js
拿来即可,背景音乐选择Main Camera->Component->Audio->Audio Source
C#代码如下
using UnityEngine;
using System.Collections; public class selectGUI : MonoBehaviour
{
public GUISkin mySkin;
/// <summary>
/// 返回按钮
/// </summary>
private Rect backPosition = new Rect(40, 280, 50, 50);
/// <summary>
/// 按钮1
/// </summary>
private Rect level1Position = new Rect(60, 10, 100, 100);
/// <summary>
/// 按钮2
/// </summary>
private Rect level2Position = new Rect(220, 10, 100, 100);
/// <summary>
/// 按钮3
/// </summary>
private Rect level3Position = new Rect(380, 10, 100, 100);
/// <summary>
/// 按钮4
/// </summary>
private Rect level4Position = new Rect(60, 160, 100, 100);
/// <summary>
/// 按钮5
/// </summary>
private Rect level5Position = new Rect(220, 160, 100, 100);
/// <summary>
/// 按钮6
/// </summary>
private Rect level6Position = new Rect(380, 160, 100, 100);
// Use this for initialization
void Start()
{ } // Update is called once per frame
void Update()
{ } void OnGUI()
{
GUI.skin = mySkin;
if (GUI.Button(level1Position,"",GUI.skin.GetStyle("LevelButton1")))
{
Application.LoadLevel(4);
}
if (GUI.Button(level2Position, "", GUI.skin.GetStyle("LevelButton2")))
{
Application.LoadLevel(4);
}
if (GUI.Button(level3Position, "", GUI.skin.GetStyle("LevelButton3")))
{
Application.LoadLevel(4);
}
if (GUI.Button(level4Position, "", GUI.skin.GetStyle("LevelButton4")))
{
Application.LoadLevel(4);
}
if (GUI.Button(level5Position, "", GUI.skin.GetStyle("LevelButton5")))
{
Application.LoadLevel(4);
}
if (GUI.Button(level6Position, "", GUI.skin.GetStyle("LevelButton6")))
{
Application.LoadLevel(4);
}
if (GUI.Button(backPosition, "", GUI.skin.GetStyle("BackButton")))
{
Application.LoadLevel(2);
}
}
}
一点要注意在JS中var mySkin:GUISkin 表示公共的变量在C#中默认是私有变量所以要写出public GUISkin mySkin; 这点小区别注意。
成功跑起来后就可以把之前的三个场景Sences组合起来跑一次