Ngui implementing role information storage or archiving
Source: Internet
Author: User
Using XML plus Ngui to implement role information storage or archiving, okay, no more talking about first, we start to do the preparatory work, this tutorial uses NGUI3.0.6.
The first step: first import Ngui, then create a new 3 folder scene, script, prefabs respectively for the scene, scripts, presets, and then use Ngui creating a 2DUI diagram:
The second step: we start to write the first script, as the XML file stored data structure, a new C # script, named Xmldatastructure, here is a simple definition of character information (in fact, it must be very complex, such as the player's position, current blood volume, skill level, etc., These people can build their own, here mainly provide a train of thought), I here only defines the 6 attribute variable code as follows: using System;
<summary>
XML data structure. Defines an XML storage structure that can be extended later
</summary>
public class Xmldatastructure {
<summary>
The identifier.id index, name, occupation, rank, remaining experience, creation time
</summary>
public int id;
public string name;
public string Occupation;
Public Int. level;
public int freeexp;
public string Createtime;
<summary>
Initializes a new instance of the <see cref= "Xmldatastructure"/> class. Constructors
</summary>
<param name= "_id" >_id.</param>
<param name= "_name" >_name.</param>
<param name= "_occupation" >_occupation.</param>
<param name= "_level" >_level.</param>
<param name= "_freeexp" >_freeexp.</param>
<param name= "_createtime" >_createtime.</param>
Public Xmldatastructure (String _name, string _occupation, int _level, int _freeexp, string _createtime) {
name = _name;
occupation = _occupation;
level = _level;
Freeexp = _freeexp;
Createtime = _createtime;
}
<summary>
Initializes a new instance of the <see cref= "Xmldatastructure"/> class. Empty constructors
</summary>
Public Xmldatastructure () {
}
The code to copy the code is simply a simple class that holds the information of the character, which you can expand yourself.
Then create a second script named Xmlmanager, which is the core class for manipulating XML files, and all of the XML operations are in this class, which has several important functions:
Addrole--> This function is responsible for writing the player data to the XML file
Clearxmlfile--> This function is responsible for emptying the contents of the XML file, that is, to clear all role information (with caution)
Refreshxmlid--> This function is responsible for re-ordering the role ID in the XML file, mainly in the XML when the role is deleted when the use of
Getdatafromxml--> This function reads the character or archive information from an XML file, and the result is stored in a linear table
Removeitembyid--> This function is to delete the role information of the specified ID from the XML file
The code is as follows: using System.IO;
Using System.Xml;
Using Unityengine;
Using System;
Using System.Collections.Generic;
public class Xmlmanager {
public enum xmlrands{
Xml_role,
Xml_save
};
private static string Xmldir_path = Application.datapath + "/xmldir";
Private XmlDocument _xmldoc = new XmlDocument ();
Public list<xmldatastructure> datasetlist;
Public Xmlmanager () {
Init ();
}
<summary>
Init this instance. Detect if a file exists
</summary>
void Init () {
if (! Directory.Exists (Xmldir_path)) {
Directory.CreateDirectory (Xmldir_path);
}
Checkandcreatexml (Xmlrole_path,xmlrole_root);
Checkandcreatexml (Xmlsave_path,xmlsave_root);
}
<summary>
Checks the and create XML. Determine if the XML exists, create one without being present, and create the root node at the same time
</summary>
<param name= "_name" >_name.</param>
<param name= "_root" >_root.</param>
void Checkandcreatexml (String _name, String _root) {
if (! File.exists (_name)) {
File.create (_name);
XmlDocument Tempdoc = new XmlDocument ();
Tempdoc.load (_name);
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.