Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Re-orderable, customizable array control for inspector

Discussion in 'Assets and Asset Store' started by duke, Jan 16, 2013.

  1. duke

    duke

    Joined:
    Jan 10, 2007
    Posts:
    763
    You know the Script execution order dialog? You know how you can drag stuff around and add/remove items? Well I hacked that to pieces and made a re-usable inspector component out of it. It is and will remain free.

    Here's a video of it in action (thanks for the pink, youtube). Note that the chopiness is due to the capture software:


    Simply copy all 4 scripts to your project's editor folder. The customisable part is in a constructor argument, where you feed it a method or lambda lambda lambda to say what's drawn for each item, although this is implemented, it's untested (the default implementation adds the drag icon, object field, and remove button). To use the control, do something like this:

    Code (csharp):
    1.  
    2. [CustomEditor(typeof(SomeComponent))]
    3. public class SomeComponentEditor : EditorBase<SomeComponent>
    4. {
    5.     private ArrayGUI<Camera> _camerasGUI;
    6.  
    7.     public void OnEnable()
    8.     {
    9.         if (_camerasGUI== null)
    10.             _camerasGUI= new ArrayGUI<Camera>("Cameras", () => Target.Cameras);
    11.     }
    12.  
    13.     public override void OnInspectorGUI()
    14.     {
    15.         if (Target.Cameras != null)
    16.             _camerasGUI.OnInspectorGUI();
    17.     }
    18.  
    Known issues:
    - Can only add 1 item at a time
    - Item source must be implementation of IList<T>, thus actual arrays aren't supported yet.

    Version 1.02
    - Added: Support for all types (previously limited to Object inheritors). Non-inheritors of UnityEngine.Object need to provide a custom drawElementAction callback.
    - Added: Readonly option (defaults to false) (display, edit reorder items, but can't add, remove or swap them)
    - Added: AllowDuplicates option (defaults to false). It's up to you to ensure you've implemented correct Equality checks.
    - Added: IArrayGUIApi to assist with custom drawElementActions and other callbacks. Any actions that alter the collection that you perform should be added to the PreFrameActions queue (PreFrameActions.Enqueue(() => { // code here }), otherwise you'll get... issues (writing to collection mid-frame, etc.).

    Version 1.01
    - Fixed: Clearing items, and adding new ones would disable sorting.
    - Fixed: Temporary array getting created every Update!
    - Changed: ItemDrawer callback is now nested with the drag handle to the left, and remove icon to the right. Previously you had to add these yourself (which didn't make sense for array items that always require the functionality).
    - Added: Item changing, previously d&d'ing new item onto existing one had no effect, now it does what you'd expect.
     

    Attached Files:

    Last edited: Jan 19, 2013
  2. jedy

    jedy

    Joined:
    Aug 1, 2010
    Posts:
    579
    Great job! Neat little script.

    I have a ton of ideas for further extending it.
    Inspecting components directly in the list, Adding New Components via dragging, Auto initialization, The whole thing could become a simple attribute to display arrays in a cool manner with ease, Create Add new component button etc etc. Quite nice!

    Sadly I'll be a bit shore on time for a couple of deadlines, but I'll try to contribute whenever I have the spare time!
     
  3. duke

    duke

    Joined:
    Jan 10, 2007
    Posts:
    763
    Hi Jedy, Thanks for the feedback! I did have an add button, but I figured it was simpler to always have an empty slot at the bottom ready to go - as soon as you fill it with something, another blank is added to the end. This means one less button click as well.

    Things planned without any form of timeframe:
    - Remove a limitation that only allows class arrays (so no Vector3's, etc.).
    - Make this a Property Drawer (attribute).
    - Drag+Drop single or multiple components and have them add themselves to the list.
     
  4. jedy

    jedy

    Joined:
    Aug 1, 2010
    Posts:
    579
    Great idea actually. I was wondering why was this empty button hanging in there.

    This week I'll be implementing some editor code for a FSM in our project, so I'll give it a shot for the components there - meanwhile I will give the "Show the components inspector" thing a shot. I'll try to get that done in a week or two so I can share the inspector part here.
     
  5. duke

    duke

    Joined:
    Jan 10, 2007
    Posts:
    763
    Here's a snippet showing how to create it with a custom GUI for the items:

    Code (csharp):
    1.  
    2.         if (_arrayGUI == null)
    3.             _arrayGUI = new ArrayGUI<Waypoint>(
    4.                         "Waypoints",
    5.                         () => Target.Waypoints,
    6.                         (rect, item, isDragging) =>
    7.                         {
    8.                             // GUI stuff here
    9.                         });
    10.  
     
  6. duke

    duke

    Joined:
    Jan 10, 2007
    Posts:
    763
    Updated to 1.01, see original post.

    Due to the Asset Store really wanting actual finished projects, it's not ready to submit in its current state. There's plenty of error checking but it's not watertight and still makes a few assumptions. Making it a finished product isn't a priority, making/keeping it usable is, so I'll continue along those lines.

    The current uploaded files reflect what you'd expect from an Asset Store package (it's own folder, an Example, a readme).

    edit: Updated to 1.02!
     
    Last edited: Jan 19, 2013
  7. jedy

    jedy

    Joined:
    Aug 1, 2010
    Posts:
    579
    Excellent work. I will give it a test run tomorrow.

    I will give the integrated inspector a shot. I have a couple of ideas about it that I need to confirm.
     
  8. frarees

    frarees

    Joined:
    Jun 8, 2012
    Posts:
    29
    Is there any progress on the propertydrawer version? I am too excited about this!
     
  9. JesOb

    JesOb

    Joined:
    Sep 3, 2012
    Posts:
    1,081
  10. Apiweb

    Apiweb

    Joined:
    Nov 26, 2011
    Posts:
    57
    Sorry for the huge bump, first congratulations for the great plugin, it is useful.
    Would any news about PropertyDrawer?