2016-03-18 4 views
0

Gibt es eine Möglichkeit, eine Klasse mit einigen Dictionary<string, CustomStruct>Unity XML-Serialisierung von komplexer Klasse

innen zu serialisiert? Scheint, dass ich selbst einen Serialisierer implementieren muss, weil Unity meldet, dass Dictionary aufgrund der IDictionary-Implementierung nicht serialisiert werden kann.

Teil meiner Klasse ist dies:

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; } 
... 

Antwort

1

Versuchen Sie, die [Serializable] und die [SerializeField] Attribute zu verwenden.

[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; } 
... 

Nicht getestet, aber das sollte funktionieren.

EDIT:

umfassen System.Runtime.Serialization Verwenden [DataContract] und [DataMember]. Initialisieren Sie das Dictionary im Konstruktor mit noParameter.

Dies funktioniert für Standard-C#. Ich weiß nicht, ob es in Unity funktioniert.

[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

gleichen Fehler: NotSupportedException: Der Typ System.Collections.Generic.Dictionary'2 [[System.String Mscorlib, Version = 2.0.0.0, Culture = neutral, PublicKeyToken = b77a5c561934e089], [Test + Stat , Assembly-CSharp, Version = 0.0.0.0, Culture = Neutral, PublicKeyToken = null]] wird nicht unterstützt, da IDictionary implementiert wird. – smark91

+0

Probieren Sie die zweite Methode, die ich in Bearbeiten bereitgestellt habe. Das könnte für dich funktionieren. – Programmer

+0

Fehler immer noch der gleiche :( – smark91