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. Qt Creator设置多核编译(-j8参数)

    In the qtcreator go to the "Projects tab" and set "Make arguments" as you like: ...

  2. 自然语言交流系统 phxnet团队 创新实训 个人博客 (七)

    使用Lucene 3.0.0的结构遍历TokenStream的内容. 以前版本的Lucene是用TokenStream.next()来遍历TokenStream的内容, 目前的版本稍微修改了一下, 使 ...

  3. Intel edison 智能硬件开发指南 基于YoctoProject

    首先简单的介绍一下edison的板子: edison 芯片 22nm工艺,quark双核SoC,采用atom架构,针对小型智能设备  X86架构 相当于一台“奔腾级电脑” 低功耗,小体积,自带wifi ...

  4. CentOS执行ping命令报错 name or service not know

    在虚拟机上安装的CentOS,但是当执行ping命令的时候,提示name or service not known 解决方法如下: 1. 添加DNS服务器 vi /etc/resolv.conf 1 ...

  5. Python之进度条

    pip install tqdm from tqdm import tqdm,trange import time for char in tqdm(['a','b','c','d']): time. ...

  6. 《深入理解Java虚拟机》读书笔记:Java内存区域

    xmind文件下载地址

  7. nodejs基础 -- 路由

    我们要为路由提供请求的URL和其他需要的GET/POST参数,随后路由需要根据这些数据(URL.GET/POST参数)来执行相应的代码. 因此,需要查看HTTP请求,从中提取出请求的URL及GET/P ...

  8. 每天一个linux命令:mv命令

    mv命令是move的缩写,可以用来移动文件或者将文件改名(move (rename) files),是Linux系统下常用的命令,经常用来备份文件或者目录. 1.命令格式: mv [选项] 源文件或目 ...

  9. unity3d 调用Start 注意

    在unity3d中,同一个脚本被绑定到多个物体上的时候,只有active的物体才会调用void Start ()  方法, 如果物体是NO Active 的状态,则不会调用Start,Awake也不会 ...

  10. iOS: Sorted Array with Compare

    Question(提问): What I want to do seems pretty simple, but I can't find any answers on the web. I have ...