C# 将纹理内容路径保存为字符串
我想知道是否有一种简单的方法可以将纹理的路径存储在字符串中。 我想知道这一点,因为我正在使用游戏的保存系统,我需要将纹理的路径存储在文件中以便稍后加载它。 我尝试过使用Texture2D.Name,但只返回“”或null。 如果我无法获得完整路径也没关系,但至少我想知道纹理的名称。
编辑:我认为我解释得有点不好或者我不明白正确回答,我会给出新的尝试解释。
我制作了一个脚本,可以保存“敌人”类别的位置、生命值等。然后,该文件将存储在 \My Documents 文件夹中的 .txt 文件中。一旦我想重新加载信息,我就会创建一个 Enemy(Vector2, int) 的新实例。然后我加载信息并使用 int.Parse 将字符串转换为 int > new Enemy(new Vector2(int.Parse(string1), int.Parse(string2)), int.Parse(string3)) <
然后我想要的是以某种方式保存纹理的名称以供以后加载它回到使用上面提到的类似方法。 (从 .txt 中以字符串形式检索)
I'm wondering if there is a simple way to store the path of a texture in a string.
I'm wondering this because I'm using a save system for a game where I need to store the path of a texture in a file to later load it.
I've tried using Texture2D.Name but that just returns "" or null.
If I can't get the full path that's okay, but at the very least I'd like to have the name of the texture.
EDIT: I think I explained it a bit bad or I don't understand the answers properly, I'll give explaining a new try.
I've made a script that will save the Position, HP etc for an "Enemy" class. This then gets stored in a .txt file on the \My Documents folder. Once I want to load the information back in I create a new instance of an Enemy(Vector2, int). Then I load the information and use an int.Parse to convert my string to an int > new Enemy(new Vector2(int.Parse(string1), int.Parse(string2)), int.Parse(string3)) <
What I then want is to somehow save the name of the texture to later load it back in using a similar method mentioned above. (Retrieve it from a .txt as a string)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
严格来说,Texture2D 是内存中的对象,Texture2D 和文件之间没有任何关系(除了可以从文件加载和保存到文件中之外)。如果您想记住从中加载的文件名,则在加载纹理时应保留该文件名。
编辑回应您的评论:
假设您不是在谈论屏幕截图,您需要执行以下操作:
A Texture2D is strictly an in-memory object, there is no relation between a Texture2D and a file (except that a Texture2D can be loaded from and saved to files). If you want to remember the file name you loaded it from, you should hold on to the file name when you load the texture.
EDIT In response to your comment:
Assuming you're not talking about a screenshot, you'll need to do something like this:
大多数对象没有可用的名称,即使有,用作文件名也不一定安全。
除非您有充分的理由期望对象有好的名称并实际使用它,否则我建议简单地使用
Guid.NewGuid().toString()
作为文件名 - 安全且唯一。Most objects don't have usable names, and even if they would it may not necessary to be safe to be used as file name.
Unless you have good reason to expect good name on you object and actually use it, I'd recommend simply
Guid.NewGuid().toString()
as file name - safe and unique.ContentManager
有一个RootDirectory
属性,用于标识存储内容管理器内容的文件夹。该路径相对于可执行文件夹,因此您可以使用该路径以及加载内容时指定的名称构建内容的路径。例如,如果您像这样加载 .PNG 纹理:
那么可以像这样构建该纹理的路径:
请注意,您需要一些文件类型的知识才能知道扩展名是什么。这还假设您没有更改内容项属性中的名称,因此您在
Load
调用中使用的键与文件名匹配,默认情况下也是如此。如果您将内容存储在子文件夹中,那么您只需附加这些内容即可构建完整路径。
如果您需要完整的磁盘路径,而不是相对路径,那么您可以获取可执行路径并将其添加到
Content.RootDirectory
中。在这方面,谷歌是你的朋友。The
ContentManager
has aRootDirectory
property that identifies the folder where content manager content is stored. The path is relative to the executable folder, so you can build a path to the content using that, along with the name you specify when loading the content.For example, if you load a .PNG texture like this:
Then the path to that texture could be built like this:
Note that you need some knowledge of the file type in order to know what the extension is. This is also assuming that you've not change the name in the content item properties, so the key you use in the
Load
call matches the file name, which it does by default.If you store your content in subfolders then you just append those as well to build up the full path.
If you need the full disk path, rather than a relative one, then you can get the executable path and prepend that to
Content.RootDirectory
. Google is your friend in that regard.如果您的游戏是基于内容项目的,最简单的方法是使用内容项目路径根的相对路径。
在您的内容项目中:
在您的脚本中:
在您的引擎中
如果您想保存,我建议您以相同的方式继续,并强制保存在内容项目文件夹中。
If your game is content project based, the easiest is to use the path relatives to the content project path root.
In your Content Project:
In your Script:
In your Engine
If you want to save I recommend you proceed in the same way, and force to save in the content project folder.