Three ways to implement unity-developed archive and read files

Source: Internet
Author: User
Tags serialization

This article is from the Siki Academy video, only for learning! Video link Address: http://www.sikiedu.com/course/129

Works with Unity 2017.3.0f3 (64-bit)

The old driver read the blog, understand the archive read the main realization way, only for reference, rookie can go to the end of the article to download the source code, or go to the above link to watch the video directly ....

First, create a save class to hold the data

[System.serializable]
public class Save
{
Public list<int> livingtargetpositions = new list<int> ();
Public list<int> livingmonstertypes = new list<int> ();

public int shootnum = 0;
public int score = 0;
}

Mode 1:2 binary method

Archive

private void Savebybin ()
{
  Serialization process (converting the Save object to a byte stream)
Create a Save object and save the current game state
Save save = Createsavego ();
  Create a binary formatter
BinaryFormatter bf = new BinaryFormatter ();
  Create a file stream
Path = Application.datapath + "/streamingfile" + "/bybin.txt";
FileStream FileStream = file.create (path);

  Serialize the Save object with the serialization method of the binary formatter, parameters: The file stream created and the object that needs to be serialized
Bf. Serialize (FileStream, save);
  Close the stream
Filestream.close ();

  Refresh Project project Files instantly
Assetdatabase.refresh ();
}

Read document

private void Loadbybin ()
{
Path = Application.datapath + "/streamingfile" + "/bybin.txt";
  Deserialization process
  Create a binary formatter
BinaryFormatter bf = new BinaryFormatter ();
  Open a file stream
FileStream FileStream = File.Open (Path,filemode.open);
  Invokes the formatter's deserialization method to convert the file stream to a Save object
Save save = BF. Deserialize (FileStream) as Save;
  Close file stream
Filestream.close ();
}

Way two: Xml

Archive

private void Savebyxml ()
{
Save save = Createsavego ();
  Create a storage path for an XML file
Path = Application.datapath + "/streamingfile" + "/byxml.xml";
  Creating an XML document
XmlDocument xmldoc = new XmlDocument ();
  Create a root node, which is the topmost node
XmlElement root = Xmldoc.createelement ("save");
  Set the value in the root node
Root. SetAttribute ("name", "SaveFile1");

XmlElement Target;
XmlElement targetposition;
XmlElement Monstertype;

for (int i = 0; i < Save.livingTargetPositions.Count; i++)
{
target = xmldoc.createelement ("target");
Targetposition = Xmldoc.createelement ("targetposition");
    Set the value of a node
Targetposition.innertext = Save.livingtargetpositions[i]. ToString ();
Monstertype = Xmldoc.createelement ("Monstertype");
Monstertype.innertext = Save.livingmonstertypes[i]. ToString ();

    Set the hierarchical relationship between nodes root-target-(Targetposition,monstertype)
Target. AppendChild (targetposition);
Target. AppendChild (Monstertype);
Root. AppendChild (target);
}

  Set the number of shots and fractional nodes and set the hierarchy relationship xmldoc--root--(Target,shootnum,score)
XmlElement shootnum = xmldoc.createelement ("Shootnum");
Shootnum.innertext = Save.shootNum.ToString ();
Root. AppendChild (Shootnum);

XmlElement score = xmldoc.createelement ("score");
Score. InnerText = Save.score.ToString ();
Root. AppendChild (score);

Xmldoc.appendchild (root);
Xmldoc.save (path);
Assetdatabase.refresh ();
}

Read document

private void Loadbyxml ()
{
Path = Application.datapath + "/streamingfile" + "/byxml.xml"; Save Save = new Save ();

  Loading an XML document
XmlDocument xmldoc = new XmlDocument ();
Xmldoc.load (path);

  Gets the element by the node name, the result is the XmlNodeList type
XmlNodeList targets = Xmldoc.getelementsbytagname ("target");
  Traverse all the target nodes of the node and get the innertext of child nodes and child nodes
if (targets. Count! = 0)
{
foreach (XmlNode target in targets)
{
XmlNode targetposition = target. Childnodes[0];
int targetpositionindex = Int. Parse (Targetposition.innertext);
    Store the resulting value in the Save
SAVE.LIVINGTARGETPOSITIONS.ADD (Targetpositionindex);

XmlNode Monstertype = target. CHILDNODES[1];
int monstertypeindex = Int. Parse (Monstertype.innertext);
SAVE.LIVINGMONSTERTYPES.ADD (Monstertypeindex);
}
}
XmlNodeList Shootnum = Xmldoc.getelementsbytagname ("Shootnum");
int shootnumcount = Int. Parse (Shootnum[0]. InnerText);
Save.shootnum = Shootnumcount;

XmlNodeList score = xmldoc.getelementsbytagname ("score");
int scorecount = Int. Parse (Score[0]. InnerText);
Save.score = Scorecount;

Setgame (save);
}

Way three: Json (Litjson)

Archive

private void Savebyjson ()
{
Save save = Createsavego ();
Path = Application.datapath + "/streamingfile" + "/byjson.json";
  Convert the Save object to a JSON-formatted string using Jsonmapper
String savejsonstr = Jsonmapper.tojson (save);
  Write this string to a file
Creates a StreamWriter and writes a string to the
StreamWriter SW = new StreamWriter (path);
Sw. Write (SAVEJSONSTR);
  Close Write Stream
Sw. Close ();
Assetdatabase.refresh ();
}

Read document

private void Loadbyjson ()
{
Path = Application.datapath + "/streamingfile" + "/byjson.json";
  Create a StreamReader to read the stream
StreamReader sr = new StreamReader (path);
  Assign the read stream to Savejsonstr
String savejsonstr = Sr. ReadToEnd ();
Sr. Close ();
  Convert a string to a Save object
Save save = jsonmapper.toobject<save> (SAVEJSONSTR);
Setgame (save);
}

Attach the project source code and Litjson Library, have time of children's shoes can go to Siki College Watch video, conscience recommendation, really good!

Link: https://pan.baidu.com/s/1IOa2Dw06tMSC-hlngAk5-w Password: glps

Three ways to implement unity-developed archive and read files

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.