当前位置:网站首页>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();
}
最终截图

边栏推荐
- 买股票通过中金证券经理的开户二维码开户资金是否安全?想开户炒股
- 【BUG反馈】WebIM在线聊天系统发消息时间问题
- [cloud native topic -51]:kubesphere cloud Governance - operation - step by step deployment of microservice based business applications - database middleware redis microservice deployment process
- 美术向的Unity动画知识
- Yolov6: the fast and accurate target detection framework is open source
- Introduction of classic wide & deep model and implementation of tensorflow 2 code
- 中金证券经理的开户链接开户买股票安全吗?有谁知道啊
- fastadmin极光推送发送消息的时候registration_id多个用逗号分割后无效
- Unity: 脚本缺失 “The referenced script (Unknown) on this Behaviour is missing!“
- MacOS環境下使用HomeBrew安裝[email protected]
猜你喜欢

Product design in the extreme Internet Era

数据清洗工具flashtext,效率直接提升了几十倍数

Convolutional neural network (CNN) explanation and tensorflow2 code implementation

开放世界机甲游戏-Phantom Galaxies

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,

Implementation of collaborative filtering evolution version neuralcf and tensorflow2

从位图到布隆过滤器,C#实现

LabVIEW Arduino tcp/ip remote smart home system (project part-5)
![leetcode:152. Product maximum subarray [consider DP of two dimensions]](/img/c8/af6a4c969affd151a5214723dffb57.png)
leetcode:152. Product maximum subarray [consider DP of two dimensions]

Bs-gx-016 implementation of textbook management system based on SSM
随机推荐
Unity: 脚本缺失 “The referenced script (Unknown) on this Behaviour is missing!“
【LeetCode】1984. Minimum difference between highest and lowest of K scores
Briefly describe the model animation function of unity
[fundamentals of image processing] GUI image curve adjustment system based on MATLAB [including Matlab source code 1923]
在哪家券商公司开户最方便最安全可靠
Unity布料系統_Cloth組件(包含動態調用相關)
[solution] sword finger offer 15 Number of 1 in binary (C language)
MacOS環境下使用HomeBrew安裝[email protected]
FPGA -vga display
Three solutions for improving embedded software development environment
Leetcode (452) - detonate the balloon with the minimum number of arrows
YOLOv6:又快又准的目标检测框架开源啦
vulnhub之DC9
开放世界机甲游戏-Phantom Galaxies
leetcode:6103. 从树中删除边的最小分数【dfs + 联通分量 + 子图的值记录】
从位图到布隆过滤器,C#实现
Flutter 中 ValueNotifier<List<T>> 监听问题解决
Restfultoolkitx of idea utility plug-in -- restful interface debugging
Matrix derivation and its chain rule
MATLAB and MySQL database connection and data exchange (based on ODBC)