Skip to content

Instantly share code, notes, and snippets.

@baba-s
Created August 4, 2014 02:02
Show Gist options
  • Select an option

  • Save baba-s/0e41f1dbe896d68bb131 to your computer and use it in GitHub Desktop.

Select an option

Save baba-s/0e41f1dbe896d68bb131 to your computer and use it in GitHub Desktop.
using UnityEditor;
using UnityEngine;
[InitializeOnLoad]
public static class HierarchyWindowCustomMenu
{
static HierarchyWindowCustomMenu()
{
// Hirarchy ウィンドウの OnGUI イベントで呼び出されるコールバックを登録します
EditorApplication.hierarchyWindowItemOnGUI += OnHierarchyWindowItemOnGUI;
}
// Hirarchy ウィンドウの OnGUI イベントで呼び出されるコールバック
private static void OnHierarchyWindowItemOnGUI( int instanceID, Rect selectionRect )
{
var current = Event.current;
if ( current.control && current.type == EventType.MouseDown )
{
// 表示位置を設定します
var mousePosition = current.mousePosition;
var width = 100;
var height = 100;
var position = new Rect( mousePosition.x, mousePosition.y - height, width, height );
// 表示するメニューの一覧を設定します
var options = new GUIContent[]
{
new GUIContent( "Empty" ),
new GUIContent( "" ),
new GUIContent( "Cube" ),
new GUIContent( "Sphere" ),
new GUIContent( "Capsule" ),
new GUIContent( "Cylinder" ),
new GUIContent( "Plane" ),
new GUIContent( "Quad" ),
new GUIContent( "" ),
new GUIContent( "Sprite" ),
};
// 現在選択中の番号を設定します
// 選択されているメニューにはチェックマークが表示されます
// チェックマークを表示したくない場合は -1 を設定します
var selected = -1;
// メニューがクリックされた時に呼び出されるコールバックに渡すデータを設定します
var userData = Selection.activeGameObject;
// コンテキストメニューを表示します
EditorUtility.DisplayCustomMenu( position, options, selected, Callback, userData );
}
}
// メニューがクリックされた時に呼び出されるコールバック
private static void Callback( object userData, string[] options, int selected )
{
var activeGameObject = userData as GameObject;
switch ( options[ selected ] )
{
case "Empty":
EditorApplication.ExecuteMenuItem( "GameObject/Create Empty" );
SetParent( Selection.activeGameObject, activeGameObject );
break;
case "Cube":
EditorApplication.ExecuteMenuItem( "GameObject/Create Other/Cube" );
SetParent( Selection.activeGameObject, activeGameObject );
break;
case "Sphere":
EditorApplication.ExecuteMenuItem( "GameObject/Create Other/Sphere" );
SetParent( Selection.activeGameObject, activeGameObject );
break;
case "Capsule":
EditorApplication.ExecuteMenuItem( "GameObject/Create Other/Capsule" );
SetParent( Selection.activeGameObject, activeGameObject );
break;
case "Cylinder":
EditorApplication.ExecuteMenuItem( "GameObject/Create Other/Cylinder" );
SetParent( Selection.activeGameObject, activeGameObject );
break;
case "Plane":
EditorApplication.ExecuteMenuItem( "GameObject/Create Other/Plane" );
SetParent( Selection.activeGameObject, activeGameObject );
break;
case "Quad":
EditorApplication.ExecuteMenuItem( "GameObject/Create Other/Quad" );
SetParent( Selection.activeGameObject, activeGameObject );
break;
case "Sprite":
EditorApplication.ExecuteMenuItem( "GameObject/Create Other/Sprite" );
SetParent( Selection.activeGameObject, activeGameObject );
break;
}
}
// 親オブジェクトを設定します
private static void SetParent( GameObject child, GameObject parent )
{
if ( child == null || parent == null )
{
return;
}
var t = child.transform;
t.parent = parent.transform;
t.localPosition = Vector3.zero;
t.localRotation = Quaternion.identity;
t.localScale = Vector3.one;
child.layer = parent.layer;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment