docs.unity3d.com
    Show / Hide Table of Contents

    Class ElementAdderMenuBuilder

    Factory methods that create IElementAdderMenuBuilder<TContext> instances that can then be used to build element adder menus.

    Inheritance
    Object
    ElementAdderMenuBuilder
    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
    SetContractType(Type)

    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.

    See Also
    SetContractType(Type)
    Back to top
    Terms of use
    Copyright © 2023 Unity Technologies — Terms of use
    • Legal
    • Privacy Policy
    • Cookies
    • Do Not Sell or Share My Personal Information
    • Your Privacy Choices (Cookie Settings)
    "Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
    Generated by DocFX on 18 October 2023