public void Write(Vector3 vector){ if ((m_Index + 12) > m_Buffer.Length) Flush();
Write(vector.X); Write(vector.Y); Write(vector.Z); } public Vector3 ReadVector3(){ return new Vector3(ReadFloat(), ReadFloat(), ReadFloat()); }
Horší situace nastane pokud budeme chtít ukládat třeba třídu Model, Texture2D a další věci načítané pomocí Content Pipeline. Zde to lze snadno obejít, uložíme pouze jména souborů a ty potom běžně po proběhnutí serializace načteme. Uložit lze i obsah delegátů, ale o triku který jsem na ně vymylel napíši něco později. Jediné na co je potřeba myslet je to, že je nutné zapisovat a číst prvky ve stejném pořadí.
Jak používat tyto třídy opět v dalším článku.
In my last post about basic of serialization I wrote something about possible ways. In this post I want to introduce classes for reading and writing into binary files. I modified classes from RunUO 2.0 RC1 source, that is used as Ultima Online emulator on ŽvB project and add reading and writing code for some XNA classes. Code can be downloaded from here. Reading and writing those types is very simple. Vector2, Vector3 or Matrix are only couple of floats. Vector2 have two X and Y, Vector3 have three and Matrix is only set of 16 floats. Here is example of serialization of Vector3:
public void Write(Vector3 vector){ if ((m_Index + 12) > m_Buffer.Length) Flush();
Write(vector.X); Write(vector.Y); Write(vector.Z); } public Vector3 ReadVector3(){ return new Vector3(ReadFloat(), ReadFloat(), ReadFloat()); }
Texture2D, Model etc.. (basically all classes that are loaded by content pipeline) classes needs to be stored as strings. Simply write only file names of that assets and then load them by Content Pipeline. Only one thing is important. All parts needs to be written and read in same order.
In next article I will show you my code that handles saving and loading.
Žádné komentáře:
Okomentovat