NGUI UIGrid组件:打造灵活的UI布局

引言

在Unity游戏开发中,UI的布局管理是一个常见且关键的任务。NGUI(Next-Gen UI)提供了一个强大的组件UIGrid,它可以帮助开发者以网格形式灵活地管理UI元素的布局。本文将详细介绍UIGrid的使用方法和技巧,帮助你在项目中实现复杂而美观的UI布局。

UIGrid简介

UIGrid是一个布局组件,允许开发者以网格形式排列UI元素,从而创建整洁、响应式的界面布局。它支持自动填充、排序和对齐等功能。

UIGrid的核心特性

  • 自动布局UIGrid能够自动排列其子对象,无需手动调整每个元素的位置。
  • 滚动支持:支持水平和垂直滚动,方便查看超出视图范围的内容。
  • 自定义网格:可以自定义行列数、单元格大小和间距。
  • 动态内容:支持动态添加和删除子对象,UIGrid会自动重新排列。

组件详解

UIGrid 是一个帮助脚本,可让您轻松地将小部件排列到固定大小的网格中。它既可以在编辑时使用,也可以在运行时使用。如果需要可变大小的单元格,可以改用 UITable。
在这里插入图片描述
要使用 UIGrid,请选择一个面板,右键单击场景视图中的任意位置,然后从 [/b]Create 菜单中选择 Grid。您也可以简单地将 UIGrid 组件附加到任何空的游戏对象。
在这里插入图片描述
Arrangement 决定了子对象的位置。水平意味着它们将向右延伸,垂直意味着它们将向下延伸。

Max Per Line 字段控制“水平”排列中的列数和“垂直”排列中的行数。

Cell WidthCell Height 确定网格内项目之间的间距。

默认情况下,Grid 将简单地重新定位其所有子项,并且顺序将是子项恰好创建的顺序。如果要更改此设置并按特定顺序对它们进行排序,可以按字母顺序命名它们(“001”、“002”、“003”等),然后选中“已排序”复选框。这样做将使网格首先按顺序对它们进行排序,然后再调整它们的位置。

最后,如果要保留不可见(禁用)子项留下的间距,请关闭“隐藏非活动”标志。默认情况下,此标志处于打开状态,不可见的子项将被忽略。

提示

Grid 对于在运行时定位事物很有用,但您也可以在编辑时执行它。只需右键单击它并选择“执行”选项。然后,如果不需要该组件,则可以安全地删除该组件。
在这里插入图片描述

使用UIGrid

  1. 创建UIGrid:在Unity编辑器中,创建一个新的UIGrid对象,通常它是UIPanel的子对象。
  2. 设置网格属性:在Inspector视图中,设置UIGrid的属性,如行列数、单元格宽度和高度、滚动条的可见性等。
  3. 添加子对象:将需要排列的UI元素(如UILabelUIButton等)作为UIGrid的子对象添加进去。
  4. 配置滚动:如果需要滚动功能,可以添加UIScrollView组件到UIGrid,并配置滚动条的相关属性。
  5. 自定义单元格:通过脚本动态创建和配置子对象,以实现更复杂的布局需求。

通过脚本控制UIGrid

using UnityEngine;
using System.Collections.Generic;

public class UIGridExample : MonoBehaviour
{
    public UIGrid grid;
    public GameObject cellPrefab; // 单元格预制体

    void Start()
    {
        // 假设我们有一个数据列表
        List<string> items = new List<string> { "Item 1", "Item 2", "Item 3" };
        
        // 为列表中的每个项创建一个单元格
        foreach (string item in items)
        {
            GameObject cell = NGUITools.AddChild(grid.gameObject, cellPrefab);
            // 配置单元格内容
            UILabel label = cell.GetComponent<UILabel>();
            label.text = item;
        }
    }
}

优化UIGrid性能

  • 重用单元格:当使用滚动功能时,可以通过重用滑出视图的单元格来减少对象的创建和销毁,提高性能。
  • 对象池:实现一个对象池来管理单元格的实例,避免频繁的InstantiateDestroy调用。

结语

UIGrid是NGUI中一个非常有用的组件,它简化了UI布局的管理,特别是对于需要展示大量数据的列表和网格。通过合理配置和脚本控制,你可以创建出既美观又高效的UI布局,提升玩家的交互体验。更多NGUI开发教程,请加入知识星球:游戏新质力。

NGUI下载地址

