unity C# 读取INI文件

该代码示例展示了如何在Unity3D环境中创建一个名为INIReader的脚本,用于读取和解析INI配置文件。它处理文件中的不同节和键值对,提供获取字符串、整数和浮点数值的方法。示例中,从Person节读取Name、Age和Height的信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using System;

public class INIReader : MonoBehaviour
{
    // INI文件路径
    public string filePath = "config.ini";

    // INI文件中的键值对
    private Dictionary<string, Dictionary<string, string>> keyValuePairs = new Dictionary<string, Dictionary<string, string>>();

    void Start()
    {
        ReadINIFile();
    }

    // 读取INI文件
    private void ReadINIFile()
    {
        if (!File.Exists(filePath))
        {
            Debug.LogError("File does not exist: " + filePath);
            return;
        }

        // 读取文件内容
        string[] lines = File.ReadAllLines(filePath);

        string section = "";
        foreach (string line in lines)
        {
            // 去除注释
            string content = line.Trim();
            int index = content.IndexOf(";");
            if (index >= 0)
            {
                content = content.Substring(0, index);
            }

            // 处理节名
            if (content.StartsWith("[") && content.EndsWith("]"))
            {
                section = content.Substring(1, content.Length - 2);
                if (!keyValuePairs.ContainsKey(section))
                {
                    keyValuePairs.Add(section, new Dictionary<string, string>());
                }
            }
            // 处理键值对
            else if (section.Length > 0)
            {
                index = content.IndexOf("=");
                if (index >= 0)
                {
                    string key = content.Substring(0, index).Trim();
                    string value = content.Substring(index + 1).Trim();
                    if (!keyValuePairs[section].ContainsKey(key))
                    {
                        keyValuePairs[section].Add(key, value);
                    }
                }
            }
        }
    }

    // 获取指定节和键的值
    public string GetStringValue(string section, string key, string defaultValue = "")
    {
        if (keyValuePairs.ContainsKey(section) && keyValuePairs[section].ContainsKey(key))
        {
            return keyValuePairs[section][key];
        }
        return defaultValue;
    }

    public int GetIntValue(string section, string key, int defaultValue = 0)
    {
        string value = GetStringValue(section, key);
        int result;
        if (int.TryParse(value, out result))
        {
            return result;
        }
        return defaultValue;
    }

    public float GetFloatValue(string section, string key, float defaultValue = 0.0f)
    {
        string value = GetStringValue(section, key);
        float result;
        if (float.TryParse(value, out result))
        {
            return result;
        }
        return defaultValue;
    }
}
INIReader reader = GetComponent<INIReader>();
string name = reader.GetStringValue("Person", "Name");
int age = reader.GetIntValue("Person", "Age");
float height = reader.GetFloatValue("Person", "Height");

以下是一个示例的INI文件:

[Person]
Name=John
Age=30
Height=1.75
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值