unity read files 从ini文件

本文介绍了一种使用Unity游戏引擎加载预制体的方法。通过读取setup.ini配置文件,该脚本能够加载指定的预制体资源并实例化它们。具体步骤包括放置setup.ini文件于正确路径,定义ReadFiles脚本用于读取文件内容,并根据文件指示加载位于Assets/Resources文件夹下的World、House、Desktop等预制体。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1, setup.ini文件

Prefabs World House Desktop
ServerAddress file://D:/CPICode/U3DPrefabs/

2 setup.ini文件放在unity工程的Assets同一级


3 World, House, Desktop是预制体,放在Assets下的Resources文件夹中


4, ReadFiles的cs脚本

using UnityEngine;
using System.Collections;
using System.IO;

public class Readfiles : MonoBehaviour {

	// Use this for initialization
	void Start () {

		string[] content = File.ReadAllLines("setup.ini");

		// 2. 根据初始化文件加载预制体
		foreach (var item in content)
		{
			string line = item.ToString();
			string[] vars = line.Split(' ');
			if (vars[0].ToString() == "Prefabs")
			{
				for (int i = 1; i < vars.Length; i++)
				{
					GameObject hp_bar = (GameObject)Resources.Load(vars[i].ToString());
					hp_bar = Instantiate(hp_bar);
					hp_bar.name = vars[i].ToString();
				}
			}
//			else if(vars[0].ToString() == "ServerAddress")
//			{
//				ServerAddress = vars[1].ToString();
//				myModelingManager.ServerAddress = ServerAddress;
//			}
		}
	
	}
	
	// Update is called once per frame
	void Update () {
	
	}
}

5, 运行加载unity工程的结果


C:\Users\陈柯嘉>pip install pandas Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple Collecting pandas Downloading https://pypi.tuna.tsinghua.edu.cn/packages/9c/d6/9f8431bacc2e19dca897724cd097b1bb224a6ad5433784a44b587c7c13af/pandas-2.2.3.tar.gz (4.4 MB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 4.4/4.4 MB 20.4 MB/s eta 0:00:00 Installing build dependencies ... done Getting requirements to build wheel ... done Installing backend dependencies ... done Preparing metadata (pyproject.toml) ... error error: subprocess-exited-with-error × Preparing metadata (pyproject.toml) did not run successfully. │ exit code: 2 ╰─> [32 lines of output] + meson setup C:\Users\陈柯嘉\AppData\Local\Temp\pip-install-1zmbipo9\pandas_f3dee660ca06481fa639fc991b6efdb4 C:\Users\陈柯嘉\AppData\Local\Temp\pip-install-1zmbipo9\pandas_f3dee660ca06481fa639fc991b6efdb4\.mesonpy-bg46k2qm\build -Dbuildtype=release -Db_ndebug=if-release -Db_vscrt=md --vsenv --native-file=C:\Users\陈柯嘉\AppData\Local\Temp\pip-install-1zmbipo9\pandas_f3dee660ca06481fa639fc991b6efdb4\.mesonpy-bg46k2qm\build\meson-python-native-file.ini Traceback (most recent call last): File "C:\Users\陈柯嘉\AppData\Local\Temp\pip-build-env-9lvekt6r\overlay\Lib\site-packages\mesonbuild\mesonmain.py", line 194, in run return options.run_func(options) ^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\陈柯嘉\AppData\Local\Temp\pip-build-env-9lvekt6r\overlay\Lib\site-packages\mesonbuild\msetup.py", line 358, in run app.generate() File "C:\Users\陈柯嘉\AppData\Local\Temp\pip-build-env-9lvekt6r\overlay\Lib\site-packages\mesonbuild\msetup.py", line 178, in generate env = environment.Environment(self.source_dir, self.build_dir, self.options) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\陈柯嘉\AppData\Local\Temp\pip-build-env-9lvekt6r\overlay\Lib\site-packages\mesonbuild\environment.py", line 552, in __init__ config = coredata.parse_machine_files(self.coredata.config_files) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\陈柯嘉\AppData\Local\Temp\pip-build-env-9lvekt6r\overlay\Lib\site-packages\mesonbuild\coredata.py", line 1030, in parse_machine_files parser = MachineFileParser(filenames) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\陈柯嘉\AppData\Local\Temp\pip-build-env-9lvekt6r\overlay\Lib\site-packages\mesonbuild\coredata.py", line 972, in __init__ self.parser.read(filenames) File "C:\Users\陈柯嘉\AppData\Local\Temp\pip-build-env-9lvekt6r\overlay\Lib\site-packages\mesonbuild\coredata.py", line 959, in read return super().read(filenames, encoding) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\陈柯嘉\AppData\Local\Programs\Python\Python312-32\Lib\configparser.py", line 684, in read self._read(fp, filename) File "C:\Users\陈柯嘉\AppData\Local\Programs\Python\Python312-32\Lib\configparser.py", line 999, in _read for lineno, line in enumerate(fp, start=1): ^^^^^^^^^^^^^^^^^^^^^^ File "<frozen codecs>", line 322, in decode UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb3 in position 33: invalid start byte ERROR: Unhandled python exception This is a Meson bug and should be reported! [end of output] note: This error originates from a subprocess, and is likely not a problem with pip. error: metadata-generation-failed × Encountered error while generating package metadata. ╰─> See above for output. note: This is an issue with the package mentioned above, not pip. hint: See above for details. 怎么解决
05-22
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值