Reorderable List Control for Unity
| ElementAdderMenuBuilder Class |
Factory methods that create IElementAdderMenuBuilderTContext
instances that can then be used to build element adder menus.
Inheritance HierarchySystemObject Rotorz.ReorderableListElementAdderMenuBuilder
Namespace: Rotorz.ReorderableList
Assembly: Editor.ReorderableList (in Editor.ReorderableList.dll) Version: 0.0.0.0 (0.3.0.0)
Syntaxpublic static class ElementAdderMenuBuilder
public static class ElementAdderMenuBuilder
Methods| Name | Description | |
|---|---|---|
![]() ![]() | ForTContext |
Gets a IElementAdderMenuBuilderTContext to build an element
adder menu for a context object of the type TContext.
|
![]() ![]() | ForTContext(Type) |
Gets a IElementAdderMenuBuilderTContext to build an element
adder menu for a context object of the type TContext.
|
ExamplesThe 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); } }
public class ShoppingListElementAdder extends IElementAdder.<ShoppingList> {
var _object:ShoppingList;
function ShoppingListElementAdder(shoppingList:ShoppingList) {
Object = shoppingList;
}
function get Object():ShoppingList { return _object; }
function CanAddElement(type:Type):boolean {
return true;
}
function AddElement(type:Type):System.Object {
var instance = Activator.CreateInstance(type);
shoppingList.Add((ShoppingItem)instance);
return instance;
}
}
function DrawAddMenuButton(shoppingList:ShoppingList) {
var content = new GUIContent('Add Menu');
var 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
