Skip to content

Instantly share code, notes, and snippets.

@kou-yeung kou-yeung/MenuOptions.cs Secret
Last active Jul 30, 2018

Embed
What would you like to do?
/// Path : Editor\UnityEditor.UI\MenuOptions.cs
///=========================================
/// UnityEditor.UI.MenuOptions の HACK です
/// メニューからUI を配置したら、イベントを受け取るように自前処理をしたいときに使用します
///
/// 参考 : https://github.com/tenpn/unity3d-ui/blob/master/UnityEditor.UI/UI/MenuOptions.cs
///=========================================
using UnityEngine.UI;
using System.Reflection;
using System.IO;
using System;
using System.Linq;
namespace UnityEditor.UI
{
public static class MenuOptions
{
static readonly string Path2DLL = Path.Combine(Path.GetDirectoryName(EditorApplication.applicationPath), @"Data\UnityExtensions\Unity\GUISystem\Editor\UnityEditor.UI.dll");
static readonly string TypeName = @"UnityEditor.UI.MenuOptions";
// ボタン作成時のコールバック
public static event Action<Button> OnCreatedButton = (btn) => { };
static Type Type
{
get { return Assembly.LoadFile(Path2DLL).GetType(TypeName); }
}
[MenuItem("GameObject/UI/Button", false, 2001)]
static void AddButton(MenuCommand menuCommand)
{
/// MEMO : 元の実装を呼び出して、ボタンを生成する
var method = Type.GetMethod("AddButton");
method.Invoke(null, new object[] { menuCommand });
/// MEMO : ヒエラルキーに選択されているオブジェクトを取得し、先頭オブジェクトのコンポーネントを取得する
var go = Selection.gameObjects.First();
OnCreatedButton(go.GetComponent<Button>());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.