Unity Inspector Editor with ReordableList

Working on my latest game, I had created a number of Mono components (Monobehaviors with public variables) that assisted with some of the Sprites in the game.

In particular, I created a SpriteFactory, where its job was simply to return something called a SpritePackage which contained a number of Sprite objects. The SpritePackage had a Name property, which was used to lookup the name of the SpritePackage we wanted and return. It was very simple, and got the job done.

However, it was also very cumbersome to use, as it used the Default Unity Inspector Editor, which was fine, but made it difficult especially when there were lots of SpritePackages. It looked like this.

/images/SNAG-0008-12-6-2015-15-06-PM.png

What I really wanted was a way to Show the list of SpritePackages and show a Thumbnail of the Package. This way I could easily pick the one I wanted to edit.

So I started working on a Inspector Editor. I watched lots of Videos and read several Blog articles showing how to make my own Editor.

The first piece of the puzzle was figuring out that I needed to convert my SpritePackage into a ScriptableObject. I am not going to go into detail on this as there is lots of information out there on what they can do. In short though, they are a great way to store a lot of information that can be used at Runtime in your game. Configuration files, Inventory databases, etc, etc.

So I went through a few iterations, lots of trial and error and finally came up with something that let me pick a SpritePackage and add more Sprite objects to it. But I could see that there was going to be a lot more work involved to have it be able to Add more items, Remove Items, etc, etc.

This was my first version of the Editor.

/images/SNAG-0005-12-3-2015-00-47-AM.png

I actually think it looked pretty good, but like I said, there was a lot more work to do. I wanted to be able to resort the List, I wanted to be able to Drag them in the order I wanted, I needed to find an easier way. After searching the Internets for, I stumbled upon this Article; http://va.lent.in/unity-make-your-lists-functional-with-reorderablelist/

It appears, that as of release 4.5, the guys at Unity created an Undocumented component called the ReordableList that could be used to create exactly what I needed.

A few hours later, I had a working prototype using the new ReordableList component with the basic functionality I wanted.

/images/SNAG-0006-12-4-2015-23-48-PM.png

Once I had those parts worked out, I simply had to re-factor the code to plug in my ScriptableObject, and this was the result.

/images/SNAG-0007-12-5-2015-12-39-PM.png

Here is a shot of it in action.

I really like how this turned out and although it took some playing around, the results are exactly what I needed.

I hope to create another Article showing how to build one of these. But to be honest, the link I provided above has everything you need to get started

Comments

Comments powered by Disqus