Created
April 5, 2024 02:40
-
-
Save baba-s/b65bb1259387917bfd4aa3d8be62ff2b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using UnityEditor; | |
using UnityEngine; | |
namespace Kogane.Internal | |
{ | |
/// <summary> | |
/// JetBrains Rider のように Ctrl + Alt + Shift + ↑↓ でゲームオブジェクトの順番を移動できるエディタ拡張 | |
/// </summary> | |
[InitializeOnLoad] | |
internal static class MoveGameObjectsUpAndDown | |
{ | |
//================================================================================ | |
// 関数(static) | |
//================================================================================ | |
/// <summary> | |
/// コンストラクタ | |
/// </summary> | |
static MoveGameObjectsUpAndDown() | |
{ | |
EditorApplication.hierarchyWindowItemOnGUI += HierarchyWindowItemOnGui; | |
EditorApplication.hierarchyWindowItemOnGUI += HierarchyWindowItemOnGui; | |
} | |
/// <summary> | |
/// Hierarchy の項目を描画する時に呼び出されます | |
/// </summary> | |
private static void HierarchyWindowItemOnGui | |
( | |
int instanceID, | |
Rect selectionRect | |
) | |
{ | |
var current = Event.current; | |
var keyCode = current.keyCode; | |
if ( !current.control ) return; | |
if ( !current.alt ) return; | |
if ( !current.shift ) return; | |
if ( current.type != EventType.KeyDown ) return; | |
if ( keyCode != KeyCode.UpArrow && keyCode != KeyCode.DownArrow ) return; | |
current.Use(); | |
foreach ( var transform in Selection.transforms ) | |
{ | |
var siblingIndex = transform.GetSiblingIndex(); | |
var nextSiblingIndex = keyCode switch | |
{ | |
KeyCode.UpArrow => Mathf.Max( 0, siblingIndex - 1 ), | |
KeyCode.DownArrow => siblingIndex + 1, | |
_ => throw new ArgumentOutOfRangeException() | |
}; | |
transform.SetSiblingIndex( nextSiblingIndex ); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment