__文本资源__是导入的文本文件的格式。将文本文件拖放到 Project 文件夹中时,它将转换为文本资源。支持的文本格式如下:
请注意,为了使用 AssetDatabase.FindAssets 函数,脚本文件也被视为文本资源,因此当此函数与“t:TextAsset”过滤条件一起使用时,脚本文件也将包含在结果列表中。
属性: | 功能: |
---|---|
Text | 资源的完整文本(作为单个字符串)。 |
文本资源是一种非常专业的用例。在构建游戏时,将文本从不同的文本文件添加到游戏中非常有用。您可以编写一个简单的 .txt 文件,并且很容易就能将该文本添加到游戏中。该资源不适用于在运行时生成文本文件。如果有此需求,您需要使用传统的输入/输出编程技术来读写外部文件。
请考虑以下情况。您正在开发一个带有大量文字的传统冒险游戏。为简化制作,您希望将游戏中的所有文本分解到不同的房间。在这种情况下,您将创建一个文本文件,其中包含将在一个房间中使用的所有文本。这样就能很容易地为您进入的房间引用正确的文本资源。然后,借助一些自定义的解析逻辑,即可非常轻松地管理大量文本。
文本资源的一个特性就在于它可用于存储二进制数据。通过为文件提供扩展名 .bytes,即可将其作为文本资源加载,并可通过 bytes 属性来访问数据。
例如,将一个 jpeg 文件放入 Resources 文件夹并将文件的扩展名更改为 .bytes,然后使用以下脚本代码在运行时读取数据:
//Load texture from disk
TextAsset bindata = Resources.Load("Texture") as TextAsset;
Texture2D tex = new Texture2D(1,1);
tex.LoadImage(bindata.bytes);
请注意,带有 .txt 和 .bytes 扩展名的文件将分别视为文本文件和二进制文件。不要尝试使用 .txt 扩展名来存储二进制文件,因为这样会在尝试从中读取数据时产生意外行为。
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.