
{"id":119,"date":"2021-02-20T19:01:37","date_gmt":"2021-02-20T19:01:37","guid":{"rendered":"http:\/\/crashedcreativity.pl\/?p=119"},"modified":"2022-06-22T20:01:10","modified_gmt":"2022-06-22T20:01:10","slug":"unity-custom-editor-context-menu-in-a-scene-view","status":"publish","type":"post","link":"https:\/\/crashedcreativity.pl\/index.php\/2021\/02\/20\/unity-custom-editor-context-menu-in-a-scene-view\/","title":{"rendered":"Unity Custom Editor: Context Menu in a Scene View"},"content":{"rendered":"\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" width=\"1024\" height=\"163\" src=\"http:\/\/crashedcreativity.pl\/wp-content\/uploads\/2021\/02\/cover-1024x163.jpg\" alt=\"\" class=\"wp-image-120\" srcset=\"https:\/\/crashedcreativity.pl\/wp-content\/uploads\/2021\/02\/cover-1024x163.jpg 1024w, https:\/\/crashedcreativity.pl\/wp-content\/uploads\/2021\/02\/cover-300x48.jpg 300w, https:\/\/crashedcreativity.pl\/wp-content\/uploads\/2021\/02\/cover-768x122.jpg 768w, https:\/\/crashedcreativity.pl\/wp-content\/uploads\/2021\/02\/cover-1536x244.jpg 1536w, https:\/\/crashedcreativity.pl\/wp-content\/uploads\/2021\/02\/cover.jpg 1548w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Hi. I have started new series of tutorials describing step by step building Adventure Game Engine on a top of a Unity. <\/p>\n\n\n\n<p>Today i will show You something simple  but not very obvious. I wanted to make context menu with custom actions in editor to simplify scene building . I want to tailor the unity engine for this narrow type of games. I next tutorial i want to make editor for player&#8217;s walk path. This path will be represented by graph with assigned animations and path finding algorithm.<\/p>\n\n\n\n<p>It is good practice to place custom editor in &#8220;Editor&#8221; folder. Thanks to that this script wil be excluded from final build. &#8220;Editor&#8221; folders can be placed in any branch of &#8220;Assets&#8221;<\/p>\n\n\n\n<p>This code bellow adds custom action &#8220;Do something&#8221; to any scene object selected by shift+ right mouse click. This action only prints debug.<\/p>\n\n\n\n<p>That is the code:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>using System;\nusing System.Linq;\nusing System.Reflection;\nusing System.Collections;\nusing System.Collections.Generic;\nusing UnityEngine;\n#if UNITY_EDITOR\nusing UnityEditor;\n#endif\n\n\/*\n \n\tThis code is based on more advanced example found here:\n\n   \/\/ https:\/\/forum.unity.com\/threads\/sceneview-context-menu-script-free.525029\/\n   \n\tI have simplified this code for tutorial purpose\n*\/\n\n#if UNITY_EDITOR\n&#91;InitializeOnLoad]\npublic class SceneViewContextMenuInitializer : Editor\n{\n\n\n\t\/\/ Key that's got to be pressed when right clicking, to get a context menu.\n\tstatic readonly EventModifiers modifier = EventModifiers.Shift;\n\n\n\tstatic SceneViewContextMenuInitializer()\n\t{\n        SceneView.onSceneGUIDelegate -= OnSceneGUI;\n        SceneView.onSceneGUIDelegate += OnSceneGUI;\n    }\n\n\n\n    static void OnSceneGUI(SceneView sceneview)\n\t{\n\t\t\/\/ If shift + right mouse click.\n        if (Event.current.modifiers == modifier &amp;&amp; Event.current.button == 1 &amp;&amp; Event.current.type == EventType.MouseDown)\n\t\t{\n\t\t\t\n\n\t\t\t\/\/ Ray from editor camera.\n\t\t\tvar ray = HandleUtility.GUIPointToWorldRay(Event.current.mousePosition);\n\n\t\t\t\/\/ Check if object selected.\n\t\t\tvar materialIndex = 0;\n\t\t\tvar go = HandleUtility.PickGameObject(Event.current.mousePosition, out materialIndex);\n\n\t\t\t\/\/ Show menu.\n\t\t\tif (go != null)\n\t\t\t{\n\t\t\t\tShowMenu(go, ray);\n\t\t\t\treturn;\n\t\t\t}\n\t\t\t\n\t\t\t\n\t\t}\n    }\n\n\t\n\n\t\/\/ Menu from context items.\n\tstatic void ShowMenu(GameObject go, Ray ray)\n\t{\n\t\tvar menu = new GenericMenu();\n\t\t\n\n\t\tmenu.AddItem(new GUIContent(\"Do something\"), false, () => menuAction()); \n\t\t\/\/ Show as context.\n\t\tmenu.ShowAsContext();\n\t}\n\tprivate static void menuAction () {\n\t\tDebug.Log(\"worked\");\n\t}\n\n\n}\n#endif<\/code><\/pre>\n\n\n\n<p>Class must inherit on Editor class and implement on onSceneGui method. When shift and right mouse click event is produced the object is picked from the scene with ray cast from scene camera. Then context menu is displayed.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>  <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hi. I have started new series of tutorials describing step by step building Adventure Game Engine on a top of<\/p>\n<div class=\"read-button read_more_btn_text\"><a class=\"read_more\" href=\"https:\/\/crashedcreativity.pl\/index.php\/2021\/02\/20\/unity-custom-editor-context-menu-in-a-scene-view\/\">Read Now<span class=\"arrow_readm\"> &#x25BA;<\/span><\/a><\/div>\n","protected":false},"author":1,"featured_media":120,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/crashedcreativity.pl\/index.php\/wp-json\/wp\/v2\/posts\/119"}],"collection":[{"href":"https:\/\/crashedcreativity.pl\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/crashedcreativity.pl\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/crashedcreativity.pl\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/crashedcreativity.pl\/index.php\/wp-json\/wp\/v2\/comments?post=119"}],"version-history":[{"count":7,"href":"https:\/\/crashedcreativity.pl\/index.php\/wp-json\/wp\/v2\/posts\/119\/revisions"}],"predecessor-version":[{"id":127,"href":"https:\/\/crashedcreativity.pl\/index.php\/wp-json\/wp\/v2\/posts\/119\/revisions\/127"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/crashedcreativity.pl\/index.php\/wp-json\/wp\/v2\/media\/120"}],"wp:attachment":[{"href":"https:\/\/crashedcreativity.pl\/index.php\/wp-json\/wp\/v2\/media?parent=119"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/crashedcreativity.pl\/index.php\/wp-json\/wp\/v2\/categories?post=119"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/crashedcreativity.pl\/index.php\/wp-json\/wp\/v2\/tags?post=119"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}