-
-
Save Frozenfire92/970ed3731cc8c70d6b12 to your computer and use it in GitHub Desktop.
Adds "Move to Top" and "Move to Bottom" items to the inspector context menu of components.
This file contains 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 UnityEngine; | |
using UnityEditor; | |
public class MoveComponentContext | |
{ | |
enum Destination | |
{ | |
Top, | |
Bottom | |
}; | |
const string kComponentArrayName = "m_Component"; | |
const int kFirstComponentIndex = 1; | |
[MenuItem ("CONTEXT/Component/Move to Top")] | |
static void Top (MenuCommand command) | |
{ | |
Move ((Component)command.context, Destination.Top); | |
} | |
[MenuItem ("CONTEXT/Component/Move to Bottom")] | |
static void Bottom (MenuCommand command) | |
{ | |
Move ((Component)command.context, Destination.Bottom); | |
} | |
static void Move (Component target, Destination destination) | |
{ | |
SerializedObject | |
component = new SerializedObject (target), | |
gameObject = new SerializedObject (target.gameObject); | |
SerializedProperty componentArray = gameObject.FindProperty (kComponentArrayName); | |
int size = componentArray.arraySize; | |
for (int index = kFirstComponentIndex; index < size; ++index) | |
{ | |
SerializedProperty iterator = componentArray.GetArrayElementAtIndex (index); | |
iterator.Next (true); | |
iterator.Next (true); | |
if (iterator.objectReferenceValue == target) | |
{ | |
componentArray.MoveArrayElement (index, destination == Destination.Top ? kFirstComponentIndex : size - 1); | |
gameObject.ApplyModifiedProperties (); | |
break; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment