示例#1
0
    public static T Spawn <T>(string poolName, Vector3 position, Transform parent) where T : MonoBehaviour
    {
        T result = null;

        // Try to get the object from the pool
        foreach (var pi in pools[poolName])
        {
            if (!pi.GameObject.activeSelf)
            {
                pi.Transform.parent   = parent;
                pi.Transform.position = position;
                pi.GameObject.SetActive(true);
                result = pi.Script as T;
                break;
            }
        }

        // Otherwise, instanitate
        if (result == null)
        {
            tmpPI = PoolItem.Create <T>((GameObject)Instantiate(prefabs[poolName]));
            tmpPI.Transform.parent   = parent;
            tmpPI.Transform.position = position;
            pools[poolName].Add(tmpPI);
            result = tmpPI.Script as T;
        }

        if (result is IObjectPoolable)
        {
            ((IObjectPoolable)result).Initialize();
        }

        return(result);
    }
    public static T Spawn <T>(string poolName, Vector3 position, Transform parent) where T : MonoBehaviour
    {
        // Try to get the object from the pool
        foreach (var pi in pools[poolName])
        {
            if (!pi.GameObject.activeSelf)
            {
                pi.Transform.parent   = parent;
                pi.Transform.position = position;
                pi.GameObject.SetActive(true);
                return(pi.Script as T);
            }
        }

        // Otherwise, instanitate
        tmpPI = PoolItem.Create <T>((GameObject)Instantiate(prefabs[poolName]));
        tmpPI.Transform.parent   = parent;
        tmpPI.Transform.position = position;
        pools[poolName].Add(tmpPI);
        return(tmpPI.Script as T);
    }