OtterList: Advanced Usage

OtterList_Logo_NewFWarning: The text below is going to be engineering heavy. None of this should matter if you just want nice lists in the inspector, and all of this functionality is completely optional. If you are a programmer that cares about good object oriented practices, then read ahead, but otherwise feel free to ignore this section.

Introduction

One of the things that causes bad coding practices in Unity is the way that it uses the public and private keywords to decide whether object members should be shown in the editor panel or not. A class member being public, protected, or private should reflect its visibility in code. A private member is one that outside classes should not be accessing at runtime, but that doesn’t mean it’s a value that you wouldn’t want edited in the editor itself. Since anything you want edited in an editor panel needs to be public, there is a tendency for accessibility creep, where every member has a tendency to become public. This makes it difficult to enforce things such as loose coupling between components, which leads towards code that is difficult to refactor. This is a problem that we worked very hard to solve, and the UxInspector class is our solution to this. UxInspector is also the class that makes OtterList possible.

 

Using UxInspector

If you mark your class with a [UseUxInspector] tag, it will use UxInspector. Let’s dive into an example to show how it works.

public class UxInspectorExampleBehaviour: MonoBehaviour
{
    public string ThisStringShouldBePublicButNotEdited;
    private string ThisStringShouldBeEditedButNotPublic;
}

 

Here I’ve created a very contrived class with 2 members, one a public string that we don’t want to show in the editor, and another that we want to show in the editor, but we don’t want public. If you attach this class to a GameObject in the normal Unity editor, you will have the opposite behavior in your Inspector. You will see the public string that is editable, and the private one won’t be shown in the inspector. If you swap the public/private flags, it will show in the Inspector correctly, but your class scope is broken. Now let’s see how UxInspector fixes that.

[UseUxInspector]
public class UxInspectorExampleBehaviour: MonoBehaviour
{
    public string ThisStringShouldBePublicButNotEdited;
    [UxInspectInfo("This is the tooltip for the editable member")]
    private string ThisStringShouldBeEditedButNotPublic;
}

 

By marking the class with UseUxInspector, we will use the our special inspector markup. The default behavior for the UxInspector is that NO members are editable in the inspector. If you want a member to be visible in the Inspector, you mark it with UxInspectInfo. The string parameter it takes is the tooltip description. This class, as shown, will work as we hoped. The public string is public, but is not editable in the Inspector. The private string shows up in the Inspector, but is still marked as private.

By using the special markup in all of our classes, we can practice proper object oriented programming practices without having Unity Editor requirements break our architecture. Internally, we use the special inspector markup for all of our classes so that we enforce proper programming OO techniques. If you decide to switch to using the UxInspector markup, it will require code changes, but you’ll quickly find you are writing much better code. If you have any questions or comments on our classes, please don’t hesitate to let us know in the forums. We’re always happy to discuss programming techniques with fellow developers!

 

Using OtterList with a custom Inspector

If you have a custom inspector for your object, list classes won’t be rendered using OtterList. To render a list from a custom inspector using OtterList, 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 ();
}

 

3 Comments:

  1. Grant Morgan

    Is there a way to stop Otter list from running on an Monobehaviour? I am having conflicts other 3rd party assests. I guess I am looking for a [DonNotUseUxInspector] that stops all CockworkOtter code from running in the inspector for an Monobehaviour.

  2. Hi Grant. Great suggestion. Because my reply is long, I am reposting your question and my answer to the forums at the link below:
    Clockwork Otter Forums Thread

  3. Grant, we have submitted v1.05 to the Unity store, which now has an attribute that can be added to classes. To use it, just mark your class with:
    [DontUseUxInspector]

Leave a Reply

Your email address will not be published. Required fields are marked *