我有一个像这样拥有 Texture2D 的类:
public class TextureData
{
public Texture2D texture;
}
当我加载所有纹理时,我通过以下方法处理它:
public void LoadAllTextures()
{
foreach(string s in texturesToBeLoaded)
{
TextureData data = new TextureData();
LoadTexture(s, data.texture);
// data.texture is still null.
}
}
public void LoadTexture(string filename, Texture2D texture)
{
texture = content.Load<Texture2D>(filename);
// texture now holds the texture information but it doesn't
// actually retain it when the method ends...why?
}
我在这里错过了什么吗?如果我改变
public void LoadTexture(string filename, Texture2D texture)
到
public void LoadTexture(string filename, out Texture2D texture)
它工作正常。
编辑:好吧,我现在理解的方式是这样的......
public void LoadAllTextures()
{
foreach(string s in texturesToBeLoaded)
{
TextureData data = new TextureData();
// here, data.texture's memory address == 0x0001
LoadTexture(s, data.texture /*0x0001*/);
}
}
public void LoadTexture(string filename, Texture2D texture /* this is now 0x0001 */)
{
texture = content.Load<Texture2D>(filename);
// now the PARAMETER is set to 0x0002, not data.texture itself.
}