コガネブログ

平日更新を目標に Unity や C#、Visual Studio、ReSharper などのゲーム開発アレコレを書いていきます

【Unity】uGUI のオブジェクトを自作のメニューから呼び出すエディタ拡張

概要

using System.Linq;
using UnityEditor;
using UnityEditor.Presets;
using UnityEngine;
using UnityEngine.UI;

public static class Example
{
    [MenuItem( "GameObject/MyProject/Image", false, 9 )]
    private static void CreateImage( MenuCommand menuCommand )
    {
        EditorApplication.ExecuteMenuItem( "GameObject/UI/Image" );
        var gameObject = Selection.activeGameObject;
        var parent     = menuCommand.context as GameObject;

        // 親オブジェクトを設定します
        GameObjectUtility.SetParentAndAlign( gameObject, parent );
    }
}
  • EditorApplication.ExecuteMenuItem を呼び出すだけだと
    親オブジェクトが正しく設定されないので
    GameObjectUtility.SetParentAndAlign を実行する必要がある

参考サイト様

優化 Unity uGUI 在 Raycast 效能消耗 – A optimizing tip of uGUI raycast