Click or drag to resize
ElementAdderMenuBuilder Class
Inheritance Hierarchy
SystemObject
  Rotorz.ReorderableListElementAdderMenuBuilder

Namespace: Rotorz.ReorderableList
Assembly: Editor.ReorderableList (in Editor.ReorderableList.dll) Version: 0.0.0.0 (0.3.0.0)
Syntax
public static class ElementAdderMenuBuilder
Methods
Examples

The following example demonstrates how to build and display a menu which allows the user to add elements to a given context object upon clicking a button:

public class ShoppingListElementAdder : IElementAdder<ShoppingList> {
    public ShoppingListElementAdder(ShoppingList shoppingList) {
        Object = shoppingList;
    }

    public ShoppingList Object { get; private set; }

    public bool CanAddElement(Type type) {
        return true;
    }
    public object AddElement(Type type) {
        var instance = Activator.CreateInstance(type);
        shoppingList.Add((ShoppingItem)instance);
        return instance;
    }
}

private void DrawAddMenuButton(ShoppingList shoppingList) {
    var content = new GUIContent("Add Menu");
    Rect position = GUILayoutUtility.GetRect(content, GUI.skin.button);
    if (GUI.Button(position, content)) {
        var builder = ElementAdderMenuBuilder.For<ShoppingList>(ShoppingItem);
        builder.SetElementAdder(new ShoppingListElementAdder(shoppingList));
        var menu = builder.GetMenu();
        menu.DropDown(buttonPosition);
    }
}
See Also