当前位置:网站首页>UnityEditor编辑器扩展-表格功能
UnityEditor编辑器扩展-表格功能
2022-06-26 22:41:00 【avi9111】
目录
因为要做个统计战场有多少,NPC,和建筑物的功能
突然发现,好像之前一直没做过表格控件(太遥远,找不到了)
第一时间,上网百度,结果还很快找到了一小哥的资料,及其开源代码
UnityEditor表格关键字
Scrope,TreeView
最终你懂的,Unity官方内部肯定有其控件,只是不公开而已
小哥说了:也希望unity尽快公开
但其实unity作为上市公司,目标是和unreal竞争做引擎+编辑器的,人家整个公司还在做“”各种升级“,根本不会在这些程序员的自定义的细微末节(imgui + c# mono层已经是很超前了,极其超前以至于无法超越)上花心思,官方更新的其实想都不用想
Editor编辑器扩展的关键还是使用了 internal 的组件
SerializedPropertyTable
和
SerializedPropertyTreeView.Column[]
小哥哥例子一:
列出Cameras
m_table = new SerializedPropertyTable("Table", FindObjects, CreateCameraColumn);//查找相机(好例子) private Camera[] FindObjects()
{
return FindObjectsOfType<Camera>();
} private SerializedPropertyTreeView.Column[] CreateCameraColumn(out string[] propnames)
{
propnames = new string[3];
var columns = new SerializedPropertyTreeView.Column[3];
columns[0] = new SerializedPropertyTreeView.Column
{
headerContent = new GUIContent("Name"),
headerTextAlignment = TextAlignment.Left,
sortedAscending = true,
sortingArrowAlignment = TextAlignment.Center,
width = 200,
minWidth = 25f,
maxWidth = 400,
autoResize = false,
allowToggleVisibility = true,
propertyName = null,
dependencyIndices = null,
compareDelegate = SerializedPropertyTreeView.DefaultDelegates.s_CompareName,
drawDelegate = SerializedPropertyTreeView.DefaultDelegates.s_DrawName,
filter = new SerializedPropertyFilters.Name()
};
columns[1] = new SerializedPropertyTreeView.Column
{
headerContent = new GUIContent("On"),
headerTextAlignment = TextAlignment.Left,
sortedAscending = true,
sortingArrowAlignment = TextAlignment.Center,
width = 25,
autoResize = false,
allowToggleVisibility = true,
propertyName = "m_Enabled",
dependencyIndices = null,
compareDelegate = SerializedPropertyTreeView.DefaultDelegates.s_CompareCheckbox,
drawDelegate = SerializedPropertyTreeView.DefaultDelegates.s_DrawCheckbox,
};
columns[2] = new SerializedPropertyTreeView.Column
{
headerContent = new GUIContent("Mask"),
headerTextAlignment = TextAlignment.Left,
sortedAscending = true,
sortingArrowAlignment = TextAlignment.Center,
width = 200,
minWidth = 25f,
maxWidth = 400,
autoResize = false,
allowToggleVisibility = true,
propertyName = "m_CullingMask",
dependencyIndices = null,
compareDelegate = SerializedPropertyTreeView.DefaultDelegates.s_CompareInt,
drawDelegate = SerializedPropertyTreeView.DefaultDelegates.s_DrawDefault,
filter = new SerializedPropertyFilters.Name()
};
for (var i = 0; i < columns.Length; i++)
{
var column = columns[i];
propnames[i] = column.propertyName;
}
return columns;
}对了,ongui必须写上(整个table结构内核还是基于ongui体系的,若是你之前写过OnGUI()就懂了)
using (new EditorGUILayout.VerticalScope())
{
m_table.OnGUI();
}
小哥哥例子二:
扩展自定义数据列表,
虽然例子一很好,但扩展性没有,几乎不能用,也只是证明了确实能实现数据Binder,甚至是Editor所有的内部绑定,但是Binder对应的字段属性,如Name,m_Enable等等,很难确定正确的名字(必须重复去尝试)
所以有了例子二,自定数据类外再包一层
定义自己的类
上面的方法一,只支持内部的方法(Unity.Object),小哥哥另外封装了一层写法,可支持自定义数据类型
//方法2
public class ExampleDataTable : CommonTable<GameRunTimeItem>
{
public ExampleDataTable(List<GameRunTimeItem> datas,
CommonTableColumn<GameRunTimeItem>[] cs,
FilterMethod<GameRunTimeItem> onfilter,
SelectMethod<GameRunTimeItem> onselect = null)
: base(datas, cs, onfilter, onselect)
{
}
} public class GameRunTimeItem
{
public string Name;
public int Count;
public int ID;
public int Age;
}包一层 Column
void InitTableData(bool forceUpdate = false)
{
if (forceUpdate || m_table == null)
{
//init datas
var datas = new List<GameRunTimeItem>();
//for example,add some test datas
datas.Add(new GameRunTimeItem() { ID = 101, Age = 10, Name = "Lili" });
datas.Add(new GameRunTimeItem() { ID = 203, Age = 15, Name = "JoJo" });
datas.Add(new GameRunTimeItem() { ID = 404, Age = 11, Name = "Kikki" });
datas.Add(new GameRunTimeItem() { ID = 508, Age = 30, Name = "Baby" });
//init columns
var cols = new CommonTableColumn<GameRunTimeItem>[]
{
new CommonTableColumn<GameRunTimeItem>
{
headerContent = new GUIContent("ID"), //header display name
canSort = true, //
Compare = (a,b)=>-a.ID.CompareTo(b.ID), //sort method
DrawCell = (rect,data)=>EditorGUI.LabelField(rect,data.ID.ToString()),
},
new CommonTableColumn<GameRunTimeItem>
{
headerContent = new GUIContent("Name"),//header display name
canSort = true,
Compare = (a,b)=>-a.Name.CompareTo(b.Name),//sort method
DrawCell = (rect,data)=>EditorGUI.LabelField(rect,data.Name),
},
new CommonTableColumn<GameRunTimeItem>
{
headerContent = new GUIContent("Age"),//header display name
DrawCell = (rect,data)=>EditorGUI.LabelField(rect,data.Age.ToString()),
}
};
m_table = new ExampleDataTable(datas, cols, OnTableFilter, OnRowSelect);
}
}
/// <summary>
///
/// </summary>
/// <param name="datas"></param>
private void OnRowSelect(List<GameRunTimeItem> datas)
{
throw new NotImplementedException();
}
private bool OnTableFilter(GameRunTimeItem data, string std)
{
int number;
if (!int.TryParse(std, out number))
return false;
return data.ID == number;
} using (new EditorGUILayout.VerticalScope())
{
if (m_table == null)
InitTableData();
if (m_table != null)
m_table.OnGUI();
}
最终截图

边栏推荐
- Yolov6: un cadre de détection de cibles rapide et précis est Open Source
- VB. Net class library - 4 screen shots, clipping
- Flower shop window layout [dynamic planning]
- 【混合编程jni 】第七篇之JNI 的命令行们
- [cloud native topic -51]:kubesphere cloud Governance - operation - step by step deployment of microservice based business applications - database middleware redis microservice deployment process
- Unity 设置Material、Shader的方法
- Vulnhub's DC8
- 从位图到布隆过滤器,C#实现
- leetcode:6107. 不同骰子序列的数目【dp六个状态 + dfs记忆化】
- C语言:简单计算器多次使用代码实现
猜你喜欢

大龄程序员的一些出路
![[cloud native topic -51]:kubesphere cloud Governance - operation - step by step deployment of microservice based business applications - database middleware redis microservice deployment process](/img/42/c2a25bb7a9fdad8fe0a048e9af44ca.jpg)
[cloud native topic -51]:kubesphere cloud Governance - operation - step by step deployment of microservice based business applications - database middleware redis microservice deployment process

VB. Net class library (advanced version - 1)

DLA model (classification model + improved segmentation model) + deformable convolution

WordPress collection plug-ins are recommended to be free collection plug-ins

CVPR 2022 - Interpretation of selected papers of meituan technical team

Configuring assimp Library in QT environment (MinGW compiler)

LabVIEW Arduino TCP/IP远程智能家居系统(项目篇—5)

Yolov6: the fast and accurate target detection framework is open source

leetcode:6103. 从树中删除边的最小分数【dfs + 联通分量 + 子图的值记录】
随机推荐
Comprehensive evaluation of online collaboration documents: note, flowus, WOLAI, Feishu, YuQue, Microsoft office, Google Docs, Jinshan docs, Tencent docs, graphite docs, Dropbox paper, nutcloud docs,
[mathematical modeling] spanning tree based on Matlab GUI random nodes [including Matlab source code 1919]
在Flutter中解析复杂的JSON
leetcode - 买卖股票的最佳时机
Raspberry pie preliminary use
Unity布料系统_Cloth组件(包含动态调用相关)
Flashtext, a data cleaning tool, has directly increased the efficiency by dozens of times
Is it safe to open an account and buy stocks? Who knows
在线协作文档综合评测 :Notion、FlowUs、Wolai、飞书、语雀、微软 Office、谷歌文档、金山文档、腾讯文档、石墨文档、Dropbox Paper、坚果云文档、百度网盘在线文档
Brief analysis of the self inspection contents of the blue team in the attack and defense drill
The sharp sword of API management -- eolink
分享三種在Excel錶格中自動求和的方法
Using C to operate SQLSERVER database through SQL statement tutorial
【混合编程jni 】第九篇之Jni总结
360 mobile assistant is the first to access the app signature service system to help distribute privacy and security
Système de distribution Unity Composants en tissu (y compris les dépendances d'appel dynamique)
Leetcode - the best time to buy or sell stocks
尚硅谷DolphinScheduler视频教程发布
vulnhub之dc8
[hybrid programming JNI] details of JNA in Chapter 11