{"id":1283,"date":"2017-09-08T20:17:49","date_gmt":"2017-09-08T12:17:49","guid":{"rendered":"http:\/\/39.100.100.179\/?p=1283"},"modified":"2017-09-08T20:18:14","modified_gmt":"2017-09-08T12:18:14","slug":"%e4%bd%bf%e7%94%a8%e7%bc%96%e8%be%91%e5%99%a8%e6%9d%a5%e6%89%b9%e9%87%8f%e4%bf%ae%e6%94%b9ugui%e5%b0%84%e7%ba%bf","status":"publish","type":"post","link":"http:\/\/www.u3d8.com\/?p=1283","title":{"rendered":"\u4f7f\u7528\u7f16\u8f91\u5668\u6279\u91cf\u4fee\u6539UGUI\u5c04\u7ebf"},"content":{"rendered":"<p>\u5982\u679c\u9879\u76eeUI\u7ba1\u7406\u4e0d\u597d\uff0c\u524d\u671f\u4f1a\u51fa\u73b0\u5f88\u591a\u4e0d\u9700\u8981\u70b9\u51fb\u4e8b\u4ef6\uff0c\u4f46\u53c8\u6253\u5f00\u7740UI\u7684Raycast Target\u5c5e\u6027\uff0c\u8fd9\u4e2a\u662f\u5f88\u8017\u6027\u80fd\u7684\u3002<\/p>\n<p>\u7136\u800c \u9879\u76ee\u8fdb\u884c\u5230\u4e00\u5b9a\u65f6\u95f4\u518d\u53bb\u7ba1\u7406UI\u5c04\u7ebf\uff0c\u663e\u7136\u662f\u5f88\u9ebb\u70e6\u8017\u65f6\u7684\u3002<\/p>\n<p>\u4eca\u5929\u7ed9\u5927\u5bb6\u901a\u8fc7Unity\u7f16\u8f91\u5668 \u6765\u5b9e\u73b0\u6279\u91cf\u4fee\u6539UI\u5c04\u7ebf\u5c5e\u6027\u7684\u5de5\u5177\u3002<\/p>\n<p>\u5de5\u5177\u56fe\uff1a<\/p>\n<p><a href=\"http:\/\/www.u3d8.com\/wp-content\/uploads\/2017\/09\/QQ\u622a\u56fe20170908200921.png\" data-lightbox=\"image_lg\"><img title=\"\u4f7f\u7528\u7f16\u8f91\u5668\u6279\u91cf\u4fee\u6539UGUI\u5c04\u7ebf - \u7b2c1\u5f20  | u3d8\u6280\u672f\u5206\u4eab\" alt=\"\u4f7f\u7528\u7f16\u8f91\u5668\u6279\u91cf\u4fee\u6539UGUI\u5c04\u7ebf - \u7b2c1\u5f20  | u3d8\u6280\u672f\u5206\u4eab\"  loading=\"lazy\" class=\"aligncenter size-full wp-image-1286\"  data-src=\"http:\/\/www.u3d8.com\/wp-content\/uploads\/2017\/09\/QQ\u622a\u56fe20170908200921.png\" width=\"517\" height=\"683\" srcset=\"http:\/\/www.u3d8.com\/wp-content\/uploads\/2017\/09\/QQ\u622a\u56fe20170908200921.png 517w, http:\/\/www.u3d8.com\/wp-content\/uploads\/2017\/09\/QQ\u622a\u56fe20170908200921-227x300.png 227w\" sizes=\"(max-width: 517px) 100vw, 517px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p><a href=\"http:\/\/www.u3d8.com\/wp-content\/uploads\/2017\/09\/QQ\u622a\u56fe20170908194104.png\" data-lightbox=\"image_lg\"><img title=\"\u4f7f\u7528\u7f16\u8f91\u5668\u6279\u91cf\u4fee\u6539UGUI\u5c04\u7ebf - \u7b2c2\u5f20  | u3d8\u6280\u672f\u5206\u4eab\" alt=\"\u4f7f\u7528\u7f16\u8f91\u5668\u6279\u91cf\u4fee\u6539UGUI\u5c04\u7ebf - \u7b2c2\u5f20  | u3d8\u6280\u672f\u5206\u4eab\"  loading=\"lazy\" class=\"aligncenter size-full wp-image-1284\"  data-src=\"http:\/\/www.u3d8.com\/wp-content\/uploads\/2017\/09\/QQ\u622a\u56fe20170908194104.png\" width=\"411\" height=\"137\" srcset=\"http:\/\/www.u3d8.com\/wp-content\/uploads\/2017\/09\/QQ\u622a\u56fe20170908194104.png 411w, http:\/\/www.u3d8.com\/wp-content\/uploads\/2017\/09\/QQ\u622a\u56fe20170908194104-300x100.png 300w\" sizes=\"(max-width: 411px) 100vw, 411px\" \/><\/a><\/p>\n<p>\u4f7f\u7528\u65b9\u6cd5\uff1a<\/p>\n<p>\u4e00\u3001\u5c06\u4e0b\u9762\u811a\u672cCopy\u5230\u9879\u76ee\u91cc\uff0c\u7136\u540e\u70b9\u51fb\u201cTools\/\u68c0\u67e5UGUI\u5c04\u7ebf\u201d\u6309\u94ae<\/p>\n<p>\u4e8c\u3001\u5728\u201cHierarchy\u201d\u6216\u201cProject\u201d\u9762\u677f\u9009\u62e9\u60f3\u8981\u4fee\u6539\u5c04\u7ebf\u7684\u9884\u8bbe\u4f53\uff08\u53ef\u4ee5\u591a\u9009\uff09<\/p>\n<p>\u4e09\u3001\u70b9\u51fb\u201c\u68c0\u67e5\u9009\u4e2dUI\u201d\u6309\u94ae\uff0c\u7136\u540e\u6240\u6709\u5e26\u5c04\u7ebf\u7684UI\u90fd\u4f1a\u663e\u793a\u5728\u4e0b\u9762<\/p>\n<p>\u56db\u3001\u53ef\u4ee5\u70b9\u51fb\u6bcf\u4e2aUI\u524d\u65b9\u7684\u5355\u9009\u6846\u6309\u94ae\u6765\u8bbe\u7f6eUI\u5c04\u7ebf\u72b6\u6001\u3001\u4e5f\u53ef\u4ee5\u70b9\u51fb\u201c\u79fb\u9664\u5168\u90e8\u5c04\u7ebf\u201d\u6309\u94ae\u6765\u79fb\u9664\u5f53\u524d\u663e\u793a\u7684\u6240\u6709\u5c04\u7ebf<\/p>\n<p>\u4e94\u3001\u6539\u540e\u53ef\u4ee5\u67e5\u770b\u4e4b\u524d\u9009\u62e9\u7684\u9884\u8bbe\u4f53\uff0c\u770b\u770b\u662f\u4e0d\u662f\u6539\u597d\u4e86\u5462~~<\/p>\n<p>\u4e0a\u811a\u672c\uff1a<\/p>\n<pre class=\"lang:c# decode:true \">using UnityEngine;\r\nusing System.Collections;\r\nusing UnityEditor;\r\nusing UnityEngine.UI;\r\nusing System.Collections.Generic;\r\n\r\npublic class CheckRaycastUtil : EditorWindow {\r\n\r\n    private List&lt;Image&gt; imageList;\r\n    private List&lt;RawImage&gt; rawimageList;\r\n    private List&lt;Text&gt; textList;\r\n\r\n    private bool includeInactive = true;\r\n    private Transform[] selectTrans;\r\n    private Vector2 scrollPosition;\r\n    static CheckRaycastUtil window;\r\n\r\n    [MenuItem(\"Tools\/\u68c0\u67e5UGUI\u5c04\u7ebf\")]\r\n    static void ModifyAtlas()\r\n    {\r\n        window = (CheckRaycastUtil)EditorWindow.GetWindow(typeof(CheckRaycastUtil), false, \"\u68c0\u67e5UGUI\u5c04\u7ebf\");\r\n        window.Show();\r\n    }\r\n\r\n    private void OnEnable()\r\n    {\r\n        imageList = new List&lt;Image&gt;();\r\n        rawimageList = new List&lt;RawImage&gt;();\r\n        textList = new List&lt;Text&gt;();\r\n    }\r\n\r\n    private void OnGUI()\r\n    {\r\n        EditorGUILayout.Space();\r\n\r\n        includeInactive = EditorGUILayout.Toggle(\"\u5305\u542b\u9690\u85cf\u7269\u4f53\uff1a\", includeInactive);\r\n\r\n        EditorGUILayout.Space();\r\n\r\n        if (GUILayout.Button(\"\u68c0\u67e5\u9009\u4e2dUI\"))\r\n        {\r\n            CheckSelected();\r\n        }\r\n\r\n        EditorGUILayout.Space();\r\n\r\n        if (GUILayout.Button(\"\u79fb\u9664\u5168\u90e8\u5c04\u7ebf\"))\r\n        {\r\n            RemoveRaycast();\r\n        }\r\n        \r\n        \/\/ \u663e\u793a\u9009\u4e2d\u7684\uff08\u542b\u5b50\u8282\u70b9\uff09\u7684\u6240\u6709Image\\RawImage\\Text\r\n        int allCount = imageList.Count + rawimageList.Count + textList.Count;\r\n\r\n        if (allCount &gt; 0)\r\n        {\r\n            int index = 0;\r\n            scrollPosition = GUI.BeginScrollView(new Rect(0, 120, Screen.width, 500), scrollPosition, new Rect(0, 120, Screen.width, allCount * 20));\r\n            for (int i = 0; i &lt; imageList.Count; i++)\r\n            {\r\n                index++;\r\n                imageList[i] = EditorGUI.ObjectField(new Rect(30, 120 + index * 20, Screen.width, 20), imageList[i].name, imageList[i], typeof(Image)) as Image;\r\n                imageList[i].raycastTarget = EditorGUI.Toggle(new Rect(5, 120 + index * 20, 20, 20), imageList[i].raycastTarget);\r\n            }\r\n            for (int i = 0; i &lt; rawimageList.Count; i++)\r\n            {\r\n                index++;\r\n                rawimageList[i] = EditorGUI.ObjectField(new Rect(30, 120 + index * 20, Screen.width, 20), rawimageList[i].name, rawimageList[i], typeof(RawImage)) as RawImage;\r\n                rawimageList[i].raycastTarget = EditorGUI.Toggle(new Rect(5, 120 + index * 20, 20, 20), rawimageList[i].raycastTarget);\r\n            }\r\n            for (int i = 0; i &lt; textList.Count; i++)\r\n            {\r\n                index++;\r\n                textList[i] = EditorGUI.ObjectField(new Rect(30, 120 + index * 20, Screen.width, 20), textList[i].name, textList[i], typeof(Text)) as Text;\r\n                textList[i].raycastTarget = EditorGUI.Toggle(new Rect(5, 120 + index * 20, 20, 20), textList[i].raycastTarget);\r\n            }\r\n\r\n            GUI.EndScrollView();\r\n        }\r\n    }\r\n\r\n    \/\/\/ &lt;summary&gt;\r\n    \/\/\/ \u68c0\u67e5\u5df2\u9009\u4e2d\u7684\u9879\r\n    \/\/\/ &lt;\/summary&gt;\r\n    void CheckSelected()\r\n    {\r\n        imageList = new List&lt;Image&gt;();\r\n        rawimageList = new List&lt;RawImage&gt;();\r\n        textList = new List&lt;Text&gt;();\r\n        selectTrans = Selection.GetTransforms(SelectionMode.TopLevel);\r\n        if (selectTrans == null)\r\n        {\r\n            Debug.LogError(\"\u6ca1\u6709\u9009\u4e2dUI\u3002\");\r\n            return;\r\n        }\r\n        for (int i = 0; i &lt; selectTrans.Length; i++)\r\n        {\r\n            Image[] images = selectTrans[i].GetComponentsInChildren&lt;Image&gt;(includeInactive);\r\n            for (int j = 0; j &lt; images.Length; j++)\r\n            {\r\n                if (images[j] != null)\r\n                    imageList.Add(images[j]);\r\n            }\r\n\r\n            RawImage[] rawimages = selectTrans[i].GetComponentsInChildren&lt;RawImage&gt;(includeInactive);\r\n            for (int j = 0; j &lt; rawimages.Length; j++)\r\n            {\r\n                if (rawimages[j] != null)\r\n                    rawimageList.Add(rawimages[j]);\r\n            }\r\n\r\n            Text[] texts = selectTrans[i].GetComponentsInChildren&lt;Text&gt;(includeInactive);\r\n            for (int j = 0; j &lt; texts.Length; j++)\r\n            {\r\n                if (texts[j] != null)\r\n                    textList.Add(texts[j]);\r\n            }\r\n        }\r\n    }\r\n\r\n    \/\/\/ &lt;summary&gt;\r\n    \/\/\/ \u79fb\u9664UI\u5c04\u7ebf\r\n    \/\/\/ &lt;\/summary&gt;\r\n    void RemoveRaycast()\r\n    {\r\n        for (int i = 0; i &lt; imageList.Count; i++)\r\n        {\r\n            imageList[i].raycastTarget = false;\r\n        }\r\n        for (int i = 0; i &lt; rawimageList.Count; i++)\r\n        {\r\n            rawimageList[i].raycastTarget = false;\r\n        }\r\n        for (int i = 0; i &lt; textList.Count; i++)\r\n        {\r\n            textList[i].raycastTarget = false;\r\n        }\r\n    }\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u679c\u9879\u76eeUI\u7ba1\u7406\u4e0d\u597d\uff0c\u524d\u671f\u4f1a\u51fa\u73b0\u5f88\u591a\u4e0d\u9700\u8981\u70b9\u51fb\u4e8b\u4ef6\uff0c\u4f46\u53c8\u6253\u5f00\u7740UI\u7684Raycas &hellip; <a href=\"http:\/\/www.u3d8.com\/?p=1283\">\u7ee7\u7eed\u9605\u8bfb <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[297],"tags":[298,293,299],"_links":{"self":[{"href":"http:\/\/www.u3d8.com\/index.php?rest_route=\/wp\/v2\/posts\/1283"}],"collection":[{"href":"http:\/\/www.u3d8.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.u3d8.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.u3d8.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.u3d8.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1283"}],"version-history":[{"count":2,"href":"http:\/\/www.u3d8.com\/index.php?rest_route=\/wp\/v2\/posts\/1283\/revisions"}],"predecessor-version":[{"id":1288,"href":"http:\/\/www.u3d8.com\/index.php?rest_route=\/wp\/v2\/posts\/1283\/revisions\/1288"}],"wp:attachment":[{"href":"http:\/\/www.u3d8.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1283"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.u3d8.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1283"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.u3d8.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1283"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}