Editor Scripting - Selecting from a list of custom data objects

Hey guys,

I was wonder if its possible to create a drop down list in the editor which displays a list of data or classes much like a enum.

To give you a better example what I am trying to do is -

I create a new prefab for each level which stores all the data of the level such as name, start ratings, descriptions, spawns and spawn timing patterns, weapon ammo for each level and an weapon unlock event which you can select from a list of which weapon you want to have a animated event for unlocking a new weapon at the start of that level.

I have weapon combos and I am trying to make a combo event like the weapon unlock event so that I can select from a list of weapon combos and on that level it will display the animated unlock event announcement.

I want to make a class/struct which stores the name of the weapon combo, and two weapons are involved e.g “Shatter”, Weapon.Ice, Weapon.Shotgun.

That part is easy, but I am having trouble and not sure if its possible to have a variable in my level script which will produce a drop down list so can select my weapon combos - as doable with a enum.

Any advice would be greatly welcome!

You could search all classes with a specific attribute and take the name from this attribute you want to display in the drop down, than after you gathered the names into an array you can put it into a popup:

Take the selected index and use this index on the array of classes.

Needed usings:

using System.Collections.Generic;
using System.Linq;

The attribute:

public class MyAttribute : System.Attribute
    {
        public string DisplayName;
    }

    [MyAttribute(DisplayName="Foo baby!")]
    public class Foo
    {
    }

Gathering the data:

KeyValuePair<MyAttribute, Type>[] attributeToType = AppDomain.CurrentDomain.GetAssemblies().SelectMany(assembly =>
                {
                    KeyValuePair<MyAttribute, Type>[] data = AppDomain.CurrentDomain.GetAssemblies().SelectMany(assembly =>
                {
                    var test = assembly.GetTypes().Select(type =>
                        {
                            var myAttribute = (MyAttribute)type.GetCustomAttributes(typeof(MyAttribute), false).FirstOrDefault();
                            if (myAttribute == null)
                                return null;
                            return new { Attribute = myAttribute, Type = type };
                        }).Where(a => a != null).ToArray();
                    return test;
                }).Select(pair => new KeyValuePair<MyAttribute, Type>(pair.Attribute, pair.Type)).ToArray();

Using the data:

var newIndex = EditorGUI.Popup(..., oldIndex, attributeToType.Select(pair => pair.Attribute.DisplayName).ToArray());
if (newIndex != oldIndex)
{
  var newClassInstance = System.Activator.CreateInstance(attributeToType[newIndex].Type);
  ...
}

Thanks!

Much appreciated, I will have a look into it today. My current work around is just using enum and finding which enum is linked to which data set - but I prefer more dynamic methods such as yours.

Cheers,
Pip