UnityEditor编辑器扩展-表格功能

目录

UnityEditor表格关键字

小哥哥例子一:

小哥哥例子二:

最终截图


因为要做个统计战场有多少,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();
		}
		

最终截图

猜你喜欢

转载自blog.csdn.net/avi9111/article/details/125086527