2016-03-18 56 views
0

有沒有辦法序列化一個類有一些Dictionary<string, CustomStruct>Unity XML序列化複雜類

裏面?似乎我需要自己實現一個序列化器,因爲Unity報告由於IDictionary實現而不可能序列化Dictionary。我班的

部分是這樣的:

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; } 
public Dictionary<string, Stat> Aim { get; set; } 
public Dictionary<string, Stat> Dodge { get; set; } 
public Dictionary<string, Stat> EmPower { get; set; } 
public Dictionary<string, Stat> HullPoint { get; set; } 
public Dictionary<string, Stat> CorePower { get; set; } 
public Dictionary<string, Stat> Reaction { get; set; } 
public string Size { get; set; } 
public int CurrentHullPoint { get; set; } 
public int CurrentCorePower { get; set; } 
... 

回答

1

嘗試使用[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; } 
... 
+0

同樣的錯誤: 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

+0

嘗試我在編輯中提供的第二種方法。這可能對你有用。 – Programmer

+0

錯誤仍然相同:( – smark91