{"id":655,"date":"2022-06-15T16:08:20","date_gmt":"2022-06-15T08:08:20","guid":{"rendered":"https:\/\/www.acgloby.com\/?p=655"},"modified":"2022-06-15T16:08:20","modified_gmt":"2022-06-15T08:08:20","slug":"unity-%e7%bc%96%e8%be%91%e5%99%a8-%e5%86%85%e7%bd%aeguistyle%e6%9f%a5%e7%9c%8b%e5%b7%a5%e5%85%b7","status":"publish","type":"post","link":"https:\/\/www.acgloby.com\/index.php\/2022\/06\/15\/unity-%e7%bc%96%e8%be%91%e5%99%a8-%e5%86%85%e7%bd%aeguistyle%e6%9f%a5%e7%9c%8b%e5%b7%a5%e5%85%b7\/","title":{"rendered":"Unity \u7f16\u8f91\u5668-\u5185\u7f6eGUIStyle\u67e5\u770b\u5de5\u5177"},"content":{"rendered":"\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/www.acgloby.com\/wp-content\/uploads\/2022\/06\/image-1.png\" alt=\"Unity \u7f16\u8f91\u5668-\u5185\u7f6eGUIStyle\u67e5\u770b\u5de5\u5177\" title=\"Unity \u7f16\u8f91\u5668-\u5185\u7f6eGUIStyle\u67e5\u770b\u5de5\u5177\"><img decoding=\"async\" src=\"https:\/\/www.acgloby.com\/wp-content\/uploads\/2022\/06\/image-1-1024x555.png\" alt=\"\" class=\"wp-image-657\"\/><\/a><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code>using UnityEngine;\nusing UnityEditor;\nusing System.Reflection;\n\n\/\/\/ &lt;summary&gt;\n\/\/\/ Unity \u81ea\u5e26\u7684 GUIStyle \u6548\u679c\u67e5\u770b\u5668\n\/\/\/ &lt;\/summary&gt;\npublic class GUIStyleViewerUtil : EditorWindow\n{\n    \/\/\/ &lt;summary&gt;\n    \/\/\/ \u6ed1\u52a8\u6761\n    \/\/\/ &lt;\/summary&gt;\n    private Vector2 mScrollView = Vector2.zero;\n    \/\/\/ &lt;summary&gt;\n    \/\/\/ \u5bfb\u627e\u7684\u6837\u5f0f\u540d\u79f0\n    \/\/\/ &lt;\/summary&gt;\n    private string mSearchStyle = \"\";\n    \/\/\/ &lt;summary&gt;\n    \/\/\/ \u6837\u5f0f\u7f13\u5b58\n    \/\/\/ &lt;\/summary&gt;\n    private static GUIStyle mBtnStyle;\n\n    &#91;MenuItem(\"Util\/GUIStyle \u6548\u679c\u67e5\u770b\u5668\")]\n    private static void MenuClicked()\n    {\n        GetWindow&lt;GUIStyleViewerUtil&gt;(\"GUIStyle\u67e5\u770b\u5668\", true);\n        \/\/ \u521d\u59cb\u5316\u4e00\u4e2a\u6309\u94ae\u7684\u6837\u5f0f\n        mBtnStyle = GetBtnStyle();\n    }\n\n    private void OnGUI()\n    {\n        GUILayout.Space(20);\n\n        \/\/ \u8bbe\u7f6e\u4e00\u4e2a\u67e5\u627e\u7684\u529f\u80fd\n        GUILayout.BeginHorizontal(\"HelpBox\");\n        GUILayout.Space(20);\n        mSearchStyle = EditorGUILayout.TextField(\"\", mSearchStyle, \"SearchTextField\");\n        GUILayout.Label(\"\", \"SearchCancelButtonEmpty\");\n        GUILayout.EndHorizontal();\n\n        \/\/ \u8bbe\u7f6e\u6eda\u52a8\u680f\n        mScrollView = GUILayout.BeginScrollView(mScrollView);\n        \/\/ \u904d\u5386\u5e76\u663e\u793a\u6240\u6709\u7b26\u5408\u6761\u4ef6\u7684 Unity \u81ea\u5e26\u7684 GUIStyle\n        foreach (GUIStyle style in GUI.skin.customStyles)\n        {\n            if (style.name.ToLower().Contains(mSearchStyle.ToLower()))\n            {\n                GUILayout.Space(10);\n                \/\/ \u4e3a\u4e86\u8ba9\u663e\u793a\u7684\u5e03\u5c40\u770b\u7740\u66f4\u8212\u670d\u4e00\u4e9b\uff0c\u9700\u8981\u4ee5\u5f53\u524d style \u4e3a\u539f\u578b\u65b0\u5efa\u4e00\u4e2a GUIStyle\n                \/\/ \u56e0\u4e3a\u63a5\u4e0b\u6765\u53ef\u80fd\u4f1a\u5bf9\u6587\u5b57\u5c45\u4e2d\u7b49\u65b9\u9762\u8fdb\u884c\u8c03\u6574\uff0c\n                \/\/ \u800c\u5982\u679c\u76f4\u63a5\u66f4\u6539\u539f\u6765\u7684 style\uff0c\u5219\u4f1a\u5bfc\u81f4\u7cfb\u7edf\u4e2d\u7684\u5bf9\u5e94 UI \u6837\u5f0f\u548c\u5e03\u5c40\u4e5f\u53d1\u751f\u6539\u53d8\n                GUIStyle tmp = new GUIStyle(style);\n                DrawStyleItem(tmp);\n            }\n        }\n        GUILayout.EndScrollView();\n    }\n\n    \/\/\/ &lt;summary&gt;\n    \/\/\/ \u7ed8\u5236\u5bf9\u5e94\u7684 GUIStyle\n    \/\/\/ &lt;\/summary&gt;\n    \/\/\/ &lt;param name=\"style\"&gt;&lt;\/param&gt;\n    private void DrawStyleItem(GUIStyle style)\n    {\n\n        GUILayout.BeginHorizontal(\"box\", GUILayout.Height(style.fixedHeight));\n        GUILayout.Space(20);\n        \/\/ \u83b7\u53d6\u6309\u94ae\u6837\u5f0f\n        GUIStyle tmp = mBtnStyle;\n\n        \/\/ \u8bbe\u7f6e\u7b2c\u4e00\u4e2a\u6309\u94ae\uff0c\u70b9\u51fb\u540e\u53ef\u4ee5\u590d\u5236\u5f53\u524d GUIStyle \u7684\u540d\u5b57\u4fe1\u606f\n        if (GUILayout.Button(\"&lt;color=#000000FF&gt;\u590d\u5236\u5230\u526a\u8d34\u677f&lt;\/color&gt;\", tmp))\n        {\n            GUIUtility.systemCopyBuffer = style.name;\n        }\n\n        GUILayout.Space(20);\n\n        \/\/ \u8bbe\u7f6e\u7b2c\u4e8c\u4e2a\u6309\u94ae\uff0c\u70b9\u51fb\u540e\u5728\u63a7\u5236\u53f0\u8f93\u51fa\u8be5 GUIStyle \u7684\u8bbe\u7f6e\u53c2\u6570\n        if (GUILayout.Button(\"&lt;color=#FFFFFFFF&gt;\u67e5\u770bStyle\u53c2\u6570&lt;\/color&gt;\", tmp))\n        {\n            ShowGUIStyleMsg(style.name);\n        }\n        GUILayout.Space(30);\n\n        \/\/ \u663e\u793a\u5f53\u524d GUIStyle \u7684\u540d\u5b57\n        EditorGUILayout.SelectableLabel(style.name);\n\n        \/\/ \u8bbe\u7f6e\u5de6\u53f3\u5bf9\u79f0\u5e03\u5c40\uff0c\u4ee5\u6b64\u4e3a\u5206\u754c\u7ebf\uff0c\u628a\u524d\u540e\u7684\u5e03\u5c40\u5bf9\u79f0\n        GUILayout.FlexibleSpace();\n\n        \/\/ \u8bbe\u7f6e\u6587\u672c\u5c45\u4e2d\u663e\u793a\n        style.alignment = TextAnchor.MiddleCenter;\n        \/\/ \u663e\u793a\u5f53\u524d\u7684\u6837\u5f0f\u53ca\u5176\u540d\u79f0\n        EditorGUILayout.SelectableLabel(style.name, style);\n\n        GUILayout.Space(100);\n\n        \/\/ \u5355\u72ec\u663e\u793a\u6837\u5f0f\n        EditorGUILayout.SelectableLabel(\"\", style,\n                                        GUILayout.Height(style.fixedHeight),\n                                         GUILayout.Width(style.fixedWidth));\n        GUILayout.Space(100);\n\n        GUILayout.EndHorizontal();\n    }\n\n    \/\/\/ &lt;summary&gt;\n    \/\/\/ \u521b\u5efa\u4e00\u4e2a\u6309\u94ae\u6837\u5f0f\uff0c\u7528\u6765\u4f5c\u4e3a\u672c\u754c\u9762\u4e2d\u7684\u6309\u94ae\u57fa\u7840\n    \/\/\/ &lt;\/summary&gt;\n    \/\/\/ &lt;returns&gt;&lt;\/returns&gt;\n    public static GUIStyle GetBtnStyle()\n    {\n        \/\/ \u4f7f\u7528\u7cfb\u7edf\u81ea\u5e26\u7684 \"AC Button\" \u6837\u5f0f\u4f5c\u4e3a\u539f\u578b\uff0c\u56e0\u4e3a \"AC Button\" \u672c\u8eab\u5c31\u5177\u6709\u70b9\u51fb\u6548\u679c\u4e86\n        GUIStyle style = new GUIStyle(\"AC Button\");\n\n        \/\/ \u5c06\u6837\u5f0f\u7684\u70b9\u51fb\u65f6\u7684\u6548\u679c\u56fe\uff0c\u66ff\u6362\u4e3a\u7cfb\u7edf\u81ea\u5e26\u7684 \"flow node 4 on\" \u6837\u5f0f\n        GUIStyle active = new GUIStyle(\"flow node 4 on\");\n        style.active.background = active.normal.background;\n\n        \/\/ \u5c06\u5e73\u65f6\u6309\u94ae\u7684\u6548\u679c\u56fe\uff0c\u66ff\u6362\u4e3a\u7cfb\u7edf\u81ea\u5e26\u7684 \"flow node 4\" \u6837\u5f0f\n        GUIStyle normal = new GUIStyle(\"flow node 4\");\n        style.normal.background = normal.normal.background;\n\n        \/\/ \u53c2\u8003 \"flow node 4\" \u7684\u5185\u90e8\u53c2\u6570\uff0c\u8bbe\u7f6e\u4e00\u4e9b\u56fe\u7247\u7684\u53c2\u6570\u3002\n        \/\/ \u5982\u679c\u4e0d\u8bbe\u7f6e\u7684\u8bdd\uff0c\u5219\u56fe\u7247\u4f1a\u6309\u7167\u539f \"AC Button\" \u7684\u6837\u5f0f\u8fdb\u884c\u663e\u793a\n        \/\/ \u56e0\u4e3a\u56fe\u7247\u4e0d\u4e00\u6837\uff0c\u6240\u4ee5\u4f1a\u6709\u663e\u793a\u4e0a\u7684\u95ee\u9898\u3002\n        style.stretchHeight = true;\n        style.stretchWidth = true;\n        style.border = new RectOffset(11, 11, 11, 15);\n        style.margin = new RectOffset(0, 0, 0, 0);\n        style.padding = new RectOffset(0, 0, 0, 0);\n        style.overflow = new RectOffset(7, 7, 6, 9);\n\n        \/\/ \u8fdb\u884c\u6587\u672c\u65b9\u9762\u7684\u8bbe\u7f6e\n        style.alignment = TextAnchor.MiddleCenter;\n        style.fontSize = 18;\n        style.richText = true;\n\n        return style;\n    }\n\n    \/\/\/ &lt;summary&gt;\n    \/\/\/ \u5c55\u793a\u6307\u5b9a\u7684 GUI \u6837\u5f0f\u7684\u4e00\u4e9b\u6570\u503c\n    \/\/\/ &lt;\/summary&gt;\n    \/\/\/ &lt;param name=\"style\"&gt;&lt;\/param&gt;\n    public void ShowGUIStyleMsg(string style)\n    {\n        \/\/ \u6e05\u7a7a\u63a7\u5236\u53f0\u4fe1\u606f\uff0c\u9632\u6b62\u6df7\u6dc6\n        ClearConsole();\n\n        GUIStyle tmp = new GUIStyle(style);\n        Debug.Log(\"Style Name: \" + tmp.name);\n        Debug.Log(\"Style Normal: \" + tmp.normal.background);\n        Debug.Log(\"Style Normal Color: \" + tmp.normal.textColor);\n        Debug.Log(\"Style Active: \" + tmp.active.background);\n        Debug.Log(\"Style Active Color: \" + tmp.active.textColor);\n        Debug.Log(\"Style Hover: \" + tmp.hover.background);\n        Debug.Log(\"Style Hover Color: \" + tmp.hover.textColor);\n        Debug.Log(\"Style Focused: \" + tmp.focused.background);\n        Debug.Log(\"Style Focused Color: \" + tmp.focused.textColor);\n        Debug.Log(\"Style border: \" + tmp.border);\n        Debug.Log(\"Style margin: \" + tmp.margin);\n        Debug.Log(\"Style padding: \" + tmp.padding);\n        Debug.Log(\"Style overflow: \" + tmp.overflow);\n        Debug.Log(\"Style fixedWidth: \" + tmp.fixedWidth);\n        Debug.Log(\"Style fixedHeight: \" + tmp.fixedHeight);\n        Debug.Log(\"Style stretchWidth: \" + tmp.stretchWidth);\n        Debug.Log(\"Style stretchHeight: \" + tmp.stretchHeight);\n        Debug.Log(\"Style lineHeight: \" + tmp.lineHeight);\n        Debug.Log(\"Style Font: \" + tmp.font);\n        Debug.Log(\"Style Font Size: \" + tmp.fontSize);\n    }\n\n    public void ClearConsole()\n    {\n        Assembly assembly = Assembly.GetAssembly(typeof(SceneView));\n\n        System.Type type = assembly.GetType(\"UnityEditor.LogEntries\");\n        MethodInfo method = type.GetMethod(\"Clear\");\n        method.Invoke(new object(), null);\n\n    }\n}\n\n\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":657,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"emotion":"","emotion_color":"","title_style":"","license":"","footnotes":""},"categories":[2],"tags":[],"class_list":["post-655","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-unity"],"_links":{"self":[{"href":"https:\/\/www.acgloby.com\/index.php\/wp-json\/wp\/v2\/posts\/655","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.acgloby.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.acgloby.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.acgloby.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.acgloby.com\/index.php\/wp-json\/wp\/v2\/comments?post=655"}],"version-history":[{"count":0,"href":"https:\/\/www.acgloby.com\/index.php\/wp-json\/wp\/v2\/posts\/655\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.acgloby.com\/index.php\/wp-json\/wp\/v2\/media?parent=655"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.acgloby.com\/index.php\/wp-json\/wp\/v2\/categories?post=655"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.acgloby.com\/index.php\/wp-json\/wp\/v2\/tags?post=655"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}