Otter Menu: Advanced Usage

OtterMenu_Logo_NewWFWarning: The text below is going to be engineering heavy. None of this should matter if you just want nice enumsĀ in the inspector, and all of this functionality is completely optional. If you want to make use of some our advanced functionality, then read ahead, but otherwise feel free to ignore this section.

Introduction

OtterMenu is a control that lets you draw nested, filtertable drop down menus in your inspector. It utilizes some of our core functionality to automatically work on enums. If you want to take advantage of our markup to make your enums nested or to name them differently, we will cover that section first. Otherwise, feel free to skip ahead to read about how we implemented our enum menu, and how you can use OtterMenu in your own controls.

Enum markup

If you want to name your enums differently than how they are used in code, or you want to categorize them into folders, you’ll have to tell us how you want us to render your enum. Let’s take the example enum below:

public enum GameSound
{
  FlameBurst, 
  FlameCircle,
  FlameSnap,
  FoodDeath,
  FoodBurning,
  FoodBurningSevere
}

This will get displayed as a list of 6 different items in a drop down list, and will be displayed using the same names shown here. As an example, let’s say we want to decorate the names with spaces, so that instead of seeing “FlameBurst” in the inspector, we want to see “Flame Burst”. We also want to put all the flame related entries and all the food related entries into subfolders, so that they’re not all shown in a single list. To do that, we will mark it up. For subcategories, OtterMenu uses the character ‘/’ to split entries into folders. Let’s mark it up:

public enum GameSound
{
  [UxInspectInfo("Flame/Flame Burst")]  
  FlameBurst,
  [UxInspectInfo("Flame/Flame Circle")]
  FlameCircle,
  [UxInspectInfo("Flame/Flame Snap")]
  FlameSnap,
  [UxInspectInfo("Food/Food Death")]
  FoodDeath,
  [UxInspectInfo("Food/Food Burning")]
  FoodBurning,
  [UxInspectInfo("Food/Food Burning Severe")]
  FoodBurningSevere
}

By marking it up as we’ve shown above, your menu will now show 2 folders, “Food” and “Flame”. Each of those will have 3 entries inside of it, and those entries will be displayed using their decorated names with spaces. You can specify any number of nested folders by using the ‘/’ character as delimiter.

Using OtterMenu with a custom Inspector

If you have a custom inspector for your object, enums won’t be rendered using OtterMenu. To render an enum from a custom inspector using OtterMenu, call the function UxInspector.InspectProperty. It takes 2 parameters, the first will be the SerializedProperty you would like to inspect, the second parameter is the target object from your serialized object. An example of how to call inspect property can be seen from the snippet of code below:

public override void OnInspectorGUI()
{
  //Tell the editor we may change some values
  EditorGUI.BeginChangeCheck();
  //Update the serialized object
  serializedObject.Update();
  //Get the property iterator
  SerializedProperty propertyIterator = serializedObject.GetIterator ();
  bool ExpandFirst = true;
  //While we can get another property
  while (propertyIterator.NextVisible(ExpandFirst))
  {
    //Inspect it using UxInspector. This is the key line
    UxInspector.InspectProperty(propertyIterator, serializedObject.targetObject);
    ExpandFirst = false;
  }
  //Tell the serialized object we may have modified it
  serializedObject.ApplyModifiedProperties();
  //And end the change check
  EditorGUI.EndChangeCheck ();
}

 

Using OtterMenuĀ for your own menus

If you want to use OtterMenu in your own code, you need to create an instance of OtterMenu by allocating an instance of UxSmartMenu. From there, you can add items using the AddItem call, which takes 3 parameters. The first parameter is the name of the item, the second parameter is an action that will be called when the item is selected, and the third parameter is passed through to your delegate action when the item is selected, and can be used to pass information to it. When you have added all your items, call Show() on the OtterMenu object to show it.

 UxSmartMenu smartMenu = new UxSmartMenu();
 smartMenu.AddItem(new GUIContent("First Item"),
   (Action<object>)delegate(object obj)
   {
     Debug.Log("We selected item " + (string)obj);
   },
   "A longer description to print for item 1"
 );
 smartMenu.AddItem(new GUIContent("Second Item"),
   (Action<object>)delegate(object obj)
   {
     object[] paramArray = (object[])obj;
     Debug.Log("We selected item " + (string)paramArray[0]);
     Debug.Log("It has " + (string)paramArray[1]);
   },
   new object[] {"Item 2", "Multiple parameters"}
 );
 smartMenu.AddItem(new GUIContent("Third Item"),
   (Action<object>)delegate(object obj)
   {
     Debug.Log("We selected item " + (string)obj);
   },
   "A longer description to print for item 3"
 );
 smartMenu.Show();

OtterMenu folders

OtterMenu AddItem parses the name you pass in for the first parameter, and will split the string passed in to find nested folders. The default delimiter character is a ‘/’, but you can change this to any character you like by setting the UxSmartMenu instance variable ‘Separator’ before calling AddItem. For example, to use a ‘>’ instead of a ‘/’, see the example below:

 UxSmartMenu smartMenu = new UxSmartMenu();
 smartMenu.Separator = '>';
 smartMenu.AddItem(new GUIContent("First Folder>First Item"),
   (Action<object>)delegate(object obj)
   {
     Debug.Log("We selected item " + (string)obj);
   },
   "A longer description to print for item 1"
 );
 smartMenu.Show();

Back to Otter Menu

Leave a Reply

Your email address will not be published.