Ini文件在unity(c#)中的使用

1、ini文件简介

主要用于存放数据量较小的文本性质的配置信息,具体实施根据实际需求来定。

1.1 文件格式

[SectionName1]                          //节点1

KeyName1=Value1   //key/value

KeyName2=Value2

........

[SectionName2]                          //节点2

KeyName1=Value1

KeyName2=Value2

........

1.2 文件内容示意

例如:xiaoming.ini

;注解下本文档仅起举例示意作用

[Character]

Height=180

Long=60

Wide=50

Circle=5

ContinueTime=forever

Degree=doctor

Profession=ThePresident

[Hobby]

InterstellerTravel=frequently

Airship=99+

[Hate]

Species=NoHave

2、Unity(c#)读写ini的使用

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Runtime.InteropServices;
using System;
using System.Text;

/// <summary>
/// ini类文件读写辅助类
/// </summary>
public class FileHelper
{
    [DllImport("kernel32")]
    private static extern int GetPrivateProfileString(string section,string key,string defValue,StringBuilder recValue,int size,string filePath);//读取信息函数
    [DllImport("kernel32")]
    private static extern int WritePrivateProfileString(string section, string key, string value,string filePath);//写入信息函数

    private string filePath = "";//ini文件路径

    public FileHelper(string filepath)
    {
        filePath = Application.streamingAssetsPath + "/" + filepath + ".ini";
    }
    /// <summary>
    /// 写入操作
    /// </summary>
    /// <param name="section"></param>
    /// <param name="key"></param>
    /// <param name="value"></param>
    public void Write(string section, string key, string value)
    {
        try
        {
            WritePrivateProfileString(section,key,value,filePath);
        }
        catch (Exception e)
        {
            Debug.Log(e.Message);
        }
    }
    /// <summary>
    /// 读取ini文件内容
    /// </summary>
    /// <param name="section"></param>
    /// <param name="key"></param>
    /// <returns></returns>
    public string Read(string section, string key)
    {
        try
        {
            StringBuilder tmpSB = new StringBuilder(255);
            GetPrivateProfileString(section,key,"",tmpSB,255,filePath);
            return tmpSB.ToString();
        }
        catch (Exception e)
        {
            Debug.Log(e.Message);
            return "";
        }
    }

    public static string configPath = Application.streamingAssetsPath + "/xiaoming.ini";

    public static void IniWrite(string section, string key, string value, string path)
    {
        WritePrivateProfileString(section,key,value,path);
    }

    public static string IniRead(string section, string key, string path)
    {
        StringBuilder tmpSB = new StringBuilder(255);
        GetPrivateProfileString(section, key, "", tmpSB, 255, path);
        return tmpSB.ToString();
    }

}
 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值