欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Unity UGUI的HorizontalLayoutGroup水平布局組件介紹使用

 更新時(shí)間:2023年07月26日 11:45:59   作者:AlianBlank  
這篇文章主要為大家介紹了Unity UGUI的HorizontalLayoutGroup水平布局組件介紹使用,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

Unity UGUI的HorizontalLayoutGroup(水平布局)組件的介紹及使用

1. 什么是HorizontalLayoutGroup組件?

HorizontalLayoutGroup是Unity UGUI中的一種布局組件,用于在水平方向上對(duì)子物體進(jìn)行排列和布局。它可以根據(jù)一定的規(guī)則自動(dòng)調(diào)整子物體的位置和大小,使它們?cè)谒椒较蛏暇鶆蚍植肌?/p>

2. HorizontalLayoutGroup的工作原理

HorizontalLayoutGroup組件通過以下步驟實(shí)現(xiàn)水平布局:

  • 獲取所有子物體的RectTransform組件。
  • 根據(jù)子物體的大小和布局規(guī)則,計(jì)算出每個(gè)子物體的位置和大小。
  • 調(diào)整子物體的位置和大小,使它們?cè)谒椒较蛏暇鶆蚍植肌?/li>

3. HorizontalLayoutGroup的常用屬性

  • Spacing:子物體之間的間距。
  • ChildForceExpandWidth:是否強(qiáng)制子物體擴(kuò)展寬度以填充整個(gè)水平布局。
  • ChildForceExpandHeight:是否強(qiáng)制子物體擴(kuò)展高度以填充整個(gè)水平布局。
  • ChildControlWidth:是否控制子物體的寬度。
  • ChildControlHeight:是否控制子物體的高度。

4. HorizontalLayoutGroup的常用函數(shù)

  • CalculateLayoutInputHorizontal():計(jì)算水平布局的輸入。
  • CalculateLayoutInputVertical():計(jì)算垂直布局的輸入。
  • SetLayoutHorizontal():設(shè)置水平布局。
  • SetLayoutVertical():設(shè)置垂直布局。

5. 例子代碼

例子1:創(chuàng)建一個(gè)水平布局,并添加三個(gè)子物體

using UnityEngine;
using UnityEngine.UI;
public class Example : MonoBehaviour
{
    public HorizontalLayoutGroup layoutGroup;
    public GameObject childPrefab;
    void Start()
    {
        for (int i = 0; i < 3; i++)
        {
            GameObject child = Instantiate(childPrefab, layoutGroup.transform);
            child.GetComponent<Text>().text = "Child " + (i + 1);
        }
    }
}

操作步驟:

  • 創(chuàng)建一個(gè)空物體,并添加HorizontalLayoutGroup組件。
  • 創(chuàng)建一個(gè)子物體預(yù)制體,包含一個(gè)Text組件。
  • 將子物體預(yù)制體拖拽到layoutGroup的Child Prefab屬性中。
  • 運(yùn)行游戲,可以看到三個(gè)子物體在水平方向上均勻分布。

例子2:設(shè)置子物體之間的間距

using UnityEngine;
using UnityEngine.UI;
public class Example : MonoBehaviour
{
    public HorizontalLayoutGroup layoutGroup;
    public GameObject childPrefab;
    void Start()
    {
        layoutGroup.spacing = 20f;
        for (int i = 0; i < 3; i++)
        {
            GameObject child = Instantiate(childPrefab, layoutGroup.transform);
            child.GetComponent<Text>().text = "Child " + (i + 1);
        }
    }
}

操作步驟:

  • 創(chuàng)建一個(gè)空物體,并添加HorizontalLayoutGroup組件。
  • 創(chuàng)建一個(gè)子物體預(yù)制體,包含一個(gè)Text組件。
  • 將子物體預(yù)制體拖拽到layoutGroup的Child Prefab屬性中。
  • 將layoutGroup的Spacing屬性設(shè)置為20。
  • 運(yùn)行游戲,可以看到子物體之間的間距變?yōu)?0。

例子3:強(qiáng)制子物體擴(kuò)展寬度以填充整個(gè)水平布局

using UnityEngine;
using UnityEngine.UI;
public class Example : MonoBehaviour
{
    public HorizontalLayoutGroup layoutGroup;
    public GameObject childPrefab;
    void Start()
    {
        layoutGroup.childForceExpandWidth = true;
        for (int i = 0; i < 3; i++)
        {
            GameObject child = Instantiate(childPrefab, layoutGroup.transform);
            child.GetComponent<Text>().text = "Child " + (i + 1);
        }
    }
}

操作步驟:

  • 創(chuàng)建一個(gè)空物體,并添加HorizontalLayoutGroup組件。
  • 創(chuàng)建一個(gè)子物體預(yù)制體,包含一個(gè)Text組件。
  • 將子物體預(yù)制體拖拽到layoutGroup的Child Prefab屬性中。
  • 將layoutGroup的Child Force Expand Width屬性設(shè)置為true。
  • 運(yùn)行游戲,可以看到子物體的寬度被擴(kuò)展以填充整個(gè)水平布局。

例子4:控制子物體的寬度和高度

using UnityEngine;
using UnityEngine.UI;
public class Example : MonoBehaviour
{
    public HorizontalLayoutGroup layoutGroup;
    public GameObject childPrefab;
    void Start()
    {
        layoutGroup.childControlWidth = false;
        layoutGroup.childControlHeight = false;
        for (int i = 0; i < 3; i++)
        {
            GameObject child = Instantiate(childPrefab, layoutGroup.transform);
            child.GetComponent<Text>().text = "Child " + (i + 1);
        }
    }
}

