[Unity3d] Read and write about Txt,xml,json files, and creation of external files (clips)

Source: Internet
Author: User
Tags text to json

Reprinted from Post bar: http://tieba.baidu.com/p/2825006787

I. Read and write about txt plain text
In fact, txt,.xml,.json such files are text files, similar to the. LRC lyrics files,. ini profiles,. reg registry files, and so on, such files are stored in text, but each has their own format, to facilitate the special recognition at some point, so read txt text is the most basic text parsing.
Static internal Read txt text: This is the simplest way to build textasset, directly drag the text file up, it can be used,
Public Textasset txtfile;
Print (txtFile.Text);
This is a string that gets the text directly, that is, the type string.
Note that the TXT text encoding format, should be changed to UTF-8 format, or not read out, although will not error. Dynamic internal read text: Put the TXT file in the Resources folder, using Resources.load loading
The above two ways of code:


Output Result:

External read text: The actual use of the two methods described above are not used much, most of the external reading, such as parsing a text and display on the screen.
External read text needs to import System.IO class, mainly use the class file class, the specific API please check CSDN, here is just a simple call to read. Be aware that the platform is on the PC.
I wrote a way to get the first few lines of text by passing parameters


The first parameter of this function is the path name of TXT, the second parameter is the number of lines, 0 words return "", File.ReadAllLines () method is to read the contents of txt text by path, the return value is sring[] array, the element is the content of each line. This is very simple. Similar to
File.readlines (filepath) is read by line, only one row at a time, but also more commonly used.
About the path and the absolute path and the relative path, the absolute path is not explained, that is, this "d:/mytest.txt", relative path to pay attention to, If using Application.datapath in addition to pay attention to the mobile platform, in particular, application.datapath in the editing environment and after the generation of EXE is not the same, the editing state, the return is asset location, after the generation assets no longer exist, the return location is _data folder, so if you want to load the txt text where the EXE is located, first handle the Application.datapath:
This method can be in the edit state output and Asset folder with the directory path, and generate EXE directory path, such as the following 2 images of the mytest text read can be used above this set of code.


The code above says these four ways:


Output Result:
It is not difficult to read the data in a plain text TXT file, with the emphasis being on the processing of the string that is taken out, such as a line, a character, a sequence, which we can use to record our game data, and sometimes quite useful. TXT text write: To write txt text can be file.writealllines (FILENAME,STR);
To modify a line of content, I wrote a simple function


Incoming parameters, txt path, which line, modified string
We can improve, I am aware of the number of rows, the situation beyond the number of people to think about how to change.
TXT text file creation:
Creating text is simple: File.createtext (fileName), which has only one path parameter, can get the path first and then determine if there is an if (! File.exists (FileName)) {
Files = File.createtext (fileName);
}
OK, about plain text read and write and create, write so much, feel enough detail, novice can understand it, we can create, read and write to use the text record data. Second, about the XML read and write about XML believe that everyone is using the most, this is a markup language, said simple point is tagged text, through these tags let us quickly find, modify, replace and other operations, easy to operate is the advantage of XML, is also the first to import using System.Xml; We mainly use this class of XmlDocument class, I myself casually wrote an XML, the content is as follows:


As simple as 4 primary nodes, each node has 3 sub-nodes below, let's read the contents of this XML


Above I use absolute path, relative path see above txt content, through getElementsByTagName directly get node, return type is XmlNodeList array, this array includes all the contents of this node, above this is output "mao_001" The contents of this node (the first) child node, output as

Note this [0]. Childnodes[i]. InnerText, the first 0 is the first element of the array, that is to find the first "mao_001", the next i is its sub-nodes, InnerText refers to the content under this node, if you change to name is the name of this node, you can output a try. If you want to output multiple nodes of the same name, you can


This is the output of all the "mao_001" nodes under the content. XML writes the same simple Xmldocument.createelement is the method of writing to the node, in addition RemoveChild remove a node, replacechild replace the child nodes, the specific other methods you can view msdn~ But we use XML is generally used to save a lot of regular data, to read the main, first read the data to understand it. Three About JSON read-write. json file is a lightweight data interchange format, it is more than plain text format, less than the XML and some markup, looks like the combination of arrays and hash table, the structure is very concise, mostly for network transmission, of course, the local storage of data is also possible, the same, the use of the first Litjson, this class is in the LitJson.dll, so we have to import this external class in advance, we use it to parse json,litjson.dll ourselves to download a line, parsing JSON is not difficult


This is the function that I parse a JSON write, the path is the same as those mentioned above, mainly through Jsonmapper.toobject (str) to convert the string to the jsondata we want to parse, with the jsondata can go directly above the data used, Here the JSON data is loaded with StreamReader, which is read through the path of the text, can be used as the previous reading of the text, and note that the parameters passed in is a string ... So in principle, we can change the TXT text to JSON format, read the data after the same can be used ~ ~ so not necessarily. JSON format file, and the network is transmitted to you is also a string, this JSON to note that the use of the WWW external loading of JSON data, Through the jsonmapper.toobject can not be converted to jsondata, the reason should be different encoding, www loaded after the code changed, can not read, how to use the WWW to read the JSON also need to further explore. Iv. creation of external files and folders the creation of external files in the day before yesterday mentioned a little, create text, create other files also using the file class, with the File.create (path) method as long as the suffix name can be used to create other files, such as Json,xml, It makes sense for us to have code to create a text class file, because you can't use code to describe a picture of a piece of music-Create a folder with unity:
Create folders with System.IO under the createdirectory, such as the use of unity screenshots, we want to have a folder in the software directory, specifically to store, this can be used to create a dynamic folder to implement, first determine whether there are folders, and then decide whether to create


This is a more useful function, Auto-save, folder location and name and image file name, wrote the function to dynamically named.

[Unity3d] Read and write about Txt,xml,json files, and creation of external files (clips)

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.