Version: 2020.2
언어: 한국어

Serializable

매뉴얼로 전환

설명

Indicates that a class or a struct can be serialized.

To enable serialization, apply the [Serializable] attribute. For more information on serialization, see Script Serialization.

Note: Only non-abstract, non-generic custom classes can be serialized.

In the following example we create a custom Player struct and give it the [Serializable] attribute to make it serializable. We then create a private field of the Player type and apply [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; }