我有一個基類:傳承 - 基於孩子值在初始化時變變
class Tile{}
而其他幾個延伸瓷磚
class Free : Tile{}
class Wall : Tile{}
每個瓷磚有它自己的紋理,它不是字符串,但它的Texture2D必須在初始化時加載。我想代碼將類似於這一點,但我不知道如何正確地創建此:
class Tile{
static Texture2D texture; //Static will use less ram because it will be same for inherited class?
static string texture_path; //This is set by inherited class
public Tile(){
if(texture==null)
texture = LoadTexture(texture_path);
}
}
class Free : Tile{
static string texture_path = "Content/wall.png";
}
隨着換言之,所有的免費瓷磚具有相同的紋理和所有牆磚具有相同的紋理 - 這就是爲什麼在我意見我應該使用靜態。
如何正確執行此操作?
什麼是你的問題?問題通常有一個問號。 –