玲珑杯Unity开发心得——开始菜单GUI制作

///2015/08/04//

//by xbw/

/环境  unity 4.6//


先看一下效果图

不错吧,,,

来代码

using UnityEngine;
using System.Collections;
[RequireComponent(typeof(AudioSource))]
public class MainMenuGUI : MonoBehaviour {

    public AudioClip beep;
    public GUISkin menuSkin;
    public Rect menuArea;
    public Rect playButton;
    public Rect instructionsButton;
    public Rect quitButton;
    public Rect playwaysButton;
    public Rect instructions;
    public Rect HighscoresButton;

    public Texture2D img;

    Rect menuAreaNormalized;
    string menuPage = "main";
	// Use this for initialization
	void Start () {

        menuAreaNormalized =
            new Rect(menuArea.x * Screen.width - (menuArea.width * 0.3f), menuArea.y * Screen.height - (menuArea.height * 0.3f), menuArea.width, menuArea.height);
       // menuAreaNormalized =
        //  new Rect(150, 70, Screen.width, Screen.height);
	}
    void OnGUI()
    {
        GUIStyle backGround = new GUIStyle();

        backGround.normal.background = img;

        GUI.Label(new Rect(0, 0, Screen.width, Screen.height), "", backGround);


        GUI.skin = menuSkin;
        GUI.BeginGroup(menuAreaNormalized);
        if (menuPage == "main")
        {
            if (GUI.Button(new Rect(playButton), "测试模式"))
            {
                //StartCoroutine("ButtonAction", "second");
                //Application.LoadLevel("second");
                audio.PlayOneShot(beep);
                menuPage = "test";
            }
            if(GUI.Button(new Rect(HighscoresButton), "酷跑模式"))
            {
               // audio.PlayOneShot(beep);
                StartCoroutine("ButtonAction", "Loading4");
            }
            if (GUI.Button(new Rect(instructionsButton), "介绍说明"))
            {
                audio.PlayOneShot(beep);
                menuPage = "instructions";
            }
            //if (GUI.Button(new Rect(playwaysButton), "playways"))
           // {
             //   audio.PlayOneShot(beep);
             //   menuPage = "playways";
           // }
            if (GUI.Button(new Rect(quitButton), "退出游戏"))
            {
                StartCoroutine("ButtonAction", "quit");
            }
           
        }
        else if(menuPage=="instructions")
        {
            GUI.Label(new Rect(instructions), "玲珑轮胎测试模式(卡车,跑车,停车入位测试,方向键控制),玲珑酷跑模式(收集四枚玲珑轮胎,空格/触摸,通过碰撞左右移动) ");
            if(GUI.Button(new Rect(quitButton),"Back"))
            {
                audio.PlayOneShot(beep);
                menuPage = "main";
            }
        }
        else if(menuPage == "instructionsa")
        {
            GUI.Label(new Rect(instructions), "未解锁,请选择酷跑模式");
            if (GUI.Button(new Rect(quitButton), "Back"))
            {
                audio.PlayOneShot(beep);
                menuPage = "main";
            }
        }
        else if(menuPage == "instructionsb")
        {
            GUI.Label(new Rect(instructions), "未解锁,请选择酷跑模式");
            if (GUI.Button(new Rect(quitButton), "Back"))
            {
                audio.PlayOneShot(beep);
                menuPage = "main";
            }
        }
        else if(menuPage == "instructionsc")
        {
            GUI.Label(new Rect(instructions), "未解锁,请选择酷跑模式");
            if (GUI.Button(new Rect(quitButton), "Back"))
            {
                audio.PlayOneShot(beep);
                menuPage = "main";
            }
        }
        else if(menuPage=="test")
        {
            //GUI.Label(new Rect(instructions), "方向键控制移动");

            if (GUI.Button(new Rect(playButton), "卡车"))
            {
                audio.PlayOneShot(beep);
                //menuPage = "main";
                //StartCoroutine("ButtonAction", "Loading");
                menuPage = "instructionsa";
            }
            if (GUI.Button(new Rect(HighscoresButton), "跑车"))
            {
                audio.PlayOneShot(beep);
                //menuPage = "main";
                //StartCoroutine("ButtonAction", "Loading2");
                menuPage = "instructionsb";

            }
            if (GUI.Button(new Rect(instructionsButton), "挑战模式"))
            {
                audio.PlayOneShot(beep);
                //menuPage = "main";
                //StartCoroutine("ButtonAction", "Loading3");
                menuPage = "instructionsc";

            }
            if (GUI.Button(new Rect(quitButton), "Back"))
            {
                audio.PlayOneShot(beep);
                menuPage = "main";
            }
        }
        GUI.EndGroup();
    }
    IEnumerator ButtonAction(string levelName)
    {
        audio.PlayOneShot(beep);
        yield return new WaitForSeconds(0.35f);
        
        if(levelName!="quit")
        {
            Application.LoadLevel(levelName);
        }
        else
        {
            Application.Quit();
            Debug.Log("Have Quit");
        }
    }
	// Update is called once per frame
	void Update () {
	
	}
}

整理了一下素材

链接:http://pan.baidu.com/s/1gdxYZnl 密码:3tu2 

一些参数菜单的位置大小自行调整即可


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值