Overview NGUI is a powerful UI system and event notification framework for Unity (both Pro and Free) written in C# that closely follows the KISS principle. It features clean code and simple, minimalistic approach to everything. Many behaviour classes are kept under 200 lines of code. For a programmer this means a much easier time when it comes to working with the kit — from extending its functionality to tweaking the existing one. For everyone else this means better performance, less frustration, and more fun. Features Full Inspector integration No need to hit Play to see the results What you see in the Scene view is what you get in the Game view (fully WYSIWYG) Component-based, modular nature: attach the behaviours you want to make your widgets do what you want without having to code. Full support for iOS/Android, Blackberry, Win8, WP8, Flash Flexible event system Make complex UIs that take only 1 draw call Create your atlases right in the editor, update/modify them at will, or import an atlas from the Texture Packer. Support for lighting, normal mapping, refraction, and more — unleash your creativity! Support for clipped panels with hard or soft edges. Support for a flexible size table, letting your widgets automatically push others out of the way. Support for eastern languages with IME input Built-in Localization system Built-in keyboard and controller support An assortment of useful scripts to help you — from changing a button color to dragging an object Simple built-in tweening system. Clean, short, simple and extensively optimized C# code No DLLs or external resources
3.0.6 - NEW: NGUI now has new written documentation. - NEW: NGUI now has an abundance of context-sensitive help. Just right click on an NGUI component and choose the Help option. - NEW: NGUI now has robust context menus letting you add, create and modify widgets by right-clicking on stuff in the Scene View. - NEW: You can now find an assortment of ready-made controls ready to be drag & dropped into your scenes (search for "Wooden"). - NEW: You can now drag & drop GUI prefabs from your Project Folder right into the Scene View. No need to create the UI beforehand. - NEW: You can now copy/paste label styles by right-clicking the UILabel script in Inspector. - NEW: Redesigned the draggable panel class a bit, and renamed it to UIScrollView. UIDragPanelContents is now UIDragScrollView. - NEW: Labels can now have gradients. - NEW: Clipped panels now have handles you can drag around instead of adjusting clipping in inspector. - NEW: Added a new widget type capable of drawing Unity 4.3 sprites. - NEW: Added UIToggle.GetActiveToggle. - NEW: You can now specify a material on dynamic font-using UILabels. - NEW: You can now specify character spacing on labels, and it works with both bitmap and dynamic fonts. - NEW: Labels set to maintain their crispness will now take UIRoot's size into account, resulting in crisp labels with fixed size UIs. - NEW: Added a simple script that makes it possible to center a scrollable panel on a child when clicked on. - NEW: Redesigned the scroll bar and the slider components. They now also derive from a new common class (Progress Bar). - NEW: UIButtonKeyBinding has been replaced with UIKeyBinding and its functionality has been enhanced. - FIX: Panels will now add rigidbodies to themselves since Unity 4.3 mentions it should improve performance. - FIX: UIScrollView's movement restriction now makes sense (no more weird 'scale') - FIX: Draggable panels should no longer move on Play. - FIX: Improved performance by reducing GC allocations and mesh assignments. - FIX: Typewriter and text list scripts can now be used with dynamic fonts. - FIX: Reference atlas references should no longer get broken when modifying sprites. - FIX: Popup list now offers a way to change the font's size even for fixed size fonts. - FIX: Popup list was not enveloping the content correctly. - FIX: Atlas and font selector will now show all existing assets, not just recently used ones. - FIX: Atlas maker should no longer throw an exception when trying to edit old atlases (SciFi etc). - FIX: Enabling and disabling widgets will no longer cause the draw call list to be rebuilt unless it's necessary. - FIX: Improving the process of NGUI remembering the last used values. - FIX: Drag handles will now hide if there is not enough space to draw them. - FIX: Anchors and stretch scripts set to "run only once" will still respond to screen size changes. - FIX: Switching panel to clipped mode then back should refresh the shader correctly. - FIX: Moving widgets around no longer causes their buffers to get rebuilt. Just re-transformed. - FIX: Added extra code to ensure that draw calls won't get orphaned. - FIX: Panel alpha is now cumulative (parents affect children). - FIX: Got rid of old double-buffering code that was causing issues. - FIX: More Win8 tweaks. - DEL: UIPopupList no longer has a 'textLabel' option. Instead use label's SetCurrentSelection for OnValueChanged. - DEL: UIGrid and UITable no longer have 'repositionNow' member variable. Right-click it to execute it instead.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Unity打怪升级

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值