Version: 2023.1





要启用序列化,则应用 [Serializable] 属性。有关序列化的更多信息,请参阅脚本序列化


The following example creates a custom PlayerStats struct and gives it the [Serializable] attribute, which makes it serializable. It then creates a private field of type PlayerStats, and applies the [SerializeField] attribute to it, to make it show up in the Inspector.

using System;
using UnityEngine;

public class Player : MonoBehaviour { //Create a custom struct and apply [Serializable] attribute to it [Serializable] public struct PlayerStats { public int movementSpeed; public int hitPoints; public bool hasHealthPotion; }

//Make the private field of our PlayerStats struct visible in the Inspector //by applying [SerializeField] attribute to it [SerializeField] private PlayerStats stats; }