Unity编辑器拓展-滚动列表

滚动列表

  • 不搞移动端那种下拉加载更多,上拉刷新
  • 使用分页和跳页的方式展示列表数据
  • 本文提供一个带有翻页功能的自定义滚动列表

效果图

在这里插入图片描述

构造滚动列表需要用到的API

//以下需要成对出现
GUILayout.BeginScrollView
GUILayout.EndScrollView

GUILayout.BeginVertical
GUILayout.EndVertical

GUILayout.BeginHorizontal
GUILayout.EndHorizontal

//Unity编辑器的滚动列表是在滚动视图中间放一个包含内容的垂直或者水平布局组件

代码

using System;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

namespace DC.DCIMGUIBox
{
    public struct ListViewUserData
    {
        public Vector2 pos;
        public int pageIndex;
        public int onePageCnt;
        public int newPageIndex;

        public ListViewUserData(Vector2 pos, int pageIndex, int onePageCnt, int newPageIndex)
        {
            this.pos = pos;
            this.pageIndex = pageIndex;
            this.onePageCnt = onePageCnt;
            this.newPageIndex = newPageIndex;
        }
    }

    public class ScrollWindow_03 : EditorWindow
    {
        [MenuItem("DC/IMGUI/ScrollWindow_03")]
        public static void Open()
        {
            var window = GetWindow<ScrollWindow_03>();
            window.minSize = new Vector2(800, 600);
        }

        List<int> dataListA = new List<int>();

        private ListViewUserData listInfo = new ListViewUserData(Vector2.zero, 0, 20, 0);

        void OnGUI()
        {
            if (dataListA.Count == 0)
            {
                for (int i = 0; i < 100; i++)
                {
                    dataListA.Add(i);
                }
            }

            listInfo = ScrollList(listInfo, true, dataListA, Render,
                new GUILayoutOption[] {GUILayout.Width(200), GUILayout.Height(200)},
                new GUILayoutOption[] { });
        }

        public void Render<T>(int index, T data)
        {
            GUILayout.Label(string.Format("id:{0} name:{1}", index, index));
        }

        public ListViewUserData ScrollList<T>(ListViewUserData listInfo, bool vertical, List<T> dataList,
            Action<int, T> renderFunc, GUILayoutOption[] svLp, GUILayoutOption[] containerLp)
        {
            var startIndex = listInfo.pageIndex * listInfo.onePageCnt;
            if (startIndex < 0)
            {
                return listInfo;
            }

            if (dataList.Count == 0)
            {
                GUILayout.Label("dataList is empty! 空列表");
                return listInfo;
            }

            if (startIndex > dataList.Count)
            {
                GUILayout.Label("page index is too big! 超最大页数了");
                return listInfo;
            }

            //页头
            GUILayout.BeginHorizontal();

            EditorGUILayout.LabelField("页号", GUILayout.Width(30));

            listInfo.newPageIndex = Math.Max(0, EditorGUILayout.IntField(listInfo.newPageIndex, GUILayout.Width(30)));

            EditorGUILayout.LabelField("每页显示", GUILayout.Width(50));

            listInfo.onePageCnt = Math.Max(1, EditorGUILayout.IntField(listInfo.onePageCnt, GUILayout.Width(30)));

            if (GUILayout.Button("跳转", GUILayout.Width(40)))
            {
                listInfo.pageIndex = listInfo.newPageIndex;
                if (vertical)
                {
                    listInfo.pos.y = 0;
                }
                else
                {
                    listInfo.pos.x = 0;
                }
            }

            GUILayout.EndHorizontal();

            //列表
            listInfo.pos = GUILayout.BeginScrollView(listInfo.pos, svLp);

            var endIndex = Math.Min(startIndex + listInfo.onePageCnt, dataList.Count);

            if (vertical)
            {
                GUILayout.BeginVertical(containerLp);
            }
            else
            {
                GUILayout.BeginHorizontal(containerLp);
            }

            for (int i = startIndex; i < endIndex; i++)
            {
                renderFunc(i, dataList[i]);
            }

            if (vertical)
            {
                GUILayout.EndVertical();
            }
            else
            {
                GUILayout.EndHorizontal();
            }

            GUILayout.EndScrollView();

            return listInfo;
        }
    }
}
  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Unity编辑器拓展(Editor Extension)可以通过自定义的脚本来扩展Unity编辑器的功能和界面,以满足特定项目的需求。通过编辑器拓展,开发者可以创建自定义的编辑器窗口、工具栏按钮、菜单项、检视面板等,来增强Unity编辑器的功能和流程。 要创建一个Unity编辑器拓展,你可以使用C#编写一个继承自Editor类的脚本。这个脚本可以通过Unity的Inspector面板来设置相关的属性和行为。以下是一个简单的示例: ```csharp using UnityEngine; using UnityEditor; public class MyEditorExtension : EditorWindow { [MenuItem("Custom Tools/My Editor Window")] public static void OpenWindow() { // 创建并打开一个自定义的编辑器窗口 MyEditorExtension window = (MyEditorExtension)EditorWindow.GetWindow(typeof(MyEditorExtension)); window.Show(); } private void OnGUI() { // 在编辑器窗口中绘制UI元素 GUILayout.Label("Hello, I am a custom editor window!"); if (GUILayout.Button("Click Me")) { Debug.Log("Button clicked!"); } } } ``` 上述代码创建了一个自定义的编辑器窗口,并在窗口中绘制了一个标签和一个按钮。通过在Unity编辑器中点击"Custom Tools"菜单下的"My Editor Window",可以打开这个自定义的编辑器窗口。 除了编辑器窗口,你还可以通过继承Editor类来创建自定义的检视面板、菜单项等。Unity官方文档中有更详细的教程和示例,可以帮助你更深入地了解和使用Unity编辑器拓展

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值