嘗試使用[Serializable]
和[SerializeField]
屬性。
[Serializable]
public class Ship
{
public struct Stat
{
public int StatValue { get; set; }
public int StatLast { get; set; }
public Stat(int statValue, int statLast)
{
StatValue = statValue;
StatLast = statLast;
}
}
public int a = 0;
public string Name { get; set; }
public string Owner { get; set; }
[SerializeField]
public Dictionary<string, Stat> Aim { get; set; }
[SerializeField]
public Dictionary<string, Stat> Dodge { get; set; }
[SerializeField]
public Dictionary<string, Stat> EmPower { get; set; }
[SerializeField]
public Dictionary<string, Stat> HullPoint { get; set; }
[SerializeField]
public Dictionary<string, Stat> CorePower { get; set; }
[SerializeField]
public Dictionary<string, Stat> Reaction { get; set; }
public string Size { get; set; }
public int CurrentHullPoint { get; set; }
public int CurrentCorePower { get; set; }
...
未測試,但應該工作。
編輯:
包括System.Runtime.Serialization
使用[DataContract]
和[DataMember]
。在構造函數中初始化字典否參數。
這適用於標準的C#。不知道它是否會在Unity中運行。
[DataContract]
public class Ship
{
// Must use parameterless constructor for serialization
public Ship()
{
Aim = new Dictionary<string, Stat>();
Dodge = new Dictionary<string, Stat>();
EmPower = new Dictionary<string, Stat>();
HullPoint = new Dictionary<string, Stat>();
CorePower = new Dictionary<string, Stat>();
Reaction = new Dictionary<string, Stat>();
}
public struct Stat
{
public int StatValue { get; set; }
public int StatLast { get; set; }
public Stat(int statValue, int statLast)
{
StatValue = statValue;
StatLast = statLast;
}
}
public int a = 0;
public string Name { get; set; }
public string Owner { get; set; }
[DataMember]
public Dictionary<string, Stat> Aim { get; set; }
[DataMember]
public Dictionary<string, Stat> Dodge { get; set; }
[DataMember]
public Dictionary<string, Stat> EmPower { get; set; }
[DataMember]
public Dictionary<string, Stat> HullPoint { get; set; }
[DataMember]
public Dictionary<string, Stat> CorePower { get; set; }
[DataMember]
public Dictionary<string, Stat> Reaction { get; set; }
public string Size { get; set; }
public int CurrentHullPoint { get; set; }
public int CurrentCorePower { get; set; }
...
同樣的錯誤: NotSupportedException異常:類型System.Collections.Generic.Dictionary'2 [[System.String,mscorlib程序,版本= 2.0.0.0,文化=中性公鑰= b77a5c561934e089],[試驗+統計,Assembly-CSharp,Version = 0.0.0.0,Culture = neutral,PublicKeyToken = null]]不支持,因爲它實現了IDictionary。 – smark91
嘗試我在編輯中提供的第二種方法。這可能對你有用。 – Programmer
錯誤仍然相同:( – smark91