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.
NOTICE: This example is using binary serialization. You would want to convert it to text serialization.
To read/write a text file:
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);
public static string DeSerialize(string name, string GamePath)
System.IO.StreamReader reader = new System.IO.StreamReader(GamePath);
while((line = reader.ReadLine()) != string.Empty)
string id_value = line.Split('=');
name = id_value.ToString();
// Update is called once per frame
void Update ()
name = SerializeINI.DeSerialize(name, gamePath);
done = true;
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 assuming 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?
Try null instead of string.Empty
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.
or in C# you can do this:
don't forget the mark my post as an answer. Thank you :)
Answer by hokares
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.
Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.
The best place to ask and answer questions about development with Unity.
To help users navigate the site we have posted a site navigation guide.
If you are a new user to Unity Answers, check out our FAQ for more information.
Make sure to check out our Knowledge Base for commonly asked Unity questions.
If you are a moderator, see our Moderator Guidelines page.
We are making improvements to UA, see the list of changes.
Answers and Comments
17 People are following this question.
Multiple Cars not working
Distribute terrain in zones
Creating a Directory in "My Documents"
Loading with binary formatter weird problem
How to save a file while connected to a server in the right dircetory