Wednesday 18 April 2012

INISystem: Updated Tutorial


So with the 2.0 release of INISystem there comes a bunch of breaking changes code side. So this is an updated tutorial showing some basic usage.

Loading an .ini file called config, that doesn't require Unity specific DataTypes.
INIContent config;
try
{
 config = INIFile.Read("config.ini"); // Reads the file from disk.
}
catch (System.IO.FileNotFoundException ex)// INIFile.Read throws a FileNotFound exception when the file doesn't exist.
{
 // You can now place any handling of that, such as creating an INI file.
 config = new INIContent();
 INIFile.Write(ex.FileName, config);
}
Loading an .ini file called config, that requires Unity specific DataTypes (e.g. Color, Vector2)
INIUnity unityConfig;
try
{
 config = INIFile.Read("config.ini").ToINIUnity(); // Reads the file from disk.
}
catch (System.IO.FileNotFoundException ex)// INIFile.Read throws a FileNotFound exception when the file doesn't exist.
{
 // You can now place any handling of that, such as creating an INI file.
 config = new INIUnity();
 INIFile.Write(ex.FileName, config);
}
To get an entry from the INI file it is as simple as it was before, even simpler when using Unity DataTypes. (The following assumes you have loaded an INI file by the previous method for Unity Specific.
// To load a string
string text = config.GetString("text", "General", "Default text, if no text is found");
// To load an integer
int number = config.GetInt("number", "General", );
// To load a Unity specific DataType (Color)
Color hudColor = config.GetColor("HUDColor", "General", Color.White);
// To load a Unity specific DataType (Vector2)
Vector2 menuLocation = config.GetVector2("menuLocation", "General", new Vector2(10,10));
The following is used to change an INI entry. (Assumes that the second loading method was used to set up a config object of type INIUnity)
// Changing a string value
config.Change("text", "New text that will now be loaded", "General");
// Changing a integer value
config.Change("number", 5, "General");
// Changing a Unity specific DataType (Color)
config.Change("hudColor", Color.Green, "General");
// Changing a Unity specific DataType (Vector2)
config.Change("menuLocation", new Vector2(50,180), "General");
Finally to save the changes back to file, it is fairly simple. (The following assumes that you have followed the previous example and made changes to the config)
// Saves the changes to file.
if (INIFile.Write("config.ini", (INIContent)config))
 Debug.Log("File saved successfully!");
else
 Debug.Log("Error encountered while trying to save config file");
I hope this tutorial has enlightened you to the changes in version 2.0. You can purchase INISystem from Unity's Asset Store at: http://u3d.as/content/corrupted-smile-studio/inisystem

Regards, Garth

No comments:

Post a Comment