所以有点像我以前的问题。我正在尝试保存一个蓝图,它只是游戏对象/实体的一堆设置。我现在将组件(及其设置)存储为一个 List<IEntityComponent>(IEntityComponent 是任何组件的接口),并包装在一个名为 ComponentTable 的类中。我只想序列化列表,所有私有的东西都没有序列化,只是为了更快的查找(以内存为代价)。这可以正确序列化,甚至可以反序列化而没有任何错误,但我注意到 componentTable 没有正确反序列化。
它创建了一个 ComponentTable 的实例,但从未实际向其中添加值。因此,它不是包含 CameraComponent、VelocityComponent 和 InputComponent 的 Component 表,它只是一个空的 ComponentTable。
{
"$types" : {
"ECS.Framework.Collections.Blueprint, Assembly-CSharp, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null" : "1",
"ECS.Features.Core.CameraComponent, Assembly-CSharp, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null" : "2",
"ECS.Features.Core.VelocityComponent, Assembly-CSharp, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null" : "3",
"InputComponent, Assembly-CSharp, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null" : "4"
},
"$type" : "1",
"Components" : [
{
"$type" : "2",
"Tag" : "MainCamera",
"Test" : "0, 0, 0",
"BackgroundColour" : "0, 0, 1, 1",
"ViewportRect" : "10, 10 : 10, 10",
"Orthographic" : false,
"FieldOfView" : 60,
"OrthoSize" : 5,
"Depth" : 0,
"OcclusionCulling" : true,
"HDR" : false,
"Enabled" : true
},
{
"$type" : "3",
"Enabled" : true,
"CurrentVelocity" : "0, 0, 0"
},
{
"$type" : "4",
"TEST" : 0,
"Enabled" : true
}
],
"Children" : [
],
"Parent" : ""
}
这就是它的保存方式,所以看起来它正在正确保存。我只控制向量、矩形和颜色的序列化/序列化,因为任何统一值类型都会导致错误。
我相信它正在正确序列化,但由于某种原因它没有反序列化到 componentTable 中。有谁知道 fastJSON 是否存在这种继承问题(使类从 List<customClass> 继承?
理想情况下,我会将它继承为 Dictionary< Type, IEntityComponent >,但 fastJSON 不会序列化 Type,只是将其保存为“System.Mono”,然后在序列化时会导致错误。
编辑:这是蓝图和组件表类
public sealed class Blueprint
{
public ComponentTable Components { get; private set; }
public List<string> Children { get; set; }
public string Parent { get; set; }
public Blueprint()
{
Components = new ComponentTable();
Children = new List<string>();
Parent = "";
}
public Blueprint(Blueprint _blueprint)
{
Children = new List<string>(_blueprint.Children);
Parent = _blueprint.Parent;
}
}
public class ComponentTable : List<IEntityComponent>
{
private Dictionary<Type, IEntityComponent> Components { get; set; }
#region Constructors
public ComponentTable()
{
Components = new Dictionary<Type, IEntityComponent>();
}
#endregion
#region Base Function Overrides
public void Add(Type _type)
{
if (Components.ContainsKey(_type))
return;
InternalAdd(_type, (IEntityComponent)Activator.CreateInstance(_type));
}
public new void Add(IEntityComponent _component)
{
InternalAdd(_component.GetType(), _component);
}
public void Add<T>() where T : IEntityComponent
{
Add(typeof(T));
}
private void InternalAdd(Type _type, IEntityComponent _component)
{
if (Components.ContainsKey(_type))
throw new InvalidOperationException("Component already contained");
Components.Add(_type, _component);
base.Add(_component);
}
public bool Remove(Type _type)
{
if (Components.ContainsKey(_type))
return InternalRemove(_type, Components[_type]);
return false;
}
public new bool Remove(IEntityComponent _component)
{
return InternalRemove(_component.GetType(), _component);
}
public bool Remove<T>() where T : IEntityComponent
{
return Remove(typeof(T));
}
private bool InternalRemove(Type _type, IEntityComponent _component)
{
if (!Components.ContainsKey(_type))
return false;
Components.Remove(_type);
return base.Remove(_component);
}
public IEntityComponent Get(Type _type)
{
if (Contains(_type))
return Components[_type];
return null;
}
public T Get<T>() where T : IEntityComponent
{
return (T)Get(typeof(T));
}
public bool TryGetValue(Type _type, out IEntityComponent _component)
{
return Components.TryGetValue(_type, out _component);
}
public bool TryGetValue<T>(out IEntityComponent _component) where T : IEntityComponent
{
return TryGetValue(typeof(T), out _component);
}
public bool Contains(Type _type)
{
return Components.ContainsKey(_type);
}
public new bool Contains(IEntityComponent _component)
{
return Contains(_component.GetType());
}
public bool Contains<T>() where T : IEntityComponent
{
return Contains(typeof(T));
}
#endregion
}