想象一下你正在做的是使用一些紋理遊戲對象:如果null
傳遞如何使用繼承緩存類的靜態數據?
public class Action {
//This two can (must) be overriden
public const string _HoverCursor = "Textures/cursors/select";
public virtual string HoverCursor { get { return _HoverCursor; } }
//This is the get texture by string
private static Texture2D cursorTex = null;
public static Texture2D cursor { get { return ResourceManager.loadTexture(ref cursorTex, HoverCursor); } }
}
的loadTexture
功能將加載質感。否則,它只是返回原始值。
正如你所看到的,我正在製作一個Action原型。會出現類似的攻擊,移動或Build。我想爲每個類定義存儲一個(和不同的)紋理對象。這個對象應該是lazy-loaded when needed使用上面的getter。
由於static property can't be overriden,我怎麼能實現這個所有的兒童實例?
這裏的loadTexture
:
public static Texture2D loadTexture(ref Texture2D target, string name)
{
if (target == null)
{
target = (Texture2D)Resources.Load(name, typeof(Texture2D));
}
return target;
}
這真的很酷,但它有效嗎?我希望數據是靜態的,以節省RAM,但CPU更重要,尤其是這些日子裏所有這些8GB RAM ...... – 2014-11-12 17:12:54
@TomášZato是啊...嗯,我想你會加載紋理或任何東西,你應該卸載他們,如果你不需要他們在某些情況下。順便說一句,看看'懶惰' –
2014-11-12 23:31:42
有趣。不知道「懶惰」,我寫了一個'LazyResource 'class ... –
2014-11-13 00:08:41