Exemple #1
0
    private ObjectPoolInstance CreateInstanceInGroup(ObjectPoolGroup group)
    {
        var go = Instantiate(group.PrefabObject, transform, false);

        go.SetActive(false);
        go.name            += "_" + group.PooledInstances.Count;
        go.transform.parent = group.Parent;
        var iPooleable = go.GetComponent(typeof(IPooleableObject)) as IPooleableObject;

        if (iPooleable == null)
        {
            Debug.LogError("IPooleableObject interface not found in " + go.name);
            return(null);
        }

        var instance = new ObjectPoolInstance
        {
            IsUsed            = false,
            Instance          = go,
            PooleableInstance = iPooleable
        };

        group.PooledInstances[instance.Instance.GetInstanceID()] = instance;
        return(instance);
    }
Exemple #2
0
 private void InitFreeList(int _offSet, int _len)
 {
     freeList[_offSet] = new ObjectPoolInstance(this);
     for (int i = 1 + _offSet; i < _len; i++)
     {
         freeList[i] = new ObjectPoolInstance(this);
         freeList[i - 1].SetNext(freeList[i]);
     }
     curAvailableInst = freeList[_offSet];
 }
Exemple #3
0
 public void Return(ObjectPoolInstance _instance)
 {
     lock (syncRoot)
     {
         temp         = _instance;
         temp.IsInUse = false;
         temp.SetNext(curAvailableInst);
         curAvailableInst = temp;
     }
 }
Exemple #4
0
    public bool IsSpawned(GameObject obj)
    {
        ObjectPoolInstance entry = null;

        if (PooledInstances.TryGetValue(obj.GetInstanceID(), out entry))
        {
            return(entry.IsUsed);
        }

        Debug.LogError("No instance found for " + obj.name);
        return(false);
    }
Exemple #5
0
    public void ReturnObject(GameObject instance)
    {
        ObjectPoolInstance instInfo = instance.GetComponent <ObjectPoolInstance>();

        if (instInfo == null)
        {
            Debug.LogErrorFormat("Attempting to add object which was not spawned from pool '{0}'", instance.name);
        }
        else
        {
            instance.transform.SetParent(transform);
            instance.SetActive(false);
            GetQueue(instInfo.m_SourceObject).Enqueue(instance);
        }
    }
Exemple #6
0
 public ObjectPoolInstance Fetch()
 {
     lock (syncRoot)
     {
         if (curAvailableInst == null)
         {
             Expand();
         }
         temp = curAvailableInst;
         curAvailableInst.IsInUse = true;
         curAvailableInst         = curAvailableInst.GetNext();
         temp.SetNext(null);
         return(temp);
     }
 }
Exemple #7
0
    public void ReturnInstance(GameObject obj)
    {
        ObjectPoolInstance entry = null;

        if (PooledInstances.TryGetValue(obj.GetInstanceID(), out entry))
        {
            entry.IsUsed = false;
            entry.Instance.SetActive(false);
            entry.PooleableInstance.OnAfterRecycle();
            ResetTransform(entry.Instance.transform);
            return;
        }

        Debug.LogError("No instance found for " + obj.name);
    }
Exemple #8
0
    private GameObject CreateNew(GameObject sourceType)
    {
        GameObject obj = Instantiate(sourceType);

        // Work around for inheritance container
        if (obj.GetComponent <InheritanceContainer>())
        {
            Debug.AssertFormat(obj.transform.childCount == 1, "Need 1 and only 1 child for inheritance container when object pooling");
            obj = obj.transform.GetChild(0).gameObject;
        }

        ObjectPoolInstance inst = obj.AddComponent <ObjectPoolInstance>();

        inst.m_SourceObject = sourceType;
        return(obj);
    }
Exemple #9
0
 private void Expand()
 {
     if (canExpand)
     {
         int newSize = size * 2;
         if (newSize > maxSize)
         {
             newSize   = maxSize;
             canExpand = false;
         }
         ObjectPoolInstance[] tempList = new ObjectPoolInstance[newSize];
         Array.Copy(freeList, tempList, size);
         freeList = tempList;
         InitFreeList(size, newSize);
         size = newSize;
     }
     else
     {
         curAvailableInst = new ObjectPoolInstance(null);//无法扩容的时候返回临时对象,它的回收交给GC
     }
 }
