Skip to content

Instantly share code, notes, and snippets.

@cubed2d
Last active December 15, 2015 06:39
Show Gist options
  • Save cubed2d/5217832 to your computer and use it in GitHub Desktop.
Save cubed2d/5217832 to your computer and use it in GitHub Desktop.
Example of how i draw the awesome Array/List inspectors in the swindle
public override void OnInspectorGUI2()
{
// First off, cache a copy of target cast to its type, makes like easier. EXAMPLE
AmbientLight light = target as AmbientLight;
// This is to top area of the inspector GUI. draw controls here for making chages to parts of the target object that arnt part of the array.
light.timeOfDay = EditorGUILayout.Slider("Time of Day", light.timeOfDay, 0, 24);
// leave some space to split the main settings from the array
EditorGUILayout.Space();
EditorGUILayout.Space();
// this lays out the toolbar above the list
EditorGUILayout.BeginHorizontal(EditorStyles.toolbar); // pass the toolbar style in to the EditorGUILayout. this makes it look about a million times nicer!
GUILayout.Label("Colours"); // This Lable acts a a itle
GUILayout.FlexibleSpace(); // this eats up all the left over space, and pushes the buttons were about to draw to the right of the tool bar
if (GUILayout.Button("Clear", EditorStyles.toolbarButton)) // Use the ToolbarButton style on buttons to make everything look ace
{
// array clearing logic...
}
if (GUILayout.Button("Fill with sample colours", EditorStyles.toolbarButton))
{
// fill array with sample data...
}
if (GUILayout.Button("+", EditorStyles.toolbarButton))
{
// logic to add a new entry to the array
}
EditorGUILayout.EndHorizontal(); // end the toolbar
EditorGUILayout.BeginVertical(); // begin the list here! You could also use a scrollview if you had lots of data in your list... for example....
//EditorGUILayout.BeginScrollView(scrollPosition, GUILayout.MaxHeight(400)); // the max height lets you contorl how big it gets to be
if (light.dayColours == null || light.dayColours.Count == 0)
{
EditorGUILayout.LabelField("No colors, add some data");
}
else
{
// keep track of adding/removing entries
bool remove = false, addUp = false, addDown = false;
int index = 0;
for (int i = 0; i < light.dayColours.Count; i++) // do a for loop over your list
{
EditorGUILayout.BeginHorizontal(EditorStyles.textField); // This horizontal area encapsulates the whole entry. Use the text field style to gice it a nice background.
EditorGUILayout.BeginVertical();
EditorGUILayout.BeginHorizontal(EditorStyles.toolbar); // start the toolbar at the top of each entry
// you can use a Label here if you wanted a title for each entry
GUILayout.FlexibleSpace(); // eat up some space untill we draw buttons
if (GUILayout.Button(" - ", EditorStyles.toolbarButton)) // draw a button for deleting this entry
{
remove = true;
index = i;
}
if (GUILayout.Button("+ ↑", EditorStyles.toolbarButton)) // draw a button for adding a new enty above this one
{
addUp = true;
index = i-1;
}
if (GUILayout.Button("+ ↓", EditorStyles.toolbarButton)) // draw a button for adding a new entry below this one
{
addDown = true;
index = i+1;
}
EditorGUILayout.EndHorizontal(); // end the toolbar
// draw the entrys content here!
EditorGUILayout.EndVertical(); // end the entry box
EditorGUILayout.EndHorizontal();
}
// the List/array has been drawn! now, respond to the entry toolbar buttons.
if (remove)
{
light.dayColours.RemoveAt(index);
}
if (addUp || addDown) // NOTE: ive no idea if this bit will work. In the swindle i actually sort the list based on time of day, in this example ive used insert. It should work, but its not tested.
{
TimeOfDayColour c = new TimeOfDayColour(.....);
light.dayColours.Insert(c, index);
}
}
EditorGUILayout.EndVertical();// or end scroll view, if that what you used
if (GUI.changed)
{
// if something changed, tell unity to save it.
EditorUtility.SetDirty(light);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment