Inline Property
Overview
The [InlineProperty]
attribute inlines the fields of a serializable class or struct, displaying them directly within the Inspector instead of requiring a nested view. This attribute allows for organized, inline data display, making it easier to manage complex data structures within the Inspector. The attribute can be applied at both the class/struct level and individual field level, with field-level decoration taking precedence.
Capabilities
The [InlineProperty]
attribute supports the following capabilities:
- Class/Struct and Field-Level Decoration:
- When applied to a class or struct,
[InlineProperty]
allows all instances of that type to be displayed inline. - To display a specific field inline, the field itself must also be decorated with
[InlineProperty]
, which takes precedence over the class-level attribute. This approach enables flexible configuration for both the entire type and individual instances.
- When applied to a class or struct,
- Custom Naming Modes: Offers different naming modes to adjust how fields are displayed inline:
- HeaderName: Displays a header for the inline property.
- PrependName: Prepends the field name to each inline field, useful when multiple instances of the same type are displayed inline.
- Default (No Naming): Displays inline fields without additional headers or prefixes.
- Supported Field Types: Compatible with various field types within the struct or class, including primitive types,
UnityEngine
types (e.g.,Vector3
,Color
), and custom serializable classes or structs.
Syntax and Examples
Note: These examples use Unity’s
[SerializeField]
onprivate
fields to promote encapsulation. This approach can help protect internal data from unintended access by other scripts and maintain clean, organized code. For more details, see Unity’s documentation on SerializeField.
Basic Syntax
[InlineProperty]
[SerializeField]
private MyCustomStruct inlineStruct;
Additional Constructor Syntax
// Specifying display mode (None, PrependName, or HeaderName)
[InlineProperty(InlinePropertyNameMode.PrependName)]
// Specifying a custom header name
[InlineProperty("Custom Header")]
// Specifying both display mode and custom header
[InlineProperty(InlinePropertyNameMode.PrependName, "Custom Header")]
// Specifying custom header and display mode, with arguments reversed
[InlineProperty("Custom Header", InlinePropertyNameMode.PrependName)]
Example 1: Decorating the Class with Custom Naming Mode, Overwritten at Field Level
In this example, [InlineProperty]
is applied both at the TestStruct
struct level with a custom header and at specific fields. The field-level decorations overwrite the default behavior defined at the struct level.
[System.Serializable]
[InlineProperty(InlinePropertyNameMode.HeaderName, "Custom Struct Header")]
public struct TestStruct
{
public int intField;
public float floatField;
public Vector3 vectorField;
}
// Field-level override with `PrependName` mode
[InlineProperty(InlinePropertyNameMode.PrependName)]
[SerializeField]
private TestStruct structWithPrepend;
Result: The Inspector displays structWithPrepend
inline, with each field name prefixed with “structWithPrepend_”. The header “Custom Struct Header” from the struct-level attribute is not applied here because the field-level [InlineProperty]
attribute overrides it.
Example 2: Applying Inline Property at Both the Class and Field Level with Default Behavior
Here, the individual field referencing this class uses [InlineProperty]
, ensuring the fields appear inline in the Inspector, even though the class is not decorated.
[System.Serializable]
public class TestClass
{
public bool boolField;
public Quaternion quaternionField;
}
[InlineProperty]
[SerializeField]
private TestClass defaultClass;
Result: The defaultClass
fields appear directly in the Inspector without any additional labels or headers, providing a clean and organized view.
Example 3: Using Class-Level Header with Field-Level Reference
In this example, the struct TestStruct
is decorated with [InlineProperty]
at the struct level, specifying a custom header. The field structWithHeader
references TestStruct
but does not provide any parameters, allowing it to inherit the header defined at the struct level.
[System.Serializable]
[InlineProperty(InlinePropertyNameMode.HeaderName, "Custom Struct Header")]
public struct TestStruct
{
public int intField;
public float floatField;
public Vector3 vectorField;
}
[InlineProperty]
[SerializeField]
private TestStruct structWithHeader;
Result: The Inspector displays structWithHeader
inline, using the “Custom Struct Header” specified at the struct level. Each field within TestStruct
(intField
, floatField
, and vectorField
) is displayed directly in the Inspector under this header without additional prefixes or labels, since no parameters were specified at the field level to override the struct’s configuration.