当前位置: 首页 > news >正文

Unity 使用INI文件存储数据或配置参数预设

法1:调用外部C++api库

具体使用:

public class Ini{//读取INI文件需要调用C++的APP[System.Runtime.InteropServices.DllImport("kernel32")]private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);[System.Runtime.InteropServices.DllImport("kernel32")]private static extern int GetPrivateProfileString(string section, string key, string def, System.Text.StringBuilder retVal, int size, string filePath);private string iPath = null;public Ini(string path){this.iPath = path;}/// <summary>/// 写数据/// </summary>/// <param name="section">配置节</param>/// <param name="key">键名</param>/// <param name="value">键值</param>public void WriteValue(string section, string key, string value){  WritePrivateProfileString(section, key, value, iPath);}/// <summary>/// 读数据/// </summary>/// <param name="section">配置节</param>/// <param name="key">键名</param>/// <returns></returns>public string ReadValue(string section, string key){// 每次从ini中读取多少字节 System.Text.StringBuilder temp = new System.Text.StringBuilder(255);        GetPrivateProfileString(section, key, "", temp, 255, iPath);return temp.ToString();}
}public class Program
{public static void Main(){string filePath = Application.streamingAssetsPath + "/file.ini";string section = "SectionName";string key = "KeyName";// 读取 INI 文件string value = Ini.ReadValue(section, key, "", filePath);Console.WriteLine("Value: " + value);// 写入 INI 文件Ini.WriteValue(section, key, "NewValue", filePath);Console.WriteLine("Value written.");// 重新读取 INI 文件value = Ini.ReadValue(section, key, "", filePath);Console.WriteLine("Value: " + value);}
}

ini文本参考:

法2:使用System.IO命名空间的类:

具体使用:

using System;
using System.Collections.Generic;
using System.IO;class IniFile
{private readonly string filePath;private readonly Dictionary<string, Dictionary<string, string>> sections;public IniFile(string filePath){this.filePath = filePath;this.sections = new Dictionary<string, Dictionary<string, string>>();Load();}public string GetValue(string section, string key){if (sections.ContainsKey(section) && sections[section].ContainsKey(key)){return sections[section][key];}return null;}public void SetValue(string section, string key, string value){if (!sections.ContainsKey(section)){sections[section] = new Dictionary<string, string>();}sections[section][key] = value;Save();}private void Load(){string currentSection = null;foreach (string line in File.ReadLines(filePath)){string trimmedLine = line.Trim();if (trimmedLine.StartsWith("[") && trimmedLine.EndsWith("]")){currentSection = trimmedLine.Substring(1, trimmedLine.Length - 2);continue;}int equalsIndex = trimmedLine.IndexOf('=');if (equalsIndex > 0){string key = trimmedLine.Substring(0, equalsIndex).Trim();string value = trimmedLine.Substring(equalsIndex + 1).Trim();if (!string.IsNullOrEmpty(currentSection) && !string.IsNullOrEmpty(key)){if (!sections.ContainsKey(currentSection)){sections[currentSection] = new Dictionary<string, string>();}sections[currentSection][key] = value;}}}}private void Save(){using (StreamWriter writer = new StreamWriter(filePath)){foreach (KeyValuePair<string, Dictionary<string, string>> section in sections){writer.WriteLine($"[{section.Key}]");foreach (KeyValuePair<string, string> entry in section.Value){writer.WriteLine($"{entry.Key}={entry.Value}");}writer.WriteLine();}}}
}public class Program
{public static void Main(){string filePath = Application.streamingAssetsPath + "/file.ini";string section = "SectionName";string key = "KeyName";IniFile iniFile = new IniFile(filePath);// 读取值string value = iniFile.GetValue("section", "key");Console.WriteLine(value);// 设置值iniFile.SetValue("Section2", "Key2", "Value2");       }
}

 

相关文章:

Unity 使用INI文件存储数据或配置参数预设

法1&#xff1a;调用外部Capi库 具体使用&#xff1a; public class Ini{//读取INI文件需要调用C的APP[System.Runtime.InteropServices.DllImport("kernel32")]private static extern long WritePrivateProfileString(string section, string key, string val, st…...

clouldcompare工具使用

文章目录 1.界面1.1 布局1.3 视觉显示方向1.4 放大镜1.5 建立旋转中心2.快速入门2.1 剪裁2.2 多点云拼接 1.界面 1.1 布局 参考&#xff1a;https://blog.csdn.net/lovely_yoshino/article/details/129595201 1.3 视觉显示方向 1.4 放大镜 1.5 建立旋转中心 2.快速入门 2.1 …...

在vue3中使用Element-plus的图标

首先安装Element-Plus-icon # 选择一个你喜欢的包管理器# NPM $ npm install element-plus/icons-vue # Yarn $ yarn add element-plus/icons-vue # pnpm $ pnpm install element-plus/icons-vue 如何使用 Element-Plus-icon官方文档链接Icon 图标 | Element Plus (element-…...

图扑智慧农业:农林牧数据可视化监控平台

数字农业是一种现代农业方式&#xff0c;它将信息作为农业生产的重要元素&#xff0c;并利用现代信息技术进行农业生产过程的实时可视化、数字化设计和信息化管理。能将信息技术与农业生产的各个环节有机融合&#xff0c;对于改造传统农业和改变农业生产方式具有重要意义。 图…...

js 加解密 jsencrypt(非对称加密 rsa)

这是一个非对称加密的库&#xff0c;可以进行 rsa 加解密 使用方法 安装 npm install jsencrypt --save jsencrypt rsa 加解密 let rsaStr "这就是一个RSA加密的测试";let jsencryptObj new jsencrypt();jsencryptObj.getKey(); //这个方法用来生成一个密钥对…...

xlua游戏热更新(lua访问C#)

CS.UnityEngine静态方法访问unity虚拟机 创建游戏物体 CS.UnityEngine.GameObject(new by lua);静态属性 CS.UnityEngine.GameObject(new by lua); -- 创建 local camera CS.UnityEngine.GameObject.Find(Main Camera); --查找 camera.name Renamed by Lua;访问组件 loca…...

04-Spring中Bean的作用域

Bean的作用域 scope的属性值 属性值作用singleton默认单例prototype原型每调用一次getBean()方法则获取一个新的Bean对象 , 每次注入的时候都是新对象request一个请求对应一个Bean仅限于在WEB应用中使用 , 需要引入web的框架如SpringMvc(global) session一个会话对应一个Bean…...

xlua游戏热更新(C#访问lua)

xlua作为Unity资源热更新的重要解决方案api&#xff0c;在Tecent重多游戏中被采用&#xff0c;本文通过案例去讲解xlua代码结构层次。 /** Tencent is pleased to support the open source community by making xLua available.* Copyright (C) 2016 THL A29 Limited, a Tence…...

【数据结构】二叉树经典例题---<你真的掌握二叉树了吗?>(第一弹)

一、已知一颗二叉树如下图&#xff0c;试求&#xff1a; (1)该二叉树前序、中序和后序遍历的结果。 (2)该二叉树是否为满二叉树&#xff1f;是否为完全二叉树&#xff1f; (3)将它转换成对应的树或森林。 (4)这颗二叉树的深度为多少? (5)试对该二叉树进行前序线索化。 (6)试对…...

基于springboot实现桥牌计分管理系统项目【项目源码】

基于springboot实现桥牌计分管理系统演示 JAVA简介 JavaScript是一种网络脚本语言&#xff0c;广泛运用于web应用开发&#xff0c;可以用来添加网页的格式动态效果&#xff0c;该语言不用进行预编译就直接运行&#xff0c;可以直接嵌入HTML语言中&#xff0c;写成js语言&#…...

机器学习——朴素贝叶斯

目录 一、贝叶斯方法 背景知识 贝叶斯公式 二、朴素贝叶斯原理 判别模型和生成模型 1&#xff0e;朴素贝叶斯法是典型的生成学习方法 2&#xff0e;朴素贝叶斯法的基本假设是条件独立性 3&#xff0e;朴素贝叶斯法利用贝叶斯定理与学到的联合概率模型进行分类预测 用于文…...

【PTE-day07 文件上传2】

1、常见的绕过方式 (1)畸形后缀名绕过 .php、.pht、.php3、.php4、.php5、.php2、.phtml、.pHp、.html、.Htm......(2)双写过滤字符绕过 (3).htaccess文件绕过 <FilesMatch "jpg"> SetHandler application/x-httpd-php...

设计模式之十一:代理模式

代理可以控制和管理访问。 RMI提供了客户辅助对象和服务辅助对象&#xff0c;为客户辅助对象创建和服务对象相同的方法。RMI的好处在于你不必亲自写任何网络或I/O代码。客户程序调用远程方法就和运行在客户自己本地JVM对对象进行正常方法调用一样。 步骤一&#xff1a;制作远程…...

在spring boot中调用第三方接口时重试问题

文章目录 前言 spring-retry对第三方接口做重试&#xff0c;和处理操作 一、引入依赖 <!--重试请求的注解依赖--><dependency><groupId>org.springframework.retry</groupId><artifactId>spring-retry</artifactId></dependency>&l…...

记录一次多数据源配置失效的情况

说明&#xff1a;在一些复杂的业务情景&#xff0c;比如我们需要在一个订单审核通过后&#xff0c;在将数据库状态修改的同时&#xff0c;将订单与订单详细这两条数据写入到另一个数据库中。我们就可以通过在配置文件中&#xff0c;配置多数据源&#xff0c;然后通过在Mapper的…...

EasyExcel导出替换列中的变量

基于easyexcel2.0版本 easyexcel官网&#xff1a;https://easyexcel.opensource.alibaba.com/docs/2.x/quickstart/write 测试代码地址&#xff1a;https://gitee.com/wangtianwen1996/cento-practice/blob/master/src/test/java/com/xiaobai/easyexcel/DynamicHeadTest.java …...

机器人规划算法——将多边形障碍物离散到地图像素点上?

问题一&#xff1a;如何判断一个点是否在多边形区域内&#xff1f; 方法1&#xff1a;向量叉乘判别法 设多边形的顶点依次为A1&#xff0c;A2…An&#xff0c;要判断的点为P&#xff0c;那么分别计算向量PA1叉乘向量PA2&#xff0c;向量PA2叉乘向量PA3&#xff0c;…&#xff…...

windows11使用docker部署安装minio

时间 2023-11-08 windows11使用docker部署安装minio 目录 1.docker 下载镜像2.docker安装镜像3.访问控制台4.安装问题解决5.使用教程 1.docker 下载镜像 调整镜像源到国内&#xff0c;否则会很慢 docker pull minio/minio2.docker安装镜像 设置用户名和密码时需要注意&…...

【JavaEESpring】Spring Web MVC⼊⻔

Spring Web MVC 1. 什么是 Spring Web MVC1.1 什么是 MVC ?1.2 是什么 Spring MVC? 2. 学习 Spring MVC2.1 建立连接2.2 请求2.3 响应 3. 相关代码链接 1. 什么是 Spring Web MVC 官⽅对于 Spring MVC 的描述是这样的&#xff1a; 1.1 什么是 MVC ? MVC 是 Model View C…...

flutter逆向 ACTF native app

前言 算了一下好长时间没打过CTF了,前两天看到ACTF逆向有道flutter逆向题就过来玩玩啦,花了一个下午做完了.说来也巧,我给DASCTF十月赛出的逆向题其中一道也是flutter,不过那题我难度降的相当之低啦,不知道有多少人做出来了呢~ 还原函数名 flutter逆向的一大难点就是不知道l…...

CocosCreator 之 JavaScript/TypeScript和Java的相互交互

引擎版本&#xff1a; 3.8.1 语言&#xff1a; JavaScript/TypeScript、C、Java 环境&#xff1a;Window 参考&#xff1a;Java原生反射机制 您好&#xff0c;我是鹤九日&#xff01; 回顾 在上篇文章中&#xff1a;CocosCreator Android项目接入UnityAds 广告SDK。 我们简单讲…...

Linux-07 ubuntu 的 chrome 启动不了

文章目录 问题原因解决步骤一、卸载旧版chrome二、重新安装chorme三、启动不了&#xff0c;报错如下四、启动不了&#xff0c;解决如下 总结 问题原因 在应用中可以看到chrome&#xff0c;但是打不开(说明&#xff1a;原来的ubuntu系统出问题了&#xff0c;这个是备用的硬盘&a…...

GitHub 趋势日报 (2025年06月08日)

&#x1f4ca; 由 TrendForge 系统生成 | &#x1f310; https://trendforge.devlive.org/ &#x1f310; 本日报中的项目描述已自动翻译为中文 &#x1f4c8; 今日获星趋势图 今日获星趋势图 884 cognee 566 dify 414 HumanSystemOptimization 414 omni-tools 321 note-gen …...

RNN避坑指南:从数学推导到LSTM/GRU工业级部署实战流程

本文较长&#xff0c;建议点赞收藏&#xff0c;以免遗失。更多AI大模型应用开发学习视频及资料&#xff0c;尽在聚客AI学院。 本文全面剖析RNN核心原理&#xff0c;深入讲解梯度消失/爆炸问题&#xff0c;并通过LSTM/GRU结构实现解决方案&#xff0c;提供时间序列预测和文本生成…...

ip子接口配置及删除

配置永久生效的子接口&#xff0c;2个IP 都可以登录你这一台服务器。重启不失效。 永久的 [应用] vi /etc/sysconfig/network-scripts/ifcfg-eth0修改文件内内容 TYPE"Ethernet" BOOTPROTO"none" NAME"eth0" DEVICE"eth0" ONBOOT&q…...

Vite中定义@软链接

在webpack中可以直接通过符号表示src路径&#xff0c;但是vite中默认不可以。 如何实现&#xff1a; vite中提供了resolve.alias&#xff1a;通过别名在指向一个具体的路径 在vite.config.js中 import { join } from pathexport default defineConfig({plugins: [vue()],//…...

在 Spring Boot 中使用 JSP

jsp&#xff1f; 好多年没用了。重新整一下 还费了点时间&#xff0c;记录一下。 项目结构&#xff1a; pom: <?xml version"1.0" encoding"UTF-8"?> <project xmlns"http://maven.apache.org/POM/4.0.0" xmlns:xsi"http://ww…...

LangFlow技术架构分析

&#x1f527; LangFlow 的可视化技术栈 前端节点编辑器 底层框架&#xff1a;基于 &#xff08;一个现代化的 React 节点绘图库&#xff09; 功能&#xff1a; 拖拽式构建 LangGraph 状态机 实时连线定义节点依赖关系 可视化调试循环和分支逻辑 与 LangGraph 的深…...

spring Security对RBAC及其ABAC的支持使用

RBAC (基于角色的访问控制) RBAC (Role-Based Access Control) 是 Spring Security 中最常用的权限模型&#xff0c;它将权限分配给角色&#xff0c;再将角色分配给用户。 RBAC 核心实现 1. 数据库设计 users roles permissions ------- ------…...

C++_哈希表

本篇文章是对C学习的哈希表部分的学习分享 相信一定会对你有所帮助~ 那咱们废话不多说&#xff0c;直接开始吧&#xff01; 一、基础概念 1. 哈希核心思想&#xff1a; 哈希函数的作用&#xff1a;通过此函数建立一个Key与存储位置之间的映射关系。理想目标&#xff1a;实现…...