2017-1-11 Unity Editor MenuItem

2017-1-11 Unity Editor MenuItem

添加菜单项

经常需要在Unity的编辑器里面写一些编辑器的插件,比如在菜单栏加上自己的开发的插件,比如在 Hierachy界面右键添加一些选项,这里面就涉及到对UnityEditor Attribute 的了解。

MenuItem

现在我想在编辑器界面的导航栏加一栏自己的导航,列如需要在菜单栏里面添加一个新的菜单项 “zz”,下面包含一个2级菜单 "tt"
添加一个zz的浏览项

只需要在特定的方法上面添加Attribute 就行了

using UnityEngine;
using System.Collections;
using UnityEditor;

public class NewMeunItem
{
   [MenuItem("zz/tt")]
   public static void Test()
   {
   }
}

在对应的方面上面标明 [MeunItem("zz/tt)"] 后,Unity会自动查找已有的菜单项,如果找到名称一样的 zz,就会添加一个2级菜单项,如果没有就会重新生成一个菜单项 。
有了这个基础之后,就方便理解之后的在 Hierachy 面板 添加选项,和在 Asset面板添加选中,需要注意的是 在 Hierachy面板添加 2级菜单需要 [MenuItem(GameObject/xxx)],因为 Hierachy面板引用的是 GameObject的内容。

博客学习

上面的内容是对编辑器添加插件的原理的基本解释,有了这个基础之后,看这篇博文应该就更容易理解了
Unity编辑器扩展-菜单项

官网资料

博文的最后有Unity官网文档的链接:
Unity 官网 MenuItem
除此之外,Unity官网还提供了教程
Unity Editor Extensions - MenuItems

c# Attribute

那么Unity 是 如何通过 [MenuItem]来查找到哪些方法是用来添加我们自定义的菜单项,这里需要用到C#提供的Attribute类。

博客链接

先放一篇我个人认为比较好的博文
hyddd 浅析C#的Attribute
文章浅显易懂,比较好理解

MSDN Attribute

放一个 MSDN的资料
MSDN Attribute

下面是对 Attirbute类的测试结果,便于我们理解

示例代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Test
{
    public enum EnumDBFiledUsage
    {
        None = 0x00,
        PrimaryKey = 0x01,
        UniqueKey = 0x02,
        BySystem = 0x04
    }

    [AttributeUsage(AttributeTargets.Property, Inherited = true)]
    class DBFieldAttribute : Attribute
    {
        EnumDBFiledUsage m_usage;
        string m_strFieldName;
        string m_strDescription;
        object m_defaultValue;

        public DBFieldAttribute(string strFieldName, object defaultValue, EnumDBFiledUsage usage, string strDescription)
        {
            m_strFieldName = strFieldName;
            m_defaultValue = defaultValue;
            m_usage = usage;
            m_strDescription = strDescription;
        }

        public DBFieldAttribute(string fieldName)
            : this(fieldName, null, EnumDBFiledUsage.None, null)
        {

        }

        public DBFieldAttribute(string fieldName, EnumDBFiledUsage usage)
            : this(fieldName, null, usage, null)
        {

        }

        public string FiledName
        {
            get
            {
                return m_strFieldName;
            }
            set
            {
                m_strFieldName = value;
            }
        }

        public object DefaultValue
        {
            get
            {
                return m_defaultValue;
            }
            set
            {
                m_defaultValue = value;
            }
        }

        public string Usage
        {
            get
            {
                return m_usage.ToString();
            }
        }
    }

    class DalObj
    {
        string m_strTableName;
        int m_nID;
        string m_strName;
        string m_password;

        public DalObj(string strTableName)
        {
            m_strTableName = strTableName;
        }
        [DBField("id", EnumDBFiledUsage.PrimaryKey)]
        public int ID
        {
            get { return m_nID; }
            set { m_nID = value; }
        }
        [DBField("name", DefaultValue = "游客")]
        public string name
        {
            get { return m_strName; }
            set { m_strName = value; }
        }
        [DBField("pwd")]
        public string Password
        {
            get { return m_password; }
            set { m_password = value; }
        }
    }
}

测试

            DalObj dalObj = new DalObj("users");

            StringBuilder sb = new StringBuilder();
            
            foreach(PropertyInfo proInfo in dalObj.GetType().GetProperties())
            {
                object[] attrs = proInfo.GetCustomAttributes(typeof(DBFieldAttribute), true);
                if(attrs.Length == 1)
                {
                    DBFieldAttribute attr = (DBFieldAttribute)attrs[0];
                    sb.Append(attr.FiledName + ":" + (attr.DefaultValue == null ? "null" : attr.DefaultValue.ToString()) + ":" + attr.Usage + "\r\n" );
                }
            }
            Console.WriteLine(sb.ToString());
            Console.ReadKey();

输出

id:null:PrimaryKey
name:游客:None
pwd:null:Node

总结

有了这些基础的知识之后,在开发Unity插件的时候,总算迈出了第一步,下一步准备写一个自动生成静态字体的插件,并且将其放到自己定义的菜单栏中。

posted @ 2017-01-12 09:53  懒惰的人  阅读(222)  评论(0)    收藏  举报