Skip to content

Instantly share code, notes, and snippets.

@p-groarke
Created March 13, 2016 21:47
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save p-groarke/1d9b45507464681d530b to your computer and use it in GitHub Desktop.
Save p-groarke/1d9b45507464681d530b to your computer and use it in GitHub Desktop.
Resize Unity Reorderable List example.
// From MALQUA
// https://feedback.unity3d.com/suggestions/custom-element-size-in-reorderable-list
// http://i.imgur.com/fIbBorr.gifv
ReorderableList CreateList (SerializedObject obj, SerializedProperty prop)
{
ReorderableList list = new ReorderableList (obj, prop, true, true, true, true);
list.drawHeaderCallback = rect => {
EditorGUI.LabelField (rect, "Sprites");
};
List<float> heights = new List<float> (prop.arraySize);
list.drawElementCallback = (rect, index, active, focused) => {
SerializedProperty element = list.serializedProperty.GetArrayElementAtIndex (index);
Sprite s = (element.objectReferenceValue as Sprite);
bool foldout = active;
float height = EditorGUIUtility.singleLineHeight * 1.25f;
if (foldout) {
height = EditorGUIUtility.singleLineHeight * 5;
}
try {
heights [index] = height;
} catch (ArgumentOutOfRangeException e) {
Debug.LogWarning (e.Message);
} finally {
float[] floats = heights.ToArray ();
Array.Resize (ref floats, prop.arraySize);
heights = floats.ToList ();
}
float margin = height / 10;
rect.y += margin;
rect.height = (height / 5) * 4;
rect.width = rect.width / 2 - margin / 2;
if (foldout) {
if (s) {
EditorGUI.DrawPreviewTexture (rect, s.texture);
}
}
rect.x += rect.width + margin;
EditorGUI.ObjectField (rect, element, GUIContent.none);
};
list.elementHeightCallback = (index) => {
Repaint ();
float height = 0;
try {
height = heights [index];
} catch (ArgumentOutOfRangeException e) {
Debug.LogWarning (e.Message);
} finally {
float[] floats = heights.ToArray ();
Array.Resize (ref floats, prop.arraySize);
heights = floats.ToList ();
}
return height;
};
list.drawElementBackgroundCallback = (rect, index, active, focused) => {
rect.height = heights [index];
Texture2D tex = new Texture2D (1, 1);
tex.SetPixel (0, 0, new Color (0.33f, 0.66f, 1f, 0.66f));
tex.Apply ();
if (active)
GUI.DrawTexture (rect, tex as Texture);
};
list.onAddDropdownCallback = (rect, li) => {
var menu = new GenericMenu ();
menu.AddItem (new GUIContent ("Add Element"), false, () => {
serializedObject.Update ();
li.serializedProperty.arraySize++;
serializedObject.ApplyModifiedProperties ();
});
menu.ShowAsContext ();
float[] floats = heights.ToArray ();
Array.Resize (ref floats, prop.arraySize);
heights = floats.ToList ();
};
return list;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment