当前位置:网站首页>Unity editor expansion - scrolling list
Unity editor expansion - scrolling list
2022-07-03 08:26:00 【T.D.C】
Scroll through the list
- Do not engage in the pull-down loading of the mobile terminal , Pull up to refresh
- Use paging and page skipping to display list data
- This article provides a custom scrolling list with page turning function
design sketch
To construct a scrolling list API
// The following need to appear in pairs
GUILayout.BeginScrollView
GUILayout.EndScrollView
GUILayout.BeginVertical
GUILayout.EndVertical
GUILayout.BeginHorizontal
GUILayout.EndHorizontal
//Unity The scrolling list of the editor is to place a vertical or horizontal layout component containing content in the middle of the scrolling view
Code
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! An empty list ");
return listInfo;
}
if (startIndex > dataList.Count)
{
GUILayout.Label("page index is too big! The maximum number of pages is exceeded ");
return listInfo;
}
// Header
GUILayout.BeginHorizontal();
EditorGUILayout.LabelField(" Page number ", GUILayout.Width(30));
listInfo.newPageIndex = Math.Max(0, EditorGUILayout.IntField(listInfo.newPageIndex, GUILayout.Width(30)));
EditorGUILayout.LabelField(" Each page shows ", GUILayout.Width(50));
listInfo.onePageCnt = Math.Max(1, EditorGUILayout.IntField(listInfo.onePageCnt, GUILayout.Width(30)));
if (GUILayout.Button(" Jump ", GUILayout.Width(40)))
{
listInfo.pageIndex = listInfo.newPageIndex;
if (vertical)
{
listInfo.pos.y = 0;
}
else
{
listInfo.pos.x = 0;
}
}
GUILayout.EndHorizontal();
// list
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;
}
}
}
边栏推荐
猜你喜欢
Easy touch plug-in
P1596 [USACO10OCT]Lake Counting S
Installation of PHP FPM software +openresty cache construction
matlab神经网络所有传递函数(激活函数)公式详解
Osgearth target selection
C语言-入门-精华版-带你走进编程(一)
Constraintlayout's constraintset dynamically modifies constraints
KunlunBase MeetUP 等您来!
P1596 [USACO10OCT]Lake Counting S
Introduction to Base64 coding
随机推荐
Haproxy+kept cluster setup 02
[set theory] order relation (hastu example | divisive relation hastu | inclusive relation hastu | refinement relation hastu)
Transmit pictures with Base64 encoding
Osgearth starry background
数据分析练习题
数据库应用技术课程设计之商城管理系统
Use filechannel to copy files
Unity change default editor
Some understandings of 3dfiles
Shader foundation 01
Vscode, idea, VIM development tool shortcut keys
Get to know unity2 for the first time
Graphics_ Learnopongl learning notes
Osganimation library parsing
MAE
使用base64编码传图片
C语言-入门-精华版-带你走进编程(一)
[usaco12mar]cows in a skyscraper g (state compression DP)
Cloudcompare learning (1) - cloudcompare compilation and common plug-in implementation
Visual Studio (VS) shortcut keys