Guest User

Variable Height Reorderable List Example

a guest
Dec 14th, 2015
7,071
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.     ReorderableList CreateList (SerializedObject obj, SerializedProperty prop)
  2.     {
  3.         ReorderableList list = new ReorderableList (obj, prop, true, true, true, true);
  4.  
  5.         list.drawHeaderCallback = rect => {
  6.             EditorGUI.LabelField (rect, "Sprites");
  7.         };
  8.  
  9.         List<float> heights = new List<float> (prop.arraySize);
  10.  
  11.         list.drawElementCallback = (rect, index, active, focused) => {
  12.             SerializedProperty element = list.serializedProperty.GetArrayElementAtIndex (index);
  13.             Sprite s = (element.objectReferenceValue as Sprite);
  14.  
  15.             bool foldout = active;
  16.             float height = EditorGUIUtility.singleLineHeight * 1.25f;
  17.             if (foldout) {
  18.                 height = EditorGUIUtility.singleLineHeight * 5;
  19.             }
  20.  
  21.             try {
  22.                 heights [index] = height;
  23.             } catch (ArgumentOutOfRangeException e) {
  24.                 Debug.LogWarning (e.Message);
  25.             } finally {
  26.                 float[] floats = heights.ToArray ();
  27.                 Array.Resize (ref floats, prop.arraySize);
  28.                 heights = floats.ToList ();
  29.             }
  30.  
  31.             float margin = height / 10;
  32.             rect.y += margin;
  33.             rect.height = (height / 5) * 4;
  34.             rect.width = rect.width / 2 - margin / 2;
  35.  
  36.             if (foldout) {
  37.                 if (s) {
  38.                     EditorGUI.DrawPreviewTexture (rect, s.texture);
  39.                 }
  40.             }
  41.             rect.x += rect.width + margin;
  42.             EditorGUI.ObjectField (rect, element, GUIContent.none);
  43.         };
  44.  
  45.         list.elementHeightCallback = (index) => {
  46.             Repaint ();
  47.             float height = 0;
  48.  
  49.             try {
  50.                 height = heights [index];
  51.             } catch (ArgumentOutOfRangeException e) {
  52.                 Debug.LogWarning (e.Message);
  53.             } finally {
  54.                 float[] floats = heights.ToArray ();
  55.                 Array.Resize (ref floats, prop.arraySize);
  56.                 heights = floats.ToList ();
  57.             }
  58.  
  59.             return height;
  60.         };
  61.  
  62.         list.drawElementBackgroundCallback = (rect, index, active, focused) => {
  63.             rect.height = heights [index];
  64.             Texture2D tex = new Texture2D (1, 1);
  65.             tex.SetPixel (0, 0, new Color (0.33f, 0.66f, 1f, 0.66f));
  66.             tex.Apply ();
  67.             if (active)
  68.                 GUI.DrawTexture (rect, tex as Texture);
  69.         };
  70.  
  71.         list.onAddDropdownCallback = (rect, li) => {
  72.             var menu = new GenericMenu ();
  73.             menu.AddItem (new GUIContent ("Add Element"), false, () => {
  74.                 serializedObject.Update ();
  75.                 li.serializedProperty.arraySize++;
  76.                 serializedObject.ApplyModifiedProperties ();
  77.             });
  78.  
  79.             menu.ShowAsContext ();
  80.  
  81.             float[] floats = heights.ToArray ();
  82.             Array.Resize (ref floats, prop.arraySize);
  83.             heights = floats.ToList ();
  84.         };
  85.  
  86.         return list;
  87.     }
RAW Paste Data