CR开发笔记-3GUI界面制作

今天是第三篇

今天主要是做UI界面,非常容易,我也不用NGUI直接上好了!

直接上代码!

using UnityEngine;
using System.Collections;

public class Startmenu : MonoBehaviour
{
    
    float fullWidth;
    float fullHeight;
    Rect rtFullscreen;
    Rect rtBeginBtn;
    //窗口定义
    //这个玩意的效果就是只让一个窗口存在
    bool WindowsExist;
    //关于窗口
    Rect rtInfoBtn;
    Rect rtInfoWin;
    bool stInfoWin;
    Rect rtInfoWinBtn;
    //设置窗口
    Rect rtSetupBtn;
    Rect rtSetupWin;
    bool stSetupWin;
    Rect rtSetupWinBtn;
    //关卡窗口
    Rect rtLevelBtn;
    Rect rtLevelWin;
    bool stLevelWin;
    Rect rtLevelWinBtn;


    // Use this for initialization
    void Start () {
        WindowsExist = false;
        fullWidth=Screen.width;
        fullHeight=Screen.height;
        rtFullscreen = new Rect(0, 0, fullWidth, fullHeight);
        rtBeginBtn = new Rect(fullWidth/2, (float)(fullHeight*0.382), (float)(fullWidth * 0.309), (float)(fullHeight * 0.2));
        rtLevelBtn = new Rect(fullWidth / 2, (float)(fullHeight * 0.25), (float)(fullWidth * 0.2), (float)(fullHeight * 0.132));
        rtSetupBtn = new Rect(fullWidth/2,(float)(fullHeight*0.582),(float)(fullWidth*0.2),(float)(fullHeight*0.118));
        rtInfoBtn = new Rect (fullWidth/2,(float)(fullHeight*0.7),(float)(fullWidth*0.15),(float)(fullHeight*0.1));
        //米娜桑,为什么要在这里这么干呢,明明可以在下面直接用new的,这是为了提高运作效率
        //但是这是有缺点的,就是你不能在游戏中拉伸画面,因为这个代码只运行一遍,也就是一开始就订好了宽长
        //关于窗口
        rtInfoWin = new Rect(fullWidth / 4, fullHeight / 4, fullWidth / 2, fullHeight / 2);
        stInfoWin = false;
        rtInfoWinBtn = new Rect(0, (float)(rtInfoWin.height * 0.75), rtInfoWin.width, (float)(rtInfoWin.height * 0.25));
        //设置窗口
        rtSetupWin = new Rect(fullWidth / 4, fullHeight / 4, fullWidth / 2, fullHeight / 2);
        stSetupWin = false;
        rtSetupWinBtn = new Rect(0, (float)(rtSetupWin.height * 0.75), rtSetupWin.width, (float)(rtSetupWin.height * 0.25));
        //关卡窗口
        rtLevelWin = new Rect(0, 0, fullWidth, fullHeight);
        stLevelWin = false;
        rtLevelWinBtn = new Rect(0, (float)(rtLevelWin.height*0.85), rtLevelWin.width, (float)(rtLevelWin.height * 0.15));
    }
    
    // Update is called once per frame
    void Update () {
    
    }
    void OnGUI()
    {
        GUI.Box(rtFullscreen, "hehe");
        if(GUI.Button(rtBeginBtn, "try"))
        {
            Application.LoadLevel("startscene");
        }
        //关卡窗口
        if (GUI.Button(rtLevelBtn, "Level"))
        {
            if (WindowsExist == false)
            {
                stLevelWin = true;
                WindowsExist = true;
            }
        }
        if (stLevelWin)
        {
            GUI.Window(2, rtLevelWin, doWindow,"gege");
        }
        //设置窗口
        if (GUI.Button(rtSetupBtn, "setup"))
        {
            if (WindowsExist == false)
            {
                stSetupWin = true;
                WindowsExist = true;
            }
        }
        if (stSetupWin)
        {
            GUI.Window(3, rtSetupWin, doWindow, "hehe");
        }
        //关于窗口
        if (GUI.Button(rtInfoBtn, "hehe"))
        {
            if (WindowsExist == false)
            {
                stInfoWin = true;
                WindowsExist = true;
            }
        }
        if (stInfoWin)
        {
            GUI.Window(4, rtInfoWin, doWindow, "hehe");
        }
        
    }
    //窗口事件
    void doWindow(int winID)
    {
        if (winID == 4)
        {
            if (GUI.Button(rtInfoWinBtn, "hehe"))
            {
                stInfoWin = false;
                WindowsExist = false;
            }
        }
        if (winID == 3)
        {
            if (GUI.Button(rtSetupWinBtn, "hehe"))
            {
                stSetupWin = false;
                WindowsExist = false;
            }
        }
        if (winID == 2)
        {
            if (GUI.Button(rtLevelWinBtn,"gege"))
            {
                stLevelWin = false;
                WindowsExist = false;
            }
        }
    }
}

效果图

这些我们暂且不提了

除了上面的一大堆东西,我也就不解释什么了。。。明天画画还要交作业。(我的七个姐姐和八个妹妹问我为什么跪倒在数位板面前)

有几个需要注意一下的地方

1、基本上任何数字*0.几几都会自动转化为double,但是double效率非常慢,一般要转为float,这点需要注意。(括号很重要)

2、也不知道什么原因,可能是图层的问题,于是很多情况下,虽然有弹出窗口,但是在窗口下面点击之后就会弹出另外一个弹出窗口。。。这时候需要一个开关,使得只存在一个弹出窗口。

3、切换关卡问题 Application.LoadLevel("startscene"); 需要在file—buildset里进行设置才可以使用并且加载,这里可以探讨一下unity的运行机制,其实可以使用异步加载,以后再说。

基本就是这些。

下一次将会带来一些美工性质的工作,建飞机模型或者是UI什么的,插一个进度,要不太没意思了。估计要写到30章去啊!

原文地址:https://www.cnblogs.com/zuoguangxing/p/3840485.html