当前位置:网站首页>UnityEditor編輯器擴展-錶格功能
UnityEditor編輯器擴展-錶格功能
2022-06-26 22:45: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();
}
最終截圖
边栏推荐
猜你喜欢
How to download on selenium computer -selenium download and installation graphic tutorial [ultra detailed]
FPGA -VGA显示
The sharp sword of API management -- eolink
Weaving dream collection plug-ins are recommended to be free collection plug-ins
Briefly describe the model animation function of unity
leetcode:6103. 从树中删除边的最小分数【dfs + 联通分量 + 子图的值记录】
数据清洗工具flashtext,效率直接提升了几十倍数
从位图到布隆过滤器,C#实现
Yolov6: the fast and accurate target detection framework is open source
Vulnhub's dc9
随机推荐
Different subsequence problems I
leetcode:141. 环形链表【哈希表 + 快慢指针】
[hybrid programming JNI] details of JNA in Chapter 11
[mixed programming JNI] Part 7: JNI command lines
DAST black box vulnerability scanner part 5: vulnerability scanning engine and service capability
树莓派初步使用
leetcode:6107. 不同骰子序列的数目【dp六个状态 + dfs记忆化】
尚硅谷DolphinScheduler视频教程发布
Are there any risks for the top ten securities companies to register and open accounts? Is it safe?
[mixed programming JNI] Part 12 jnaerator
不同的子序列问题I
Selenium电脑上怎么下载-Selenium下载和安装图文教程[超详细]
买股票通过中金证券经理的开户二维码开户资金是否安全?想开户炒股
WordPress collection plug-ins are recommended to be free collection plug-ins
Release of dolphin scheduler video tutorial in Shangsi Valley
What is the “ How to remove a custom form list?
论文解读(LG2AR)《Learning Graph Augmentations to Learn Graph Representations》
Wechat applet is authorized to log in wx getUserProfile
FPGA -VGA显示
Vulnhub's DC8