ソースコード
using System;
using System.Reflection;
using UnityEditor;
using UnityEditorInternal;
using UnityEngine.Tilemaps;
public static class GridPaintPaletteWindowUtils
{
private const BindingFlags BINDING_ATTR = BindingFlags.Static | BindingFlags.Instance | BindingFlags.NonPublic;
private static readonly Assembly m_assembly = typeof( AssetDatabase ).Assembly;
private static readonly Type m_type = m_assembly.GetType( "UnityEditor.GridPaintPaletteWindow" );
private static readonly MethodInfo m_gridSelectKey = m_type.GetMethod( "GridSelectKey", BINDING_ATTR );
private static readonly MethodInfo m_gridMoveKey = m_type.GetMethod( "GridMoveKey", BINDING_ATTR );
private static readonly MethodInfo m_gridBrushKey = m_type.GetMethod( "GridBrushKey", BINDING_ATTR );
private static readonly MethodInfo m_gridRectangleKey = m_type.GetMethod( "GridRectangleKey", BINDING_ATTR );
private static readonly MethodInfo m_gridPickerKey = m_type.GetMethod( "GridPickerKey", BINDING_ATTR );
private static readonly MethodInfo m_gridEraseKey = m_type.GetMethod( "GridEraseKey", BINDING_ATTR );
private static readonly MethodInfo m_gridFillKey = m_type.GetMethod( "GridFillKey", BINDING_ATTR );
public static void GridSelectKey() => m_gridSelectKey.Invoke( null, new object[0] );
public static void GridMoveKey() => m_gridMoveKey.Invoke( null, new object[0] );
public static void GridBrushKey() => m_gridBrushKey.Invoke( null, new object[0] );
public static void GridRectangleKey() => m_gridRectangleKey.Invoke( null, new object[0] );
public static void GridPickerKey() => m_gridPickerKey.Invoke( null, new object[0] );
public static void GridEraseKey() => m_gridEraseKey.Invoke( null, new object[0] );
public static void GridFillKey() => m_gridFillKey.Invoke( null, new object[0] );
}
[InitializeOnLoad]
public static class Example
{
static Example()
{
Selection.selectionChanged += SelectionChanged;
}
private static void SelectionChanged()
{
var gameObject = Selection.activeGameObject;
if ( gameObject == null ) return;
var tilemap = gameObject.GetComponent<Tilemap>();
var isTilemap = tilemap != null;
if ( !isTilemap ) return;
if ( EditMode.editMode == EditMode.SceneViewEditMode.GridBox ) return;
GridPaintPaletteWindowUtils.GridRectangleKey();
}
}
使用例