Unity 序列化 当我用父类引用子类的实例时,序列化只会保存父类的数据。解决这种问题只能使用Json序列化继承unity的序列化接口,用一个字符串保存真正的值,在序列化之前和序列化之后,对这个字符串进行还原。代码如下 [Serializable] public class VFXStageSetting : ISerializationCallbackReceiver { public string effectType; public VFXAction action; public bool edit; [SerializeField] public string dataStr; IReadOnlyDictionary<string, Type> types = (from assembly in AppDomain.CurrentDomain.GetAssemblies () from type in assembly.GetTypes () where type.IsSubclassOf (typeof (VFXAction)) let attribute = type.GetCustomAttribute<EffectActionAttribute> (false) select (attribute.showName, type)).ToReadOnlyDictionary (p => p.showName, p => p.Item2); void ISerializationCallbackReceiver.OnAfterDeserialize () { action = (VFXAction)JsonConvert.DeserializeObject (dataStr, types[effectType]); } void ISerializationCallbackReceiver.OnBeforeSerialize () { dataStr = JsonConvert.SerializeObject (action); } }