Exemple #10
0
    public GameObject Spawn(GameObject prefabObject, Vector3 position, Quaternion rotation)
    {
        var group = _poolGroup.Find(g => g.PrefabObject.name == prefabObject.name);

        if (group == null)
        {
            Debug.LogError("Group not found! Name: " + prefabObject.name);
            return(null);
        }

        ObjectPoolInstance instance = group.GetUnusedInstance();

        if (instance == null)
        {
            instance = CreateInstanceInGroup(group);
        }

        instance.Instance.SetActive(true);
        instance.Instance.transform.position = position;
        instance.Instance.transform.rotation = rotation;
        instance.PooleableInstance.OnSpawn();
        return(instance.Instance);
    }
 public void Release(string name, GameObject instanceObject)
 {
     if (m_ObjectMap.ContainsKey(name))
     {
         PoolObjectState          pos   = m_ObjectMap[name];
         List <PoolInstanceState> poiss = m_ObjectInstanceMap[name];
         for (int i = 0; i < poiss.Count; i++)
         {
             if (poiss[i].gameObject == instanceObject)
             {
                 poiss[i].state = PoolInstanceStateType.Sleep;
                 ObjectPoolInstance opi = instanceObject.GetComponent <ObjectPoolInstance>();
                 if (opi != null)
                 {
                     opi.OnPoolRelease();
                 }
                 instanceObject.SetActive(false);
                 instanceObject.transform.parent        = m_ObjectPool.transform;
                 instanceObject.transform.localScale    = Vector3.one;
                 instanceObject.transform.localPosition = Vector3.one;
             }
         }
     }
 }
 public GameObject Instantiate(string name)
 {
     if (m_ObjectMap.ContainsKey(name))
     {
         PoolObjectState          pos   = m_ObjectMap[name];
         List <PoolInstanceState> poiss = m_ObjectInstanceMap[name];
         int activeCount            = 0;
         int minId                  = -1;
         int maxId                  = 0;
         PoolInstanceState oldest   = null;
         PoolInstanceState sleepOne = null;
         for (int i = 0; i < poiss.Count; i++)
         {
             if (i == 0 && minId == -1)
             {
                 minId = poiss[i].stateId;
             }
             if (poiss[i].state == PoolInstanceStateType.Sleep)
             {
                 sleepOne = poiss[i];
             }
             else
             {
                 activeCount++;
                 if (poiss[i].stateId <= minId)
                 {
                     oldest = poiss[i];
                     minId  = oldest.stateId;
                 }
                 if (maxId < poiss[i].stateId)
                 {
                     maxId = poiss[i].stateId;
                 }
             }
         }
         if (sleepOne != null)
         {
             sleepOne.stateId = maxId + 1;
             sleepOne.state   = PoolInstanceStateType.Active;
             ObjectPoolInstance opi = sleepOne.gameObject.GetComponent <ObjectPoolInstance>();
             if (opi != null)
             {
                 opi.OnPoolInstantiated();
             }
             return(sleepOne.gameObject);
         }
         else
         {
             if (activeCount >= pos.max)
             {
                 sleepOne         = oldest;
                 sleepOne.stateId = maxId + 1;
                 sleepOne.state   = PoolInstanceStateType.Active;
                 ObjectPoolInstance opi = sleepOne.gameObject.GetComponent <ObjectPoolInstance>();
                 if (opi != null)
                 {
                     opi.OnPoolRelease();
                     opi.OnPoolInstantiated();
                 }
                 return(sleepOne.gameObject);
             }
             else
             {
                 GameObject goI = Instantiate <GameObject>(pos.gameObject);
                 goI.SetActive(false);
                 goI.transform.parent        = m_ObjectPool.transform;
                 goI.transform.localScale    = Vector3.one;
                 goI.transform.localPosition = Vector3.one;
                 PoolInstanceState pis = new PoolInstanceState();
                 pis.gameObject = goI;
                 pis.name       = pos.name;
                 pis.stateId    = maxId + 1;
                 pis.state      = PoolInstanceStateType.Active;
                 m_ObjectInstanceMap[name].Add(pis);
                 ObjectPoolInstance opi = goI.GetComponent <ObjectPoolInstance>();
                 if (opi != null)
                 {
                     opi.OnPoolInstantiated();
                 }
                 return(goI);
             }
         }
     }
     return(null);
 }
Exemple #13
0
 public void SetNext(ObjectPoolInstance _next)
 {
     next = _next;
 }
Exemple #14
0
 public ObjectPoolInstance(ObjectPool <T> _pool)
 {
     IsInUse    = false;
     next       = null;
     IsFromPool = _pool != null;
 }