unity 读取含有中文的配置文件

using System.IO;
using System.Runtime.InteropServices;
using System.Text;

/// <summary>
/// 读取ini配置文件
/// [Time] 
/// time=10 
/// [Speed] 
/// speed=5 
/// ConfigIni ini=new ConfigIni(Application.StreamingAssets+"/Setting.ini"); 
/// time=ini.ReadIniContent("Time","time");
/// speed=ini.ReadIniContent("Speed","speed");
/// ini.WritePrivateProfileString("Count","count","5");
/// </summary>
public class ConfigIni
{
    public string path;

    //ini文件的路径  
    public ConfigIni(string path)
    {
        this.path = path;
    }
    //这个读取中文路径的报错
    /*
    [DllImport("kernel32")]
    public static extern long WritePrivateProfileString(string section, string key, string value, string path);
    [DllImport("kernel32")]
    public static extern int GetPrivateProfileString(string section, string key, string deval, StringBuilder stringBuilder, int size, string path);
    [DllImport("User32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)]
    public static extern int MessageBox(IntPtr handle, String message, String title, int type);
    */
    
    //这个可以读取中文路径的
    [DllImport("kernel32", CharSet = CharSet.Unicode, SetLastError = true)]
    private static extern bool WritePrivateProfileString(
        string lpAppName, string lpKeyName, string lpString, string lpFileName);
    [DllImport("kernel32", CharSet = CharSet.Unicode, SetLastError = true)]
    private static extern int GetPrivateProfileString(
        string lpAppName, string lpKeyName, string lpDefault, StringBuilder lpReturnedString,
        int nSize, string lpFileName);

    //写入ini文件
    public void WriteIniContent(string section, string key, string value)
    {
        WritePrivateProfileString(section, key, value, this.path);
    }

    //读取Ini文件
    public string ReadIniContent(string section, string key)
    {
        StringBuilder temp = new StringBuilder(2550);
        int i = GetPrivateProfileString(section, key, "", temp, 2550, this.path);
        //MessageBox(IntPtr.Zero, this.path+i + ","+temp+","+section+key, "ReadIniContent", 0);
        return temp.ToString();
    }
    //判断路径是否正确
    public bool IsIniPath()
    {
        return File.Exists(this.path);
    }
}

读取测试:

string configPath = Application.streamingAssetsPath + "\\config.ini";
ConfigIni configIni = new ConfigIni(configPath);
string str = configIni.ReadIniContent("para", "minNum");
int num = Convert.ToInt32(str);

Unity读取配置文件可以通过使用Unity提供的API来实现。一种常见的配置文件格式是JSON,下面是一个简单的示例: ```json { "playerName": "John", "playerLevel": 10, "playerHealth": 100 } ``` 要读取这个配置文件,可以按照以下步骤进行操作: 1. 创建一个C#脚本,用于读取配置文件的数据。可以将该脚本附加到一个游戏对象上,或者作为一个单独的脚本使用。 2. 在脚本使用`System.IO`命名空间下的`File.ReadAllText()`方法来读取配置文件的内容。该方法接受一个文件路径作为参数,并返回文件内容的字符串。 3. 使用Unity提供的JsonUtility类来解析JSON字符串,并将其转换为对应的数据结构。例如,可以定义一个与配置文件结构相匹配的C#类,然后使用`JsonUtility.FromJson()`方法将JSON字符串转换为该类的实例。 4. 可以通过访问该类的属性或字段来获取配置文件的数据。 下面是一个示例代码: ```csharp using UnityEngine; using System.IO; public class ConfigReader : MonoBehaviour { public string configFilePath; // 配置文件路径 private void Start() { string json = File.ReadAllText(configFilePath); ConfigData configData = JsonUtility.FromJson<ConfigData>(json); Debug.Log("Player Name: " + configData.playerName); Debug.Log("Player Level: " + configData.playerLevel); Debug.Log("Player Health: " + configData.playerHealth); } } [System.Serializable] public class ConfigData { public string playerName; public int playerLevel; public int playerHealth; } ``` 请注意,上述示例的`configFilePath`变量需要指定配置文件路径。你可以将配置文件放在Unity项目的任何位置,并将其路径分配给`configFilePath`变量。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值