Resolve INI configuration file----Iniparser in Unity

Source: Internet
Author: User
Tags open ini file save file

??

Hello, I am Sun Guangdong. Reprint Please specify source: http://blog.csdn.net/u010019717

more full content please see my game bull's address: http://www.unitymanual.com/space-uid-18602.html ??

Ini files

This library can handle INI files. Note that the file can be any extension (such as. txt) as long as the contents of the file are in the correct format.

[player]name=arnoldavatar=2; This section stored hi-scores[hi-score]top1=32900top2=12000top3=4700

So how do you use this library?

1. Add "INIParser.cs" to Unity.
2. Declare a 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

Please note that for each Iniparser instance, you can only have one open INI file at any one time, you may 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", "n"); 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 finished reading/writing remember to call Function Close (). To save all changes to this INI file.

Open (Textasset Asset)

Open a textasset as Ini_file. If you make any changes, the copy will be saved under the path of the persistent data path persistence. This function will always be looking at the persistent data path persistent paths, if there are any modifications to the Textasset copy, actually look at the game in the text resource bundle before you first see the changes in the persistent data path persistence.


Openfromstring (String str)

Creates an INI file from a string and opens it for read/write. A properly formatted string as an INI file (that is, the sections section, keys, and values) will not create the INI file correctly. Note that this INI file is temporary and only exists in memory. But you can use ToString () to return the string that can be saved to the server or to the disk's full INI file.


String ToString (String str)

Returns the full INI file string.


Close ()

This method should be called once you have finished reading or writing to any open INI file. INI file data is stored in memory until this method is called, and this data is written to disk.


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

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

Reads the value from the Ini_file. If the value does not exist, (the default value) 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)

Deletes the section portion of the entire INI file, which also removes all key/value pairs associated with it.

BOOL Issectionexists (string section)

Check for the presence of section sections in the INI file. You do not need to check to prevent errors, because if you readvalue from a non-existent section stanza, ReadValue will only return the default value. However, sometimes it can be useful if the INI file has been saved for specific data.

Keydelete (string section, string key)

Delete the selected key (and the value associated with it) from the INI file.

BOOL Iskeyexists (string section, string key)

Check to see if there is a specified key present in the INI file. You do not need to check to prevent errors, because if you readvalue a non-existent section, ReadValue will only return the default value. However, sometimes it can be useful if the INI file has been saved for specific data.

Open (Textasset Asset)

Textasset is read-only, so any modifications are 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 will want to use the Textasset text resource as an INI file. The game pack contains textasset text resources, so it is reliable for read/write operations on each platform. If you use streaming assets streaming assets as an INI file, sometimes you will reach the error on the read/write permission on the mobile platform. You must make sure that the Textasset text resource exists, otherwise any read/write operation will not work correctly.

Credits

The library is adapted from STA inifile, modeled after the game Production room INI file system.



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"), score + = 100;ini. WriteValue ("Player", "score", score); Ini. Close ();

What happens the first time this game is run?

This code reads the score from the save file, adds 100, and saves the new score value.

Open () will detect that "save.txt" does not exist, so a blank "Save.txt" will be created. Then, the score is read. Since "Save.txt" is blank, the score cannot be found in the INI file, so it defaults to 10. Then, the value of the new score is written to 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"), score + = 100;ini. WriteValue ("Player", "score", score); Ini. Close ();


Sometimes you will want to use the Textasset text resource as an INI file. The game pack contains textasset text resources, so it is reliable for read/write operations on each platform. If you use streaming assets streaming assets as an INI file, sometimes you will reach the error on the read/write permission on the mobile platform. You must make sure that the Textasset text resource exists, otherwise any read/write operation will not work correctly.



Resolve INI configuration file----Iniparser in Unity

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.