// Bad code! Do not use! public sealed class Singleton { static Singleton instance=null; Singleton() { } public static Singleton Instance { get { if (instance==null) { instance = new Singleton(); } return instance; } } }
public sealed class Singleton { private static Singleton instance = null;//volatile 可以禁止 private static readonly object padlock = new object();
Singleton() { }
public static Singleton Instance { get { if (instance == null) { lock (padlock) { if (instance == null) { var tmp= new Singleton(); // ensures that the instance is well initialized, // and only then, it assigns the static variable. System.Threading.Thread.MemoryBarrier(); instance = tmp; } } } return instance; } } }
public void Trim(){ while(dormantObjects.Count>Capacity){ GameObject dob = dormantObjects[0]; dormantObjects.RemoveAt(0); Destory(dob); } }
PROBLEMS 1. This pool is not able to manage
the Load/Unload of prefabs. 2. Only dormant objects are managed
in the pool, active objects must be managed out of pool
separately. 3. The total number of dormant objects can be
controlled, rather than the instances of each
prefabs.(总物体有数量限制,而不是每中类型没有限制)
Spawnpool:design rules for spawnpool 1. define an
empty object,set its transform as the parent of all the instances in the
pool.(空物体) 2. manager multiple prefabpools via a dictionary
prefabPool 1. create a prefabPool for each prefab.
2. maintains a list of activated objects and another list of deactive
objects. 3. centrally manage the load/unload process here.