config.ini存放在Assets目录下,项目打包之后存放在Data目录下exe同目录

// 从配置文件读取
string configFile = Application.dataPath + "/config.ini";//打包好的“xxx_Data”目录貌似没有读取里面的文件权限
//所以对于打包的程序,需要把配置文件config.ini放在exe同目录下
#if !UNITY_EDITOR
configFile = System.Environment.CurrentDirectory + "/config.ini";
#endif
if (File.Exists(configFile))
{
ConfigIni ini = new ConfigIni(configFile);
//host = ini.ReadIniContent("DeepStreamServer", "host");
//port = ini.ReadIniContent("DeepStreamServer", "port");
host = ini.keyVal["host"];
port = ini.keyVal["port"];
//MessageBox(IntPtr.Zero, configFile+","+host+","+port, "从配置文件读取DeepStream的IP:Port", 0);
}
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using UnityEngine; /// <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;
public Dictionary<string, string> keyVal = new Dictionary<string, string>(); //ini文件的路径
public ConfigIni(string path)
{
this.path = path; StreamReader sr = new StreamReader(path, Encoding.Default);
string line;
while ((line = sr.ReadLine()) != null)
{
if (line.Contains("="))
{
string[] kv = line.Split('=');
string key = kv[].Trim();
string v = kv[].Trim();
keyVal.Add(key, v);
}
}
} [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); //写入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();
int i = GetPrivateProfileString(section, key, "", temp, , this.path);
//MessageBox(IntPtr.Zero, this.path+i + ","+temp+","+section+key, "ReadIniContent", 0);
return temp.ToString();
}
//判断路径是否正确
public bool IsIniPath()
{
return File.Exists(this.path);
}
}

Unity读取配置文件的更多相关文章

  1. IT咨询顾问:一次吐血的项目救火 java或判断优化小技巧 asp.net core Session的测试使用心得 【.NET架构】BIM软件架构02:Web管控平台后台架构 NetCore入门篇:(十一)NetCore项目读取配置文件appsettings.json 使用LINQ生成Where的SQL语句 js_jquery_创建cookie有效期问题_时区问题

    IT咨询顾问:一次吐血的项目救火   年后的一个合作公司上线了一个子业务系统,对接公司内部的单点系统.我收到该公司的技术咨询:项目启动后没有规律的突然无法登录了,重新启动后,登录一断时间后又无法重新登 ...

  2. 【无私分享:ASP.NET CORE 项目实战(第八章)】读取配置文件(二) 读取自定义配置文件

    目录索引 [无私分享:ASP.NET CORE 项目实战]目录索引 简介 我们在 读取配置文件(一) appsettings.json 中介绍了,如何读取appsettings.json. 但随之产生 ...

  3. 解决IntelliJ IDEA无法读取配置文件的问题

    解决IntelliJ IDEA无法读取配置文件的问题 最近在学Mybatis,按照视频的讲解在项目的某个包里建立配置文件,然后读取配置文件,但是一直提示异常. 读取配置文件的为官方代码: String ...

  4. java-工具类-读取配置文件

    java读取配置文件,当发现文件被修改后则重新加载 package com.zg.config; import java.io.File; import java.io.FileInputStream ...

  5. java 4种方式读取配置文件 + 修改配置文件

    版权声明:本文为博主原创文章,未经博主允许不得转载.   目录(?)[-] 方式一采用ServletContext读取读取配置文件的realpath然后通过文件流读取出来 方式二采用ResourceB ...

  6. 在IIS Express中调试时无法读取配置文件 错误

    在IIS Express中调试代码时,如果出现"无法读取配置文件"的问题(如图),这种情况是IIS Express的"applicationhost.config&quo ...

  7. ASP.NET Core开发-读取配置文件Configuration

    ASP.NET Core 是如何读取配置文件,今天我们来学习. ASP.NET Core的配置系统已经和之前版本的ASP.NET有所不同了,之前是依赖于System.Configuration和XML ...

  8. Java 利用 ByteArrayOutputStream 和 ByteArrayInputStream 避免重复读取配置文件

    最近参与了github上的一个开源项目 Mycat,是一个mysql的分库分表的中间件.发现其中读取配置文件的代码,存在频繁多次重复打开,读取,关闭的问题,代码写的很初级,稍微看过一些框架源码的人,是 ...

  9. Python+Selenium进行UI自动化测试项目中,常用的小技巧2:读取配置文件(configparser,.ini文件)

    在自动化测试项目中,可能会碰到一些经常使用的但 很少变化的配置信息,下面就来介绍使用configparser来读取配置信息config.ini 读取的信息(config.ini)如下: [config ...

随机推荐

  1. thinkphp5的Redis缓存配置

    thinkphp采用cache类提供缓存功能支持,采用驱动方式,在使用缓存之前需要进行初始化操作.支持的缓存类型包括file.memcache.wincache.sqlite.redis和xcache ...

  2. Mac 系统上安装Protocol buffer

    1. cd /Software/protobuf-2.5.0 2.sudo ./configure --prefix=$/Software/protobuf-2.5.0 3.sudo make 4.s ...

  3. SphereFace的原理

    https://blog.csdn.net/qianqing13579/article/details/78288780

  4. C++自定义异常类

    代码样例: #include <iostream> using namespace std; class illegalParameterValue { public: illegalPa ...

  5. C语言中的控制语句: 判断、环循等;

    列举这一部分的目的就是注意它的写法,尤其是 分号 : 的位置: 1. if-else if else 语句: if (表达式1) 语句1: else if (表达式2) 语句2: else 语句3: ...

  6. JUnit4忽略(Ignore)测试实例

    这种“忽略”是指方法还没有准备好进行测试,JUnit引擎会绕过(忽略)这个方法. import org.junit.*; /** * JUnit Ignore Test * @author yiiba ...

  7. e833. 获得JTabbedPane中的卡片

    This example retrieves all the tabs in a tabbed pane: // To create a tabbed pane, see e828 创建JTabbed ...

  8. Objective-C MacOS以管理员权限执行程序

    在MacOS下非常多操作是须要管理员权限的, 比方我们执行chmod.在命令行下能够使用sudo chmod来申请以管理员权限执行.可是使用XCode写的程序是不能使用sudo的. 须要自己写代码来申 ...

  9. Xianfeng轻量级Java中间件平台:用户管理

    用户管理:简单地说,就是管理系统用户的账号信息.在典型的业务系统中,用户管理往往不包含用户隶属组织机构相关属性(比如部门.岗位等)的维护,而是由组织机构管理专门去维护.因此在用户管理的功能中,主要就维 ...

  10. XSD 数据类型

    字符串数据类型(String Data Type) 字符串数据类型可包含字符.换行.回车以及制表符. 下面是一个关于某个 scheme 中字符串声明的例子: <xs:element name=& ...