{"id":1791,"date":"2020-04-18T08:48:04","date_gmt":"2020-04-18T00:48:04","guid":{"rendered":"http:\/\/blog.coolcoding.cn\/?p=1791"},"modified":"2020-04-18T08:48:04","modified_gmt":"2020-04-18T00:48:04","slug":"unity%e5%9c%a8%e7%bc%96%e8%be%91%e5%99%a8%e4%b8%ad%e5%ae%9e%e7%8e%b0%e4%bd%8d%e6%93%8d%e4%bd%9cbit%e7%9a%84%e7%bc%96%e8%be%91","status":"publish","type":"post","link":"https:\/\/blog.coolcoding.cn\/?p=1791","title":{"rendered":"[Unity]\u5728\u7f16\u8f91\u5668\u4e2d\u5b9e\u73b0\u4f4d\u64cd\u4f5c(Bit)\u7684\u7f16\u8f91"},"content":{"rendered":"\n<p>\u6709\u4e9b\u65f6\u5019\uff0c\u60f3\u9ad8\u6548\u5229\u7528\u6570\u636e\uff0c\u628a\u4e00\u4e2aint\uff0832\u4f4d\uff09\u63b0\u621032\u4e2abool\u503c\u3002\u90a3\u4e48\u7f16\u8f91\u5668\u4e2d\u6709\u6ca1\u6709\u4e00\u4e2a\u8fd9\u6837\u7684\u63d2\u4ef6\u652f\u6301\u5462\uff1f<\/p>\n\n\n\n<p>\u4ee5\u4e0b\u662f\u4ee3\u7801<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>using UnityEngine;\nusing UnityEditor;\nusing System;\nusing System.Collections;\nusing System.Reflection;\n \npublic enum MyEnum\n{\n\tEnum1 = 1,\n\tEnum2 = 2,\n}\n \npublic class MyEnumFieldName\n{\n\tpublic const string Enum1 = @\"\u7b2c\u4e00\u4f4d\";\n\tpublic const string Enum2 = @\"\u7b2c\u4e00\u4f4d\";\n}\n \npublic class BitMaskAttribute : PropertyAttribute\n{\n\tpublic BitMaskAttribute()\n\t{\n\t}\n}\n \n[CustomPropertyDrawer(typeof(BitMaskAttribute))]\npublic class BitMaskDrawer : PropertyDrawer\n{\n    private float extraHeight = 20.0f;\n    private static bool showValues = true;\n    private bool hasError = false;\n \n    public string GetFieldName(string enumName)\n    {\n        string className = string.Format(\"{0}FieldName\", this.fieldInfo.FieldType.Name);\n        Type classType = Type.GetType(className);\n        if (classType != null)\n        {\n            FieldInfo fi = classType.GetField(enumName);\n            if (fi != null)\n            {\n                return fi.GetValue(null).ToString();\n            }\n        }\n \n        return enumName;\n    }\n \n    public override void OnGUI(Rect position, SerializedProperty prop, GUIContent label)\n    {\n        if (CheckInvalid(position, prop, label))\n        {\n            return;\n        }\n \n        showValues = EditorGUI.Foldout(new Rect(position.x, position.y, position.width, extraHeight), showValues, label);\n \n        if (!showValues)\n        {\n            return;\n        }\n \n        EditorGUI.indentLevel++;\n \n        for (int i = 0; i &lt; prop.enumNames.Length; i++)\n        {\n            bool toggleValue = (prop.intValue &amp; (1 &lt;&lt; i)) == (1 &lt;&lt; i);\n \n            Rect rect = new Rect(position.x, position.y + ((i + 1) * extraHeight), position.width, extraHeight);\n            string enumTitle = GetFieldName(prop.enumNames [i]);\n            if (EditorGUI.Toggle(rect, enumTitle, toggleValue))\n            {\n                prop.intValue |= 1 &lt;&lt; i;\n            } else\n            {\n                prop.intValue &amp;= ~(1 &lt;&lt; i);\n            }\n        }\n    }\n \n    private bool CheckInvalid(Rect position, SerializedProperty prop, GUIContent label)\n    {\n        if (prop.propertyType != SerializedPropertyType.Enum)\n        {\n            showValues = false;\n            hasError = true;\n            return true;\n        }\n \n        return false;\n    }\n \n    public override float GetPropertyHeight(SerializedProperty prop, GUIContent label)\n    {\n        if (!showValues)\n        {\n            if (!hasError)\n            {\n                return base.GetPropertyHeight(prop, label);\n            } else\n            {\n                return base.GetPropertyHeight(prop, label) + extraHeight;\n            }\n        }\n        return base.GetPropertyHeight(prop, label) * (prop.enumNames.Length + 3);\n    }\n}<\/code><\/pre>\n\n\n\n<p>\n\n\u7528\u7684\u65f6\u5019\u5f88\u7b80\u5355\uff0c\u53ea\u9700\u8981\u4e00\u53e5\uff1a<\/p>\n\n\n\n<p>[BitMask]&nbsp;public MyEnum enumData;<\/p>\n\n\n\n<p>\u5728\u7f16\u8f91\u5668\u770b\u6548\u679c\u5427<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6709\u4e9b\u65f6\u5019\uff0c\u60f3\u9ad8\u6548\u5229\u7528\u6570\u636e\uff0c\u628a\u4e00\u4e2aint\uff0832\u4f4d\uff09\u63b0\u621032\u4e2abool\u503c\u3002\u90a3\u4e48\u7f16\u8f91\u5668\u4e2d\u6709\u6ca1\u6709\u4e00\u4e2a\u8fd9\u6837\u7684\u63d2\u4ef6\u652f\u6301\u5462\uff1f [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[],"_links":{"self":[{"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/posts\/1791"}],"collection":[{"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1791"}],"version-history":[{"count":1,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/posts\/1791\/revisions"}],"predecessor-version":[{"id":1792,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/posts\/1791\/revisions\/1792"}],"wp:attachment":[{"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1791"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1791"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1791"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}