Unity Interface Optimization: Making the Inspector More User-Friendly.
Author: bwaynesu
Created: August 03, 2017
Tags: C#, Unity3D
Support: Unity 2019 or Lower
The purpose of this project is to optimize Unity Inspector and improve game development efficiency. As shown in Fig.1, the current variable does not indicate whether leaving it empty would cause a game error.
Developers can only check it during runtime through code. If we can notify developers of this error while editing data, we can avoid unnecessary debugging time and improve project development efficiency (Fig.2).
In addition, this project improves the display format of some variables for easier editing and reading by developers. The project also includes a sample scene for practical exercises.
Use a specific variable in a class as the display name instead of "Element 0", "Element 1", "Element 2", etc.
// Method 1: Display name will use field value
// Parameters:
// typeVarName: The name of the variable to be used as the display name.
[BInspector.ArrayElementTitle("i")]
public CustomClass[] customClassAry = new CustomClass[0];
[Serializable]
public class CustomClass
{
public int i = -1;
public bool b = false;
public float f = 0f;
}
// Method 2: Display name will use enum value
// Parameters:
// typeVarName: The Enum to be used as the display name.
[BInspector.ArrayElementTitle(typeof(TestEnumTitle))]
public CustomClass[] customClassAry2 = new CustomClass[0];
public enum TestEnumTitle
{
Enum1,
Enum2,
Enum3,
}
Display the variable based on conditions
// Parameters:
// VariableName: the name of the Boolean field, property, or method to be evaluated
// IsInverse: whether to invert the triggering condition (only trigger if False)
public bool isShow = false;
[BInspector.ConditionalHide("isShow", _isInverse: false)]
public int byVar = 0;
Change the display name, tooltip, color, and read-only status of a variable.
// Parameters:
// name: Display name.
// tips: Tooltip (optional).
// (r, g, b, a): Background color (optional).
// isReadOnly: Whether the variable is read-only (default: false).
[BInspector.Descriptor("Integer", "I'm a tooltip.", 0, 1, 1, 1)]
public int testInt = -1;
[BInspector.Descriptor("GameObject", "Please drag an object here.", 1, 1, 0, 1)]
public GameObject testGo = null;
[BInspector.Descriptor("Read-only", null, true)]
public float testFloat = 123f;
Display an enum using a mask (multiple selections are possible).
// Parameters:
// displayName: Display name (optional).
[BInspector.EnumMask("Mask")]
public MyMask myMask;
// The enum values must follow this sequence: 1, 2, 4, 8, 16, 32, 64, etc.
// Otherwise, an error will occur when the value is retrieved.
// Value of 0 do not need to be set.
public enum MyMask
{
One = 1,
Two = 2,
Three = 4,
Four = 8,
}
Display a message (info/warning/error) based on a condition.
// Method 1: Use the built-in Common Judge Type to evaluate the condition.
// CommonJudgeType contains frequently used conditions.
// If the condition is met, a message is displayed:
// IntNegative: The integer is negative.
// IntZero: The integer is zero.
// FloatNegative: The float is negative.
// StringNullOrEmpty: The string is null or empty.
// ReferenceNull: The object is null.
[BInspector.HelpMessage(CommonJudgeType.ReferenceNull)]
public Camera c = null;
// Method 2: Use a custom function to evaluate the condition.
// Parameters:
// CustomMethodName: The name of the custom function to use.
[BInspector.HelpMessage("Show")]
public int i = -1;
HelpMessageContent helpMsgContent = new HelpMessageContent();
public HelpMessageContent Show()
{
if (i < 0)
{
helpMsgContent.SetMessage(HelpType.Error, i + " is smaller than 0.");
return helpMsgContent;
}
if (i >= 0 && i < 10)
{
helpMsgContent.SetMessage(HelpType.Warning, "i = " + i);
return helpMsgContent;
}
return null;
}
This feature allows using a Property (getter, setter) to display a variable in the Inspector, instead of displaying the variable directly.
// Note: this feature must be used with the SerializeField attribute.
// Parameters:
// propertyName: The name of the Property to display.
// displayName: The display name (if not provided, the original Property name will be used).
[BInspector.InsteadShowByProperty("BoolProperty", "Bool Prop"), SerializeField]
bool b = false;
public bool BoolProperty
{
get { return b; }
set
{
Debug.Log("Set b: " + value);
b = value;
}
}
Display Min Max Slider
// Divided into RangeInt and RangeFloat
// Parameters:
// minLimit: the minimum value
// maxLimit: the maximum value
// defaultMinVal: the default smaller value
// defaultMaxVal: the default larger value
// isFixedRange: whether to limit the modification of the minimum and maximum values (default is True)
public RangeInt rangeInt1 = new RangeInt(0, 100);
public RangeInt rangeInt2 = new RangeInt(0, 100, 25, 75, false);
public RangeFloat rangeFloat1 = new RangeFloat(-10f, 10f, false);
public RangeFloat rangeFloat2 = new RangeFloat(-10f, 10f, -5f, 5f);
Adjustable order list
// Parameters:
// elementTitleVar: the name of the variable to be displayed as the title
/// <summary>
/// Display name will use "i" value
/// </summary>
[BInspector.Reorderable("i")]
public CustomClass[] customClassAry = new CustomClass[0];
[Serializable]
public class CustomClass
{
public int i = -1;
public bool b = false;
public float f = 0f;
}
Change string display style
// The String Style Type determines the display mode of the string:
// Default: Default style. No difference from the original interface.
// FilePath: File search mode. Will open the file search window.
// FolderPath: Folder search mode. Will open the folder search window.
// Password: Cipher mode.
// Tag and Category: Tag and custom drop-down menu mode.
[StringStyle(StringStyleType.Default)]
public string defaultString = "Default Style";
[StringStyle(StringStyleType.FilePath)]
public string filePath = "D:/test.sln";
[StringStyle(StringStyleType.FolderPath)]
public string folderPath = "D:/";
[StringStyle(StringStyleType.Password)]
public string password = "I'm Password.";
[StringStyle(StringStyleType.Tag)]
public string tagString = "Untagged";
[StringStyle("Speed/Low", "Speed/High")]
public string categoryString = "Speed/High";
Create the ScriptableObject directly on the Inspector of the code file, without the need to write an additional function to add the ScriptableObject.
Display the file location on the ScriptableObject for quick access to the file path.
Add a preview button to the drag-and-drop field to preview the object parameter settings.