これを行うには、このヘルパー クラスを使用できます。これは、指定されたディレクトリを取得し、GetFiles()
メソッドを使用して、ロードする必要があるすべてのテクスチャのリストを作成することによって機能します。次に、通常どおりにそれらをロードし、ContentManager
それらを辞書に入れて使用できるようにします。
public static class TextureContent
{
public static Dictionary<string, T> LoadListContent<T>(this ContentManager contentManager, string contentFolder)
{
DirectoryInfo dir = new DirectoryInfo(contentManager.RootDirectory + "/" + contentFolder);
if (!dir.Exists)
throw new DirectoryNotFoundException();
Dictionary<String, T> result = new Dictionary<String, T>();
FileInfo[] files = dir.GetFiles("*.*");
foreach (FileInfo file in files)
{
string key = Path.GetFileNameWithoutExtension(file.Name);
result[key] = contentManager.Load<T>(contentFolder + "/" + key);
}
return result;
}
}
Texture2D
s の行ごとではなく、テクスチャを格納するための辞書を作成します。
public Dictionary<string, Texture2D> spriteContent;
...そして、メソッドでメソッドを呼び出しLoadContent
ます
spriteContent = TextureContent.LoadListContent<Texture2D>(content, "textures");
テクスチャが必要なときはいつでも、次のようにします。
Whatever.Image = spriteContent["WhateverTexture"]
TextureName
がテクスチャのアセット名であることを確認してください。