Last active
November 5, 2015 01:22
-
-
Save Naphier/0451e93fdb057cc173b5 to your computer and use it in GitHub Desktop.
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
// This method allows us to add a ShowLocalAxis component to the currently selected transform. | |
// We'll be using a toggle box to add or remove the component. This bool holds that state. | |
private bool showLoxalAxisToggle = false; | |
// This holds a reference to the ShowLocalAxis component. | |
private ShowLocalAxis showLocalAxis; | |
private void ShowLocalAxisComponentToggle() | |
{ | |
// First we check to see if there is a ShowLocalAxis component on this game object. | |
showLocalAxis = _transform.gameObject.GetComponent<ShowLocalAxis>(); | |
// If not we can set the toggle to false which will remove the tick mark. | |
if (showLocalAxis == null) | |
showLoxalAxisToggle = false; | |
else // If the ShowLocalAxis component exists then ensure the toggle is set true. | |
showLoxalAxisToggle = true; | |
// Here we're going to use a Horizontal layout which allows us to put the elements on a single line. | |
EditorGUILayout.BeginHorizontal(); | |
// Put in our label field | |
EditorGUILayout.LabelField("Show local rotation handles", EditorStyles.boldLabel); | |
// Start monitoring the inspector for change in the showLocalAxisToggle value. | |
EditorGUI.BeginChangeCheck(); | |
// Display and set the toggle's value. | |
// In the label for this I am using a ternary operator which to decide what label to display. | |
// This is shorthand for saying: if showLocalAxisToggle is true then display "on" otherwise display "off". | |
showLoxalAxisToggle = EditorGUILayout.ToggleLeft((showLoxalAxisToggle ? "on" : "off" ), showLoxalAxisToggle); | |
// We can now stop monitoring for changes and IF a change has occurred then we add or remove the component. | |
if (EditorGUI.EndChangeCheck()) | |
{ | |
// If we've toggle ON the component then add it. | |
if (showLoxalAxisToggle == true) | |
{ | |
// Add the component | |
showLocalAxis = _transform.gameObject.AddComponent<ShowLocalAxis>(); | |
// Now we're going to move that component up right underneath the transform component. | |
// First we get the total number of components on the game object. | |
int componentCount = _transform.GetComponents<Component>().Length; | |
// Then we move the component up 1 less times than the number of components since the | |
// first compoent is the Transform component. | |
for (int i = 1; i < componentCount; i++) | |
{ | |
UnityEditorInternal.ComponentUtility.MoveComponentUp(showLocalAxis); | |
} | |
} | |
else | |
{ | |
// If we've toggle OFF the toggle then mark the component so that | |
// it can be destroyed at the proper time. This is handled in the | |
// ShowLocalAxisInspector class. | |
showLocalAxis.destroyWhenSafe = true; | |
} | |
} | |
// Finally we can end our horizontal layout so the next GUI element is on a new line. | |
EditorGUILayout.EndHorizontal(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment