{"id":1194,"date":"2014-05-22T05:21:14","date_gmt":"2014-05-22T05:21:14","guid":{"rendered":"http:\/\/www.clonefactor.com\/wordpress\/?p=1194"},"modified":"2014-05-22T09:36:49","modified_gmt":"2014-05-22T09:36:49","slug":"propertydrawer-%e7%9a%84%e4%bf%ae%e7%b7%b4","status":"publish","type":"post","link":"https:\/\/www.clonefactor.com\/wordpress\/program\/unity3d\/1194\/","title":{"rendered":"PropertyDrawer \u7684 ResourceFolder \u8a66\u4f5c"},"content":{"rendered":"<p>\u73a9\u591a\u4e86 Editor script, \u5c31\u5e0c\u671b tools \u5f04\u5f97\u66f4\u00a0flexible \u4e00\u9ede,<\/p>\n<p>\u628a\u6700\u5e38\u7528\u7684 Resource \u5f04\u4e00\u500b property drawer \u51fa\u4f86.<\/p>\n<p>\u76f4\u63a5\u628a string \u7528\u4f5c resourceFolder \u7684 path, \u542b checking<\/p>\n<p>\u5148\u6e96\u5099\u597d\u57fa\u672c\u7684 structure.<\/p>\n<pre class=\"brush:csharp\">namespace Kit.Resource\r\n{\r\n\t\/\/\/ &lt;summary&gt;Resource folder attribute.&lt;\/summary&gt;\r\n\t[Serializable] public class ResourceFolderAttribute : PropertyAttribute\r\n\t{\r\n\t\tpublic readonly string title;\r\n\t\tpublic readonly string defaultName;\r\n\t\tpublic readonly string helpMessage;\r\n\t \t\r\n\t\t\/\/\/ &lt;summary&gt;Initializes a new instance of the &lt;see cref=\"Kit.Resource.ResourceFolderAttribute\"\/&gt; class.&lt;\/summary&gt;\r\n\t\t\/\/\/ &lt;param name=\"title\"&gt;Title of popup screen.&lt;\/param&gt;\r\n\t\t\/\/\/ &lt;param name=\"defaultName\"&gt;Default name to search.&lt;\/param&gt;\r\n\t\t\/\/\/ &lt;param name=\"helpMessage\"&gt;Help message, when not matching.&lt;\/param&gt;\r\n\t\tpublic ResourceFolderAttribute (string title, string defaultName, string helpMessage)\r\n\t\t{\r\n\t\t\tthis.title = title;\r\n\t\t\tthis.defaultName = defaultName;\r\n\t\t\tthis.helpMessage = helpMessage;\r\n\t\t}\r\n\t\tpublic ResourceFolderAttribute()\r\n\t\t\t: this(\"Select Resource Folder\",\"\",\"Folder must put in \\\"Resources\\\" folder\")\r\n\t\t{}\r\n\t}\r\n}<\/pre>\n<p>\u9700\u8981\u7559\u610f\u7684\u662f class \u7684\u547d\u540d\u65b9\u5f0f, ResourceFolder<span style=\"color: #ff0000;\">Attribute<\/span> \u6709 Attribute \u5b57\u6a23\u65b9\u4fbf\u65e5\u5f8c\u8abf\u7528.<\/p>\n<p>\u4e26\u4e14 extend PropertyAttribute class. \u518d\u5f04\u4e00\u500b PropertyDrawer \u7d66\u9019\u500b class.<\/p>\n<pre class=\"brush:csharp\">[CustomPropertyDrawer(typeof(ResourceFolderAttribute))]\r\npublic class ResourceDrawer : PropertyDrawer\r\n{\r\n\tconst string RESOURCE_FOLDER = \"resources\/\";\r\n\t\r\n\tconst float BUTTON_HEIGHT = 16f;\r\n\tconst float HELP_HEIGHT = 30f;\r\n\t\r\n\t\/\/ Provide easy access to the RegexAttribute for reading information from it.\r\n\t\/\/ ResourceFolderAttribute resourceFolder { get { return ((ResourceFolderAttribute)attribute); } }\r\n\tResourceFolderAttribute resourceFolder { get { return (ResourceFolderAttribute)attribute; } }\r\n\r\n\tpublic override void OnGUI (UnityEngine.Rect position, SerializedProperty property, UnityEngine.GUIContent label)\r\n\t{\r\n\t\tRect _buttonPosition = EditorGUI.PrefixLabel(position, label);\r\n\t\t_buttonPosition.height = BUTTON_HEIGHT;\r\n\t\tstring _btn = \"Select Folder\";\r\n\t\tif( IsResourseFolder(property) )\r\n\t\t\t_btn = \"Resources:\/ \"+ property.stringValue;\r\n\t\t\r\n\t\tif ( GUI.Button(_buttonPosition,_btn) )\r\n\t\t{\r\n\t\t\tstring _string = EditorUtility.OpenFolderPanel(resourceFolder.title, \"\", resourceFolder.defaultName);\r\n\t\t\tif( !string.IsNullOrEmpty(_string) )\r\n\t\t\t{\r\n\t\t\t\tbool _check = _string.ToLower().IndexOf(RESOURCE_FOLDER) &gt; 0 &amp;&amp; !_string.ToLower().EndsWith(RESOURCE_FOLDER);\r\n\t\t\t\tif( _check )\r\n\t\t\t\t{\r\n\t\t\t\t\tproperty.stringValue = GetShortPath(_string);\r\n\t\t\t\t}\r\n\t\t\t\telse\r\n\t\t\t\t{\r\n\t\t\t\t\tproperty.stringValue = string.Empty;\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t}\r\n\t\t\r\n\t\tRect _helpPosition = EditorGUI.IndentedRect(position);\r\n\t\t_helpPosition.y += _buttonPosition.height;\r\n\t\t_helpPosition.height = 30f;\r\n\t\tif ( !IsResourseFolder(property) )\r\n\t\t{\r\n\t\t\tEditorGUI.HelpBox(_helpPosition, resourceFolder.helpMessage, MessageType.Warning);\r\n\t\t}\r\n\t}\r\n\t\r\n\tpublic override float GetPropertyHeight (SerializedProperty property, GUIContent label)\r\n\t{\r\n\t\tif( IsResourseFolder(property) )\r\n\t\t\treturn base.GetPropertyHeight(property,label);\r\n\t\telse\r\n\t\t\treturn base.GetPropertyHeight(property,label) + HELP_HEIGHT;\r\n\t}\r\n\t\r\n\tbool IsResourseFolder(SerializedProperty _prop)\r\n\t{\r\n\t\treturn !string.IsNullOrEmpty(_prop.stringValue);\r\n\t}\r\n\t\r\n\tstring GetShortPath(string fullPath)\r\n\t{\r\n\t\treturn fullPath.Substring( fullPath.ToLower().IndexOf(RESOURCE_FOLDER)+ RESOURCE_FOLDER.Length );\r\n\t}\r\n}<\/pre>\n<p>\u7531\u65bc Resources asset \u9700\u653e\u9032\u540d\u70ba\u00a0\\Resources \u7684 folder \u4e2d, \u7528\u4e4b\u4f5c\u6aa2\u67e5\u70ba\u689d\u4ef6.<\/p>\n<p>\u4e26\u66f4\u65b0\u539f\u672c\u7684 \u00a0property.stringValue \u70ba\u8655\u7406\u5f8c\u7684 short path.<\/p>\n<p>\u8abf\u7528\u65b9\u6cd5\u4e5f\u5f88\u7c21\u55ae.<\/p>\n<pre class=\"brush:csharp\">[ResourceFolderAttribute]\r\npublic string myFolder;\r\n\r\n[ResourceFolder \"Select XYZ Folder\",\"\",\"Folder must put in \\\"Resources\\\" folder\"]\r\npublic string myFolder2;<\/pre>\n<p>\u7531\u65bc Attribute \u662f\u53ef\u4ee5\u5ffd\u7565\u7684\u5b57\u4e32, \u539f\u672c\u7684 [ResourceFolderAttribute] \u4e5f\u53ef\u4ee5\u5beb\u70ba [ResourceFolder]<\/p>\n<p>\u5c3e\u96a8\u7684 string \u662f constructer \u7684 overload method, \u9700\u57fa\u65bc\u539f\u672c\u7684 structure \u4f86\u4f5c\u6b63\u78ba\u5beb\u5165.<\/p>\n<p>&nbsp;<\/p>\n<p>Ref :<\/p>\n<ul>\n<li><a href=\"https:\/\/docs.unity3d.com\/Documentation\/ScriptReference\/PropertyDrawer.html\">https:\/\/docs.unity3d.com\/Documentation\/ScriptReference\/PropertyDrawer.html<\/a><\/li>\n<li><a href=\"http:\/\/blogs.unity3d.com\/2012\/09\/07\/property-drawers-in-unity-4\/\">http:\/\/blogs.unity3d.com\/2012\/09\/07\/property-drawers-in-unity-4\/<\/a><\/li>\n<li><a href=\"http:\/\/catlikecoding.com\/unity\/tutorials\/editor\/custom-data\/\">http:\/\/catlikecoding.com\/unity\/tutorials\/editor\/custom-data\/<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u73a9\u591a\u4e86 Editor script, \u5c31\u5e0c\u671b tools \u5f04\u5f97\u66f4\u00a0flexible \u4e00\u9ede, \u628a\u6700\u5e38\u7528 &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[16,43],"class_list":["post-1194","post","type-post","status-publish","format-standard","hentry","category-unity3d","tag-c-2","tag-unity3d-2"],"_links":{"self":[{"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/posts\/1194","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/comments?post=1194"}],"version-history":[{"count":0,"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/posts\/1194\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/media?parent=1194"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/categories?post=1194"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/tags?post=1194"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}