C# 在获取要序列化的类时遇到问题
我正在使用一个xml序列化程序,我正在尝试序列化一个类。xml为下面的类提供了一个空节点。当我将键和值列表设置为public时,unity将冻结C# 在获取要序列化的类时遇到问题,c#,xml,serialization,C#,Xml,Serialization,我正在使用一个xml序列化程序,我正在尝试序列化一个类。xml为下面的类提供了一个空节点。当我将键和值列表设置为public时,unity将冻结 using System; using System.Collections; using System.Collections.Generic; using UnityEngine; namespace Grimoire.Tools.CerealTools { [Serializable] public class CerealDic
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace Grimoire.Tools.CerealTools
{
[Serializable]
public class CerealDictionary<Key, Value> //: IEnumerable
{
[SerializeField]
private List<Key> keys = new List<Key>();
[SerializeField]
private List<Value> values = new List<Value>();
public CerealDictionary(){}
public Value this[Key k]
{
get
{
int count = keys.IndexOf(k);
return values[count];
}//getters and setters for indexers :)
set
{
int count = keys.IndexOf(k);
if (count>-1){values[count]=value;}
else//if the index doesnt exist, then we should make a new one :)
{
values.Add(value);
keys.Add(k);
}
}
}
public void Add(Key key, Value value)
{
//if (keys.Contains(key)){return;}
keys.Add(key);
values.Add(value);
}
public void Remove(Key key)
{
if (!keys.Contains(key)){return;}
int index = keys.IndexOf(key);
keys.RemoveAt(index);
values.RemoveAt(index);
}
public bool TryGetValue(Key key, out Value value)
{
if (keys.Count != values.Count)
{
keys.Clear();
values.Clear();
value = default(Value);
return false;
}
if (!keys.Contains(key))
{
value = default(Value);
return false;
}
int index = keys.IndexOf(key);
value = values[index];
return true;
}
public bool TryGetKey(Value value, out Key key)
{
if (keys.Count != values.Count)
{
values.Clear();
keys.Clear();
key = default(Key);
return false;
}
if (!values.Contains(value))
{
key = default(Key);
return false;
}
int index = values.IndexOf(value);
key = keys[index];
return true;
}
public bool getGroup(Key key, out List<Value> thisValues)
{
thisValues = new List<Value>();
if (keys.IndexOf(key)==-1){return false;}
foreach(Key keyTemp in keys)
{
int index = keys.IndexOf(keyTemp);
thisValues.Add(values[index]);
}
return true;
}
public void ChangeValue(Key key, Value value)
{
if (!keys.Contains(key)){return;}
int index = keys.IndexOf(key);
values[index] = value;
}
public int length()
{
return keys.Count;
}
// IEnumerator IEnumerable.GetEnumerator()
// {
// return getEnumerator(keys,values);
// }
// private Grimoire.Tools.CerealTools.CerealDictionaryEnumerator<Value> getEnumerator()
// {
// return new CerealDictionaryEnumerator<Value>(values);
// }
}
}
使用系统;
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
命名空间Grimoire.Tools.CerealTools
{
[可序列化]
公共类字典//:IEnumerable
{
[序列化字段]
私有列表密钥=新列表();
[序列化字段]
私有列表值=新列表();
公共语言词典(){}
公共价值本[k键]
{
得到
{
int count=keys.IndexOf(k);
返回值[计数];
}//索引器的getter和setter:)
设置
{
int count=keys.IndexOf(k);
如果(计数>-1){values[count]=value;}
否则//如果索引不存在,那么我们应该创建一个新的:)
{
增加(价值);
添加(k);
}
}
}
公共无效添加(键、值)
{
//if(keys.Contains(key)){return;}
key.Add(key);
增加(价值);
}
公共无效删除(密钥)
{
如果(!keys.Contains(key)){return;}
int index=keys.IndexOf(key);
键。移除(索引);
移除值(索引);
}
public bool TryGetValue(Key-Key,out-Value)
{
if(keys.Count!=values.Count)
{
键。清除();
value.Clear();
值=默认值(值);
返回false;
}
如果(!key.Contains(key))
{
值=默认值(值);
返回false;
}
int index=keys.IndexOf(key);
值=值[索引];
返回true;
}
public bool TryGetKey(值,out Key)
{
if(keys.Count!=values.Count)
{
value.Clear();
键。清除();
key=默认值(key);
返回false;
}
如果(!values.Contains(value))
{
key=默认值(key);
返回false;
}
int index=值。IndexOf(值);
键=键[索引];
返回true;
}
public bool getGroup(Key-Key,out-List-thisValues)
{
thisValues=新列表();
if(keys.IndexOf(key)==-1){return false;}
foreach(钥匙中的钥匙温度)
{
int index=keys.IndexOf(keyTemp);
添加(值[索引]);
}
返回true;
}
public void ChangeValue(键、值)
{
如果(!keys.Contains(key)){return;}
int index=keys.IndexOf(key);
数值[指数]=数值;
}
公共整数长度()
{
返回键。计数;
}
//IEnumerator IEnumerable.GetEnumerator()
// {
//返回getEnumerator(键、值);
// }
//私有Grimoire.Tools.CerealTools.CerealDictionaryEnumerator getEnumerator()
// {
//返回新的CeralDictionaryEnumerator(值);
// }
}
}
这是序列化我的类的代码
using UnityEngine;
using System.Collections;
using System.Xml.Serialization;
using System.IO;
public class StringSerializer {//for object to string serialization
public static string convertToString<t>(t workObject)
{//changes object to string
XmlSerializer xmlSerializer = new XmlSerializer(workObject.GetType());
StringWriter stringWriter = new StringWriter();
xmlSerializer.Serialize(stringWriter,workObject);
return stringWriter.ToString();
}
public static t convertFromString<t>(System.Type buildType,string buildString)
{
TextReader stringReader = new StringReader(buildString);
XmlSerializer xmlSerlializer = new XmlSerializer(buildType);
return (t)xmlSerlializer.Deserialize(stringReader);
}
}
使用UnityEngine;
使用系统集合;
使用System.Xml.Serialization;
使用System.IO;
用于对象到字符串序列化的公共类StringSerializer{//
公共静态字符串convertToString(t workObject)
{//将对象更改为字符串
XmlSerializer XmlSerializer=新的XmlSerializer(workObject.GetType());
StringWriter StringWriter=新StringWriter();
序列化(stringWriter,workObject);
返回stringWriter.ToString();
}
公共静态t convertFromString(System.Type buildType,string buildString)
{
TextReader stringReader=新stringReader(构建字符串);
XmlSerializer xmlSerlializer=新的XmlSerializer(buildType);
返回(t)xmlSerlializer.Deserialize(stringReader);
}
}
这是正在序列化的类。它使用的类将出现一个空白节点
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;
[Serializable]
public class ExampleBoard : IBoardData {
public override List<Piece> pieces { get;set;}//position number, game piece
public override Grimoire.Tools.CerealTools.CerealDictionary<int,BookInformation> books {get;set;}//number of books, book number
public override Grimoire.Tools.CerealTools.CerealDictionary<int,int> rooms {get;set;}//position, room number. It details the relationship between positions and rooms
public override Grimoire.Tools.Vector2 boardDimensions{get;set;}
public override Grimoire.Tools.Vector2 boardSize{get;set;}
public override int bookTotal{get;set;}
public override int roomSize{get;set;}
public ExampleBoard(){}//serialization....sigh
public override void set()
{
boardDimensions = new Grimoire.Tools.Vector2(2,2);
boardSize = new Grimoire.Tools.Vector2(968,968);
roomSize = 484;
books = new Grimoire.Tools.CerealTools.CerealDictionary<int,BookInformation> ();
populateBooks ();
bookTotal = books.length();
pieces = new List<Piece> ();
populatePieces ();
rooms = new Grimoire.Tools.CerealTools.CerealDictionary<int, int> ();
populateRoom (boardSize);
}
public override void saveSize(Grimoire.Tools.Vector2 size)
{
populateRoom (size);
boardSize = getBoardSize(size);
boardDimensions = size;
}
private Grimoire.Tools.Vector2 getBoardSize(Grimoire.Tools.Vector2 size)
{
return new Grimoire.Tools.Vector2((float)(size.x*roomSize),(float)((size.y*roomSize)));
}
public override void save(){}// needs to be here.
private void populateRoom(Grimoire.Tools.Vector2 size)//position number, room number
{
int count = (int)(size.x*size.y);
double roomCount = 0;
int positionCount = -1;
for(int i=0;i<count;i++)
{
roomCount += 0.25;
positionCount +=1;
rooms.Add((int)roomCount,positionCount);
}
}
private void populateBooks()// number of books, book name.
{
books.Add (1,new BookInformation(9,0,1));
books.Add (2,new BookInformation(9,1,2));
books.Add (3,new BookInformation(9,2,3));
books.Add (4,new BookInformation(9,3,4));
books.Add (5,new BookInformation(9,4,5));
books.Add (6,new BookInformation(9,5,6));
books.Add (7,new BookInformation(9,6,7));
books.Add (8,new BookInformation(9,7,8));
books.Add (9,new BookInformation(9,8,9));
}
public override int getBookPosition(int bookNumber)
{
for(int i=1;i<books.length()+1;i++)
{
if (books[i].type==bookNumber){return books[i].key;}
}
return 0;
}
private void populatePieces()// piece position, piece. Change the pieces to something else later
{
pieces.Add (new Piece(1,"start",-1f));//start
pieces.Add (new Piece(1,"apprentice",-1.1f));//player
//_pieces.Add (new Piece(2,"apprentice",-1.1f));
pieces.Add (new Piece(7,"right wall",-1f));//a box maybe
pieces.Add (new Piece(8,"exit",-1f));//exit
pieces.Add (new Piece(13,"gargoyle",-1.1f));
}
public override int getRoomFromPosition(int position)
{
int room;
rooms.TryGetKey(position-1,out room);
return room;
}
}
使用UnityEngine;
使用系统集合;
使用System.Collections.Generic;
使用制度;
[可序列化]
公共类示例板:IBoardData{
公共覆盖列表块{get;set;}//位置号,游戏块
public override Grimoire.Tools.CerealTools.CerealDictionary图书{get;set;}//图书数量,图书编号
public override Grimoire.Tools.CerealTools.CerealDictionary rooms{get;set;}//位置,房间号。它详细说明了位置和房间之间的关系
public override Grimoire.Tools.Vector2 boardDimensions{get;set;}
public override Grimoire.Tools.Vector2 boardSize{get;set;}
公共重写int bookTotal{get;set;}
公共覆盖int roomSize{get;set;}
public ExampleBoard(){}//序列化…叹气
公共覆盖无效集()
{
板尺寸=新格里莫工具矢量2(2,2);
boardSize=新的Grimoire.Tools.Vector2(968968);
房间面积=484;
books=新Grimoire.Tools.CerealTools.CerealDictionary();
大众图书();
bookTotal=books.length();
工件=新列表();
大众电影();
房间=新Grimoire.Tools.CerealTools.CerealDictionary();
人口空间(板大小);
}
公共覆盖无效保存大小(Grimoire.Tools.Vector2大小)
{
人口空间(大小);
<?xml version="1.0" encoding="utf-16"?>
<Level xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<position>
<x>0</x>
<y>0</y>
<z>0</z>
</position>
<name>0</name>
<unlocked>true</unlocked>
<starTotal>0</starTotal>
<difficulty>0</difficulty>
<size>
<x>0</x>
<y>0</y>
</size>
<board xsi:type="ExampleBoard">
<pieces>
<Piece>
<position>1</position>
<name>start</name>
<z>-1</z>
</Piece>
<Piece>
<position>1</position>
<name>apprentice</name>
<z>-1.1</z>
</Piece>
<Piece>
<position>7</position>
<name>right wall</name>
<z>-1</z>
</Piece>
<Piece>
<position>8</position>
<name>exit</name>
<z>-1</z>
</Piece>
<Piece>
<position>13</position>
<name>gargoyle</name>
<z>-1.1</z>
</Piece>
</pieces>
<books />
<rooms />
<boardDimensions>
<x>2</x>
<y>2</y>
</boardDimensions>
<boardSize>
<x>968</x>
<y>968</y>
</boardSize>
<bookTotal>9</bookTotal>
<roomSize>484</roomSize>
</board>
</Level>
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Xml.Serialization;
[XmlInclude(typeof(ExampleBoard)),XmlInclude(typeof(LevelBoard))]
public abstract class IBoardData
{//this was an interface, but those cant be serialized easily
[XmlIgnore]public virtual List<Piece> pieces { get{throw new UnityException("Class doesn't inherit pieces");}set{}}//position number, game piece
[XmlIgnore]public virtual Grimoire.Tools.CerealTools.CerealDictionary<int,BookInformation> books { get{throw new UnityException("Class doesn't inherit books");}set{}}//number of books, book name
[XmlIgnore]public virtual Grimoire.Tools.CerealTools.CerealDictionary<int,int> rooms{ get{throw new UnityException("Class doesn't inherit rooms");}set{}}
public virtual void save(){throw new UnityException("Class doesn't inherit save");}
[XmlIgnore]public virtual Grimoire.Tools.Vector2 boardDimensions{get{throw new UnityException("Class doesn't inherit boardDimensions");}set{}}
[XmlIgnore]public virtual Grimoire.Tools.Vector2 boardSize{get{throw new UnityException("Class doesn't inherit boardSize");}set{}}
[XmlIgnore]public virtual int bookTotal{get{throw new UnityException("Class doesn't inherit bookTotal");}set{}}
public virtual int getRoomFromPosition(int pos){throw new UnityException("Class doesn't inherit getRoomPosition");}
[XmlIgnore]public virtual int roomSize{get{throw new UnityException("Class doesn't inherit roomSize");}set{}}
public virtual int getBookPosition(int bookNumber){throw new UnityException("Class doesn't inherit getBookPosition");}
public virtual void saveSize(Grimoire.Tools.Vector2 size){throw new UnityException("Class doesn't inherit saveSize");}
public virtual void set(){throw new UnityException("Class doesn't inherit set");}
}