Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
3
Question by Cobradabest · Aug 05, 2013 at 02:26 PM · c#filedirectoryresolution settingsconfig

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

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

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Cobradabest · Aug 05, 2013 at 10:04 PM 1
Share

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?

avatar image Xtro · Aug 06, 2013 at 01:59 AM 1
Share

Try null ins$$anonymous$$d of string.Empty

avatar image Cobradabest · Aug 06, 2013 at 10:15 AM 1
Share

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?

avatar image Xtro · Aug 06, 2013 at 01:33 PM 1
Share

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 :)

avatar image
5

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image EmpirisoftNathan · Oct 15, 2017 at 03:24 AM 0
Share

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 :)

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

16 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Distribute terrain in zones 3 Answers

Multiple Cars not working 1 Answer

How do you check if a directory exists c#? 2 Answers

An OS design issue: File types associated with their appropriate programs 1 Answer

C# Getting only the Name of the XML File and not the Directory of the XML File 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges