Version: 2022.1

Serializable

切换到手册

描述

指示可序列化的类或结构。

要启用序列化,则应用 [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; }
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961