将文本写入.ini文件时遇到问题

时间:2014-05-21 14:55:37

标签: c# file unity3d

我正在开发Unity 3D引擎中的一个小游戏项目。我正在尝试编写一个允许设置菜单将记录的设置写入.ini文件的类。但是,我似乎无法将myto写入文件。我想我是根据有关该主题的MSDN文章正确地编写了这个类。这是我的班级

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();
    }
}
}

以下是我对这堂课的要求:

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");
    }

我创建了这样的类的实例:

iniFile ini = new iniFile(@"..\settings\settingsTest.ini");

1 个答案:

答案 0 :(得分:0)

也许这不是你问题的确切答案,但你想用设置创建文件是什么?使用Unity3d内置功能来保存游戏首选项会不会容易得多,这也适用于跨平台?

如果您想尝试一下,请阅读PlayerPrefs