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
0
Question by webmaster94 · Jun 08, 2014 at 01:03 AM · menuwindowssettingsfiles

Trouble with writing text to a .ini file

I am working on a small game project and I am trying to write a class that will allow a settings menu to write the recorded settings to a .ini file. However, I can't seem to get it to write to the file. I think I wrote the class correctly to do it based upon the MSDN article on the subject. But it just doesn't seem to work. Here is my class:

 using System;
 using System.IO;
 using System.Runtime.InteropServices;
 using System.Text;
 using UnityEngine;
 
 //Used to allow other files to access the script by name
 namespace INI
 
 {
 public class iniFile {
     public string path;
 
     string winDir=System.Environment.GetEnvironmentVariable("windir");
 
     [DllImport("KERNEL32.DLL")]
     private static extern long WritePrivateProfileString(string section, string key, string value, string filePath);
     [DllImport("KERNEL32.DLL")]
     private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
 
     /// INIFile Constructor.
     public iniFile(string INIPath) {
         path = INIPath;
     }
     /// Write Data to the INI File
     public void IniWriteValue(string Section, string Key, string Value) {
         long success = WritePrivateProfileString(Section, Key, Value, this.path);
         Debug.Log (Section);
         Debug.Log (Key);
         Debug.Log (Value);
         Debug.Log (path);
         Debug.Log (success);
         return;
     }
 
     /// Read Data Value From the Ini File
     public string IniReadValue(string Section, string Key) {
         StringBuilder temp = new StringBuilder(255);
         int i = GetPrivateProfileString(Section,  Key, "", temp, 255, this.path);
         return temp.ToString();
     }
 }
 }

Here is my call for the class in the main menu script:

 if (GUI.Button (new Rect (Screen.width * guiGameplayApplyButtonPlaceX, Screen.height *    guiGameplayApplyButtonPlaceY, Screen.width * guiGameplayApplyButtonSizeX, Screen.height *     guiGameplayApplyButtonSizeY), "Apply")) {
         ini.IniWriteValue("Gameplay","Difficulty","Test");
         print ("Apply");
     }

I created the instance of the class like this:

 iniFile ini = new iniFile(@"..\settings\settingsTest.ini");
Comment
Add comment · Show 4
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 Benproductions1 · Jun 08, 2014 at 08:51 AM 0
Share

GetEnvironmentVariable("windir") and [DllImport("$$anonymous$$ERNEL32.DLL")] look extremely unportable. You also don't check for success and whatever i is supposed to be.

On that note, are you sure the folder that your supposed file is supposed to exist in also exists?

avatar image Graham-Dunnett ♦♦ · Jun 08, 2014 at 07:14 PM 0
Share

Why not use playerprefs?

avatar image webmaster94 · Jun 19, 2014 at 11:06 PM 0
Share

Sorry for the lag in response, I wasn't even sure if a moderator had finally approved this to be posted. To Graham Dunnett's question: I do not want to use playerPrefs because it uses the registry and using the registry is not the way I want the game to store settings. It makes it difficult to change settings quickly (without having to modify them in the code). I had planned on making a dev console to change settings that I don't want the player to access. ini files are what I am more familiar with.

To benproductions1 answer: I am not planning on porting the game to any other platform, so using that .dll file is not a problem for me. The folder does exist. However, that isn't a problem anyway because it will create the file if it doesn't exist already.

avatar image webmaster94 · Jun 20, 2014 at 03:40 PM 0
Share

Also to your question about checking for success. I do cheeck and just send it to the debug log. It checks here:

 long success = WritePrivateProfileString(Section, $$anonymous$$ey, Value, this.path);
         Debug.Log (Section);
         Debug.Log ($$anonymous$$ey);
         Debug.Log (Value);
         Debug.Log (path);
         Debug.Log (success);
 
 

This shows up in the class.

As far as the "i" variable goes, I am not exactly sure what it does. It was show in the $$anonymous$$SDN article as needing to be there. However, it is irrelevant. It is in the function for reading the file not writing.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Namey5 · Nov 24, 2014 at 10:23 AM

You could just use using System.IO, and write:

 File.WriteAllLines(*path you want*, string [] {"Gameplay", "Difficulty", "Test"});

Note I use JavaScript primarily, and in JavaScript, the line is just:

 System.IO.File.WriteAllLines(*path you want*, ["Gameplay", "Difficulty", "Test"])

I'm not too sure on using arrays in C# but I beleive that is how you do it.

Comment
Add comment · 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

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

24 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to make main menu including settings? 0 Answers

The application icon is always 32x32 - why? 1 Answer

How make a Settings Menu? 1 Answer

How to create a custom Game Settings window 0 Answers

Options Menu doesnt work after scene change 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