Unity3D筆記七 GUILayout


 一、說到GUILayout就要提到GUI,二者的區別是什么

  GUILayout是游戲界面的布局。GUI(界面)和GUILayout(界面布局)功能上面是相似的從命名中就可以看到這兩個東西非常相像,但是在使用過程中兩者還是存在一定區別的。

1、使用GUI繪制控件的時候,需要設置控件的Rect()方法,這樣設置的控件非常不靈活,因為它的坐標以及大小已經固定死了,這時如果控件中的內容長度發生改變,就會直接影響展示效果。如果我們需要制作控件的自適應?怎么辦?GUILayout登場見2

2、GUILayout制作界面,可以很方便地為我們解決上述難題。GUILayout可以自適應組件大小和自動計算組件顯示區域,無須設定顯示區域,系統會自動幫我們計算控件的顯示區域,並且保證它們不會重疊。

 

 二、GUILayout常用分類

1、線性布局 

     1.1水平線性布局:首先需要使用BeginHorizontal()方法,然后將控件添加至線性布局當中,最后使用EndHorizontal()方法來結束當前線性布局

  1.2垂直線性布局:首先需要使用BeginVertical()方法與EndVertical()方法。

   public Texture2D texture2D;

   private void OnGUI()
   {
       //開始水平線性布局
        GUILayout.BeginHorizontal();
        GUILayout.Box("開始水平布局");
        GUILayout.Button("按鈕");
        GUILayout.Label("文本");
        GUILayout.TextField("輸入框");
        GUILayout.Box(texture2D, GUIStyle.none);
        //結束水平線性布局
        GUILayout.EndHorizontal();

        //開始垂直線性布局
        GUILayout.BeginVertical();
        GUILayout.Box("開始垂直布局");
        GUILayout.Button("按鈕");
        GUILayout.Label("文本");
        GUILayout.TextField("輸入框");
        GUILayout.Box(texture2D, GUIStyle.none);

        //objCube.renderer.material.mainTexture=texture2D動態加貼圖這么加的
        //結束垂直線性布局
        GUILayout.EndVertical();
   }

 

2、控件偏移

  使用Space()方法可以設置控件之間的偏移量

     #region  控件偏移
        GUILayout.BeginArea(new Rect(0, 10, 300, 100));//"開始一個顯示的區域"
        GUILayout.BeginHorizontal("開始最外層橫向布局");
        GUILayout.BeginVertical("嵌套第一個縱向布局");
        GUILayout.Box("Box11");
        GUILayout.Space(10);//兩個Box中間偏移10 像素
        GUILayout.Box("Box12");
        GUILayout.EndVertical();//"結束嵌套的縱向布局"
        GUILayout.Space(20);//兩個縱向布局間隔20像素
        GUILayout.BeginVertical("嵌套第二個縱向布局");
        GUILayout.Box("Box21");
        GUILayout.Space(10);//兩個Box中間偏移10 像素
        GUILayout.Box("Box22");
        GUILayout.EndVertical();//"結束嵌套的縱向布局"
        GUILayout.EndHorizontal();//結束最外層橫向布局
        GUILayout.EndArea();
        #endregion

3、對齊方式

  一般情況下,游戲窗口都是矩形的,而矩形由4個角組成:左上角、右上角、左下角和右下角。如果要使控件分別對齊在這4個角當中,就需要使用FlexibleSpace()對其做偏移了。FlexibleSpace(){[ˈfleksəbl]adj. 的原理是將兩個控件完全左右或上下對齊在顯示區域當中。它是一個非常重要的方法,在不同分辨率下可以直接確定偏移的位置,並且不會超出顯示范圍。

 #region 對齊方式
        GUILayout.BeginArea(new Rect(0,0,Screen.width,Screen.height));//開始一個顯示區域
        GUILayout.BeginHorizontal();//創建一個最外層橫向布局
        GUILayout.BeginVertical();//開始嵌套一個內層縱向布局
        GUILayout.Box("Box1");//兩個Box控件上下對齊 
        GUILayout.FlexibleSpace();
        GUILayout.Box("Box2");
        GUILayout.EndVertical();//結束內層嵌套的縱向布局
        GUILayout.FlexibleSpace();//布局之間左右對齊
        GUILayout.BeginVertical();
        GUILayout.Box("Box3");
        GUILayout.FlexibleSpace();
        GUILayout.Box("Box4");
        GUILayout.EndVertical();
        GUILayout.EndHorizontal();
        GUILayout.EndArea();
        #endregion

4、添加與關閉窗口

 游戲窗口是可以動態添加與關閉

 #region 窗口
    public ArrayList winList=new ArrayList();
    public Texture icon;
    #endregion
    // Use this for initialization
    void Start()
    { 
        winList.Add(new Rect(winList.Count*100, 50, 150, 50));
    }

 private void OnGUI()
    {
    #region 窗口

        int count = winList.Count;
        for (int i = 0; i < count; i++)
        {
            winList[i] = GUILayout.Window(i, new Rect(count*100, 50, 150, 100), AddWindow, "窗口ID:" + i);
        }

        #endregion
    }


 private void AddWindow(int id)
    {
        GUILayout.BeginHorizontal();//開始一個水平布局
        GUILayout.Label(icon, GUILayout.Width(50), GUILayout.Height(50));//繪制圖標
        GUILayout.Label("這是一個全新的窗口");
        GUILayout.EndHorizontal();

        GUILayout.BeginHorizontal();//開始一個水平布局
        if (GUILayout.Button("添加窗口按鈕"))
        {
            winList.Add(new Rect(winList.Count*100, 50, 150, 100));
        }

        if (GUILayout.Button("關閉窗口按鈕"))
        {
            winList.RemoveAt(id);
        }
        GUILayout.EndHorizontal();//關閉水平布局
        GUI.DragWindow(new Rect(0,0,Screen.width,Screen.height));
    }

 5、字體

  Unity支持所有.ttf的字符集,默認的字體為Arial[ə'rɪəl]角,輻數

  打開C:\Windows\Fonts 找到你需要的字體拖動到Unity當前工程資源下即可。

6、中文顯示

  默認是不支持中文的,可以把編碼方式改成UTF-8 如果不行就改成UTf-16

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM