简体   繁体   English

Unity如何将文件目录加载到脚本中并将其读取为文本资产?

[英]Unity how to load directory of files into a script and read them as text assets?

I have a script attached to an object in Unity. 我有一个脚本附加到Unity中的对象。 I want to load a folder of .vtk files into my project assets folder, attach that folder to the script on the object as I would attach any public variable to a script in the editor (by dragging and dropping it), and have my script read each .vtk file in the folder as separate TextAsset objects. 我想将.vtk文件的文件夹加载到我的项目资产文件夹中,将该文件夹附加到对象上的脚本,就像我将任何公共变量附加到编辑器中的脚本(通过将其拖放)一样,并保存我的脚本将文件夹中的每个.vtk文件读取为单独的TextAsset对象。 I would actually like each file to be an element of a TextAsset array, but that should be easy once I can get Unity to accept the folder of .vtk files as individual TextAsset objects. 我实际上希望每个文件都是TextAsset数组的元素,但是一旦我可以让Unity接受.vtk文件的文件夹作为单独的TextAsset对象,那应该很容易。

I've tried declaring a public Directory and Path object to specify the folder, but I get the error that I "cannot declare a variable of static type 'Directory'". 我尝试声明一个公共Directory and Path对象来指定文件夹,但是我收到错误消息“无法声明静态类型'Directory'的变量”。

It sounds like you're looking for Resources.LoadAll() . 听起来您在寻找Resources.LoadAll() The folder you're loading from needs to be in a folder named Resources inside your Assets folder for this function to work. 要从中加载的文件夹必须位于Assets文件夹内名为Resources的文件夹中,此功能才能起作用。

The function returns an array of objects, so you'll also need to cast them to TextAssets if you need that. 该函数返回一个对象数组,因此如果需要,还需要将它们强制转换为TextAssets。

Example usage: 用法示例:

TextAsset[] results = Array.ConvertAll(Resources.LoadAll("FolderName", typeof(TextAsset)), asset => (TextAsset)asset);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM