- Home /
How do I load a config (.cfg) or ini file?
I want my game to load a config (.cfg) or ini file from outside the game directory, and also have it save to it when you save game settings.
The reason I want to do this is because I want the to give people the option to fine tune the settings, and set it to things not available in the options. Many PC games do this, you can edit the config.cfg/config.ini file to add/remove graphic effects, and change the resolution if the game doesn't support the resolution you use, you can set it in the config file, I want to have a config file for my game to let people to the same thing.
I figured that if I loaded the file from outside the game folder, it will still be accessible to the user after I build the game, I would just have the move the to the build game's directory.
How would I go about doing this? Preferably in C#. I know how to get the game's directory, but I don't know much beyond that, and I never got any answers when I looked it up.
Answer by Xtro · Aug 05, 2013 at 02:42 PM
You can store your settings in on a prefab object(via a custom settings script on it) and you can serialize (generate text from it) and deserialize (load from text) the prefab object into/from the config file you created.
To serialize: http://answers.unity3d.com/questions/264347/serialization.html
NOTICE: This example is using binary serialization. You would want to convert it to text serialization.
To read/write a text file: http://www.csharp-station.com/howto/readwritetextfile.aspx
Okay, I looked it up, and found a way to serialize ini files, and the serialization works great, but the deserialization is a different story.
It saves just fine, although I have a bit of a problem concering the directory/file, but I can deal with that later. I'm always getting an error when trying to load what I saved.
I just used a string to (de)serialize for a test, here's my code:
public class SerializeINI
{
public static void Serialize(string name, string GamePath)
{
System.Text.StringBuilder sb = new System.Text.StringBuilder();
sb.AppendLine("name=" + name);
System.IO.StreamWriter writer = new System.IO.StreamWriter(GamePath);
writer.Write(sb.ToString());
writer.Close();
}
public static string DeSerialize(string name, string GamePath)
{
System.IO.StreamReader reader = new System.IO.StreamReader(GamePath);
string line;
while((line = reader.ReadLine()) != string.Empty)
{
string[] id_value = line.Split('=');
switch (id_value[0])
{
case "name":
name = id_value[1].ToString();
break;
}
}
reader.Close();
return name;
}
}
// Update is called once per frame
void Update ()
{
if (load)
{
if (!done)
{
name = SerializeINI.DeSerialize(name, gamePath);
done = true;
}
else
{
//Destroy(gameObject);
}
}
}
I'm getting an error saying: "NullReferenceException: Object reference not set to an instance of an object ConfigScript+SerializeINI.DeSerialize (System.String name, System.String GamePath) (at Assets/Temporary Assets/Scripts/ConfigScript.cs:47) ConfigScript.Update () (at Assets/Temporary Assets/Scripts/ConfigScript.cs:66)"
I'm assu$$anonymous$$g the numbers are meant to be what lines the error occurred, line 66 is where it's being called and line 47 is "string[] id_value = line.Split('=');"
I can't see anything wrong here, what am I doing wrong?
It works, thank you! One more question, when I tried to add a file name to my extension e.g. "\config.ini", I got an error saying "Assets/Temporary Assets/Scripts/ConfigScript.cs(26,30): error CS1009: Unrecognized escape sequence `\c'", so I tried ""/" + "config.ini", and I still got an error, how do I add a file at the end of an file path?
in path string use double back slash for one backslash. like:
"c:\\games\\wow\\config.ini"
or in C# you can do this:
@"c:\games\wow\config.ini"
don't forget the mark my post as an answer. Thank you :)
Answer by $$anonymous$$ · Feb 16, 2015 at 12:13 AM
I know this answer is late, but for others having the same problem: I wrote a .NET library specifically for CFG/INI files called SharpConfig.
Just a quick note here as if I found this, then others will too.
hokares' package is excellent and I very much recommend it.
The quick start here is perfect https://github.com/cemdervis/SharpConfig/wiki/How-to-quickly-get-this-up-and-running-in-Unity
Nice work dude you've saved me so much effort. If I somehow make a brazilian dollars I'll be back mate :)