Class ElementAdderMenuBuilder
Factory methods that create IElementAdderMenuBuilder<TContext> instances that can then be used to build element adder menus.
Namespace: Unity.VisualScripting.ReorderableList.Element_Adder_Menu
Syntax
public static class ElementAdderMenuBuilder
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);
}
}
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);
}
}
Methods
For<TContext>()
Gets a IElementAdderMenuBuilder<TContext> to build an element
adder menu for a context object of the type TContext
.
Declaration
public static IElementAdderMenuBuilder<TContext> For<TContext>()
Returns
Type | Description |
---|---|
IElementAdderMenuBuilder<TContext> | A new IElementAdderMenuBuilder<TContext> instance. |
Type Parameters
Name | Description |
---|---|
TContext | Type of the context object that elements can be added to. |
See Also
For<TContext>(Type)
Gets a IElementAdderMenuBuilder<TContext> to build an element
adder menu for a context object of the type TContext
.
Declaration
public static IElementAdderMenuBuilder<TContext> For<TContext>(Type contractType)
Parameters
Type | Name | Description |
---|---|---|
Type | contractType | Contract type of addable elements. |
Returns
Type | Description |
---|---|
IElementAdderMenuBuilder<TContext> | A new IElementAdderMenuBuilder<TContext> instance. |
Type Parameters
Name | Description |
---|---|
TContext | Type of the context object that elements can be added to. |