Skip to content

Instantly share code, notes, and snippets.

@kou-yeung
Last active July 30, 2018 02:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kou-yeung/e203065159b84529a7f05ccb0dadb70f to your computer and use it in GitHub Desktop.
Save kou-yeung/e203065159b84529a7f05ccb0dadb70f to your computer and use it in GitHub Desktop.
/// 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