Parse ini configuration file ---- INIParser, Unity ---- iniparser in unity

Source: Internet
Author: User
Tags open ini file

Parse ini configuration file ---- INIParser, Unity ---- iniparser in unity

Hello everyone, I am Sun Guangdong. Reprinted please indicate the source: http://blog.csdn.net/u010019717

More comprehensive content please see my game pretty cool address: http://www.unitymanual.com/space-uid-18602.html

Ini files

This library can process INI files. Please refer to the extension name of the file (for example, .txt) as long as the file content is in the correct format.

[Player]name=Arnoldavatar=2; This section stored hi-scores[Hi-score]Top1=32900Top2=12000Top3=4700

How can I use this database?

1. Add "INIParser. cs" to Unity.
2. Declare An INIParser object and use it.

INIParser ini = new INIParser();ini.Open(“C:/Test.ini”);ini.WriteValue(“Player”,“Name”,“Arnold”);ini.Close();
When multiple INI files exist

Note that for each INIParser instance, you can only have one open INI file at any time. You can open the next INI file, but you must use Close () before ().

INIParser ini = new INIParser();ini.Open(“C:/Test.ini”);ini.WriteValue(“Player”,“Name”,“Arnold”);ini.WriteValue(“Hi-score”,“Top3”,1000);ini.Close();ini.Open(“C:/Test2.ini”);ini.WriteValue(“Position”,“x”,2);ini.WriteValue(“Position”,“y”,3);ini.Close();


Methods Method

Open (string path)

Open ini_file about reading and writing. If this file does not exist, it will be created .. Once you have completed reading/writing, remember to call the Close () function (). To save all changes to the INI file.

Open (TextAsset asset)

Open a TextAsset as ini_file. If any changes are made, the copy will be saved in the Path of Persistent Data Path Persistent Data. This function will always look at the Persistent Data Path of the Persistent Data Path. If there is a copy of any modified TextAsset, you will first see the change in the Persistent Data Path of the Persistent Data Path before viewing the text resource package in the game.


OpenFromString (string str)

Create an INI file from the string and open it for reading/writing. The correctly formatted string is used as the INI file (I .e., sections section, keys key and values value). Otherwise, the INI file cannot be created correctly. Note that this INI file is temporary and only exists in the memory. However, you can use the string returned by ToString () to save the complete INI file to the server or disk.


String ToString (string str)

Returns the complete ini file string.


Close ()

This method should be called once you have read or written any opened INI file. The INI file data is stored in the memory until this method is called, and the data is written to the disk.


String ReadValue (string section, string key, string default)

(Overload: bool, int, long, double, byte [], DateTime)

Read the value from ini_file. If the value does not exist, (default) is returned.


WriteValue (string section, string key, string value)
(Overload: bool, int, long, double, byte [], DateTime)
Write a value to ini_file

SectionDelete (string section)

Delete the section of the entire INI file, which also deletes all associated key/value pairs.

Bool IsSectionExists (string section)

Check whether the section in the INI file exists. You do not need to check to prevent errors, because if your ReadValue is from a section that does not exist, ReadValue will only return the default value. However, sometimes it can be useful if the INI file has stored specific data.

KeyDelete (string section, string key)

Delete the selected key (and its related value) from the ini file.

Bool IsKeyExists (string section, string key)

Check whether a specified key exists in the INI file. You do not need to check to prevent errors, because if your ReadValue does not exist, ReadValue will only return the default value. However, sometimes it can be useful if the INI file has stored specific data.

Open (TextAsset asset)

TextAsset is read-only, so any modification is placed in the sandbox area (persistentDataPath ).

Example code:

INIParser ini = new INIParser();TextAsset asset = Resource.Load("TextAssetExample") as TextAsset;ini.Open(asset);ini.WriteValue("Player","Name","Arnold");ini.Close();

Sometimes, you want to use TextAsset text resources as INI files. The game package contains TextAsset text resources, So it provides reliable read/write operations on each platform. If you use the assets of the streaming assets stream as the INI file, sometimes you reach the mobile platform with incorrect read/write permissions. Make sure that the TextAsset text resource exists, otherwise any read/write operations will not work correctly.

Credits

Library is adapted from STA INIFile and modeled after the INIFile System of the game production room.



Example code:

Save and load game data

INIParser ini = new INIParser();// Open the save file. If the save file does not exist, INIParser automatically create// oneini.Open(Application.persistentDataPath + "save.txt");// Read the score. If the section/key does not exist, default score to 10int score = ini.ReadValue("Player","Score",10);score += 100;ini.WriteValue("Player","Score",score);ini.Close();

What will happen during the first running of this competition?

This Code reads the score from the saved file, increases by 100, and saves the new score.

Open(alibaba cloud will detect that the save.txtworkflow does not exist, and the blank space save.txt will be created. The score is then read. "Save.txt" is blank, and the score cannot be found in the INI file. Therefore, the default value is 10. Then, the new score value is written into the INI file.


Save and load game data from the TextAsset File

INIParser ini = new INIParser();TextAsset asset = Resource.Load("TextAssetExample") as TextAsset;ini.Open(asset);int score = ini.ReadValue("Player","Score",10);score += 100;ini.WriteValue("Player","Score",score);ini.Close();


Sometimes, you want to use TextAsset text resources as INI files. The game package contains TextAsset text resources, So it provides reliable read/write operations on each platform. If you use the assets of the streaming assets stream as the INI file, sometimes you reach the mobile platform with incorrect read/write permissions. Make sure that the TextAsset text resource exists, otherwise any read/write operations will not work correctly.



Related Article

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.