【发布时间】:2015-04-22 20:00:14
【问题描述】:
有时(并非总是如此,大多数时候它可以完美运行),当它无法加载 Texture2D 时,我会捕捉到我提供的异常。
public static class Extras
{
public static class Load
{
private static Dictionary<string, Texture2D> Textures;
public static Texture2D Texture(string Path)
{
if (Textures == null) Textures = new Dictionary<string, Texture2D>();
if (Textures.ContainsKey(Path)) return Textures[Path];
else
{
try { Textures.Add(Path, Service<ContentManager>().Load<Texture2D>(Path)); return Textures[Path];
catch { throw new ArgumentNullException(string.Format("Failed to load Texture2D from \"{0}\"!", Path)); }
}
return null;
}
}
public static class Services
{
private static GameServiceContainer Container;
public static T Get<T>() { return (T)Container.GetService(typeof(T)); }
public static void Add<T>(T Service) { if (Container == null) Container = new GameServiceContainer(); Container.AddService(typeof(T), Service); }
public static void Remove<T>() { Container.RemoveService(typeof(T)); }
}
public static T Service<T>() { return Services.Get<T>(); }
}
-
游戏加载时:
Extras.Services.Add<ContentManager>(Content);
Texture2D Texture = Extras.Load.Texture("Textures\\Player");
现在大多数情况下它都能正常工作,但有时我会遇到异常(第一次将纹理加载到游戏中时)。
为什么加载 Texture2D 总是失败?
【问题讨论】:
-
究竟是什么异常?
-
@Silveor "catch { throw new ArgumentNullException(string.Format("Failed to load Texture2D from \"{0}\"!", Path)); }"