unten ist meine allgemeine Schnittstelle, die für jede Klasse platziert wird, die ich möglicherweise bündeln möchte
Code: Select all
public interface IPoolable where T : MonoBehaviour, IPoolable
{
///This is just a reference to a list so that any object can pool itself back
public Pool Pool { get; set; }
///
/// determines if pooled object is new spawn to run any function needed
/// to initialise the object. Manually set it to false after you run any custom initialisation function
///
public bool IsNewSpawn { get; set; }
public bool IsPooled { get; set; }
///
/// Implement a function to add itself back into the pool.
/// Use to replace destroy function in execution
///
public void PoolSelf();
}
Dies ist das Objekt, das gepoolt wird
Code: Select all
public class PooledObject : IPoolable
{
public Pool Pool { get; set; }
public bool IsNewSpawn { get; set; }
public bool IsPooled { get; set; }
public void PoolSelf()
{
///whatever you need to do when this object needs to be pooled
///you then run a function in the pool which pools this object
}
///say these are values i need to initialize on object spawn but not when it gets merely cached to be reused later
public int randomValue;
public string randomText;
public vector3 randomVector;
// i now setup an initialization function with variable parameter input based on what this class need
public void Initialize(int value1, string value2, vector3 value3)
{
//do field assignment etc or anything else i may need
}
}
Code: Select all
public class Spawner
{
//reference to the pool, i handle the assignment of reference with a static pool manager elsewhere
private Pool ObjPool;
//a potential spawn method
public void SpawnObj()
{
//this gets me a pooled object etc.
PooledObject newSpawn = ObjPool.GetPooledObj();
//now i need to manually check if the newSpawn is actually a new object before i run the custom initialize method in the pooledobject
}
}
Jeder Designvorschlag ist willkommen.
Mobile version