挂载脚本
/**
*┌──────────────────────────────────────────────────────────────┐
*│ Description:读取或者写入Ini文件的基类 ,其他类想要获取ini配置文件信息,需要该类作为中介
*│ Author:#Keneyr#
*│ Version:#1.0#
*│ Date:#2019.7.31#
*│ UnityVersion: #Unity2018.3.2f#
*└──────────────────────────────────────────────────────────────┘
*┌──────────────────────────────────────────────────────────────┐
*│ ClassName:#ConfigIni#
*└──────────────────────────────────────────────────────────────┘
*/
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Runtime.InteropServices;
using System.Text;
using System.IO;
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);
//写入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(255);
int i = GetPrivateProfileString(section, key, "", temp, 255, this.path);
return temp.ToString();
}
//判断路径是否正确
public bool IsIniPath()
{
return File.Exists(this.path);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ConfigTestDemo : MonoBehaviour
{
ConfigIni ini;
private string time = "";
private string speed = "";
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
void Start()
{
Debug.Log("ConfigTestDemo Start");
//获取ini文件
ini = new ConfigIni("Assets/Config/info.ini");
//获取Ini文件Time类型下的time对应的数值
time = ini.ReadIniContent("Time", "time");
speed = ini.ReadIniContent("Speed", "speed");
}
public void OnClick()
{
//写入Count类型count值对应的数值(如果存在了相同的key会覆盖原来key的内容)
//ini.WriteIniContent("Count", "count", "5");
ini.WriteIniContent("Time", "time", "666");
Debug.Log(time);
Debug.Log(speed);
}
}
点击按钮就写入