操作步驟:

  • 創(chuàng)建一個(gè)空物體,并添加HorizontalLayoutGroup組件。
  • 創(chuàng)建一個(gè)子物體預(yù)制體,包含一個(gè)Text組件。
  • 將子物體預(yù)制體拖拽到layoutGroup的Child Prefab屬性中。
  • 將layoutGroup的Child Control Width和Child Control Height屬性都設(shè)置為false。
  • 運(yùn)行游戲,可以看到子物體的寬度和高度不受控制,保持原始大小。

例子5:自定義布局規(guī)則

using UnityEngine;
using UnityEngine.UI;
public class Example : MonoBehaviour
{
    public HorizontalLayoutGroup layoutGroup;
    public GameObject childPrefab;
    void Start()
    {
        layoutGroup.childAlignment = TextAnchor.MiddleCenter;
        for (int i = 0; i < 3; i++)
        {
            GameObject child = Instantiate(childPrefab, layoutGroup.transform);
            child.GetComponent<Text>().text = "Child " + (i + 1);
        }
    }
}

操作步驟:

  • 創(chuàng)建一個(gè)空物體,并添加HorizontalLayoutGroup組件。
  • 創(chuàng)建一個(gè)子物體預(yù)制體,包含一個(gè)Text組件。
  • 將子物體預(yù)制體拖拽到layoutGroup的Child Prefab屬性中。
  • 將layoutGroup的Child Alignment屬性設(shè)置為Middle Center。
  • 運(yùn)行游戲,可以看到子物體在水平方向上居中對(duì)齊。

注意事項(xiàng)

  • HorizontalLayoutGroup組件只能用于水平布局,如果需要垂直布局,可以使用VerticalLayoutGroup組件。
  • 子物體的RectTransform組件的錨點(diǎn)和位置會(huì)影響布局效果,需要根據(jù)實(shí)際需求進(jìn)行調(diào)整。

參考資料

Unity官方文檔 - HorizontalLayoutGroup

以上就是Unity UGUI的HorizontalLayoutGroup水平布局組件介紹使用的詳細(xì)內(nèi)容,更多關(guān)于Unity UGUI水平布局組件的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • 基于Unity3D實(shí)現(xiàn)3D迷宮小游戲的示例代碼

    基于Unity3D實(shí)現(xiàn)3D迷宮小游戲的示例代碼

    迷宮游戲作為經(jīng)典的小游戲,一直深受大家的喜愛。本文小編將為大家詳細(xì)介紹一下如何用Unity實(shí)現(xiàn)一個(gè)3D版的迷宮小游戲,感興趣的可以動(dòng)手試一試
    2022-03-03
  • C# AE之返回上一級(jí)和下一級(jí)的實(shí)戰(zhàn)操作

    C# AE之返回上一級(jí)和下一級(jí)的實(shí)戰(zhàn)操作

    這篇文章主要介紹了C# AE之返回上一級(jí)和下一級(jí)的實(shí)戰(zhàn)操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2021-01-01
  • Windows Form 分頁 具體實(shí)現(xiàn)

    Windows Form 分頁 具體實(shí)現(xiàn)

    其實(shí)功能實(shí)現(xiàn)很簡(jiǎn)單。我做的是一個(gè)通用的分頁控件。項(xiàng)目時(shí)間很緊,可能有點(diǎn)粗糙。歡迎大家斧正。不說了直接貼代碼吧
    2013-12-12
  • C#中使用angular的方法步驟

    C#中使用angular的方法步驟

    在本篇內(nèi)容里我們給大家整理了關(guān)于C#中使用angular的方法以及具體步驟內(nèi)容,有興趣的朋友們學(xué)習(xí)下。
    2019-06-06
  • 基于C#實(shí)現(xiàn)俄羅斯方塊游戲

    基于C#實(shí)現(xiàn)俄羅斯方塊游戲

    這篇文章主要為大家詳細(xì)介紹了基于C#實(shí)現(xiàn)俄羅斯方塊游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-06-06
  • C#10的13個(gè)特性

    C#10的13個(gè)特性

    本文詳細(xì)講解了C#10的13個(gè)特性,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-12-12
  • 使用c#實(shí)現(xiàn)微信自動(dòng)化功能

    使用c#實(shí)現(xiàn)微信自動(dòng)化功能

    這篇文章主要介紹了使用c#實(shí)現(xiàn)微信自動(dòng)化,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-08-08
  • c# DateTime常用操作實(shí)例(datetime計(jì)算時(shí)間差)

    c# DateTime常用操作實(shí)例(datetime計(jì)算時(shí)間差)

    字符串操作DateTime操作,datetime計(jì)算時(shí)間差,取當(dāng)前時(shí)間,更多方法看下面代碼
    2013-12-12
  • C# 設(shè)計(jì)模式系列教程-建造者模式

    C# 設(shè)計(jì)模式系列教程-建造者模式

    用戶只需要指定要建造的類型就可以得到它們,而具體的建造過程和細(xì)節(jié)不需要知道。
    2016-06-06
  • C#優(yōu)雅的實(shí)現(xiàn)INotifyPropertyChanged接口

    C#優(yōu)雅的實(shí)現(xiàn)INotifyPropertyChanged接口

    這篇文章介紹了C#實(shí)現(xiàn)INotifyPropertyChanged接口的方法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-08-08

最新評(píng)論