Represents an array, list, struct or class.
The Generic type is used for compound types that are serialized in-line:
- Arrays and lists.
- Structs that are user defined (e.g. not a built in Unity type).
- Classes that are serialized 'by value' (e.g. referenced without the SerializeReference attribute).
- Certain built in Unity structs, when they do not have dedicated SerializedPropertyType enum value. For example RectOffset is Generic, but Vector3 is SerializedPropertyType.Vector3.
Note: The term Generic, when used as a SerializedProperty type, should not be confused with the unrelated C# feature of Generic classes and methods.
See Also: SerializedProperty.isArray, SerializedPropertyType.ManagedReference, SerializedPropertyType.ObjectReference
using System; using System.Text; using UnityEngine; using UnityEditor; using System.Collections.Generic;
namespace GenericObjectTypeExample { [Serializable] public struct ExampleStruct { public int m_Field; };
[Serializable] public class ExampleClass { public int m_Field; };
public class GenericTypeExample : ScriptableObject { // All these fields will be serialized public int[] m_ArrayOfInt; public List<int> m_ListOfInt; public ExampleStruct m_ExampleStruct = new ExampleStruct() { m_Field = 1 }; public ExampleClass m_ExampleClass = new ExampleClass() { m_Field = 2 };
[SerializeReference] public ExampleClass m_ManagedClass = new ExampleClass() { m_Field = 3 };
[MenuItem("Example/SerializedPropertyType Generic Example")] static void GenericExample() { var scriptableObject = ScriptableObject.CreateInstance<GenericTypeExample>(); using (var serializedObject = new SerializedObject(scriptableObject)) { var report = new StringBuilder();
// Generic ReportType(report, serializedObject, "m_ArrayOfInt"); ReportType(report, serializedObject, "m_ListOfInt"); ReportType(report, serializedObject, "m_ExampleStruct"); ReportType(report, serializedObject, "m_ExampleClass");
// Not Generic ReportType(report, serializedObject, "m_ManagedClass");
Debug.Log(report.ToString());
AccessGenericValues(serializedObject); } }
static void AccessGenericValues(SerializedObject serializedObject) { // "generic" type struct and objects can be retrieved directly with boxedValue ExampleStruct structValues = (ExampleStruct)serializedObject.FindProperty("m_ExampleStruct").boxedValue;
// Alternatively individual fields can be read int fieldInStruct = serializedObject.FindProperty("m_ExampleStruct.m_Field").intValue;
Debug.Log($"Value of field in struct: {structValues.m_Field}, Value of field direct read: {fieldInStruct}");
// Similarly boxedValue supports writing to an inline class SerializedProperty inlineClass = serializedObject.FindProperty("m_ExampleClass");
// Serialize new state in a single call inlineClass.boxedValue = new ExampleClass() { m_Field = 4 };
// Individual fields can also be accessed Debug.Log($"Value of field in class after write: {inlineClass.FindPropertyRelative("m_Field").intValue}");
serializedObject.ApplyModifiedProperties(); }
static void ReportType(StringBuilder report, SerializedObject serializedObject, string propertyPath) { var serializedProperty = serializedObject.FindProperty(propertyPath); report.AppendLine($"{propertyPath} has type {serializedProperty.propertyType}"); } } }
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.