Example #1
0
    //---------------------------------------------------------------------------------------------------------------
    public void AddPool(ObjectPoolData data, Transform container)
    {
        if (GetPool(data.name) != null)
        {
            Debug.LogError("PoolManager => pool " + data.name + " already exists. Removing old instance.");
            this.DestroyPool(data.name);
            // return; here was a bug, we destroy old, but not allow new to proceed. However, it still shouldn't happen.
        }

        if (container == null)
        {
            Debug.LogError("PoolManager => no container for pool " + data.name + ".");
            return;
        }

        if (data.HasRoot)
        {
            data.Root.ListenRootUnloaded(() => DestroyPool(data.name));
        }


        GameObject parent;

        if (container != null)
        {
            parent = container.gameObject;
        }
        else
        {
            parent = new GameObject(data.name + "Pool");
            parent.transform.SetParent(this.transform);
        }

        pools.Add(new ObjectPool(data, parent));
    }
Example #2
0
    public void InitObjectsInPool(Object obj, string kind, int iCount)
    {
        int iSlot = FindEmptySlot();

        // Not found, return or resize buffer.
        if (iSlot < 0)
        {
            return;
        }
        m_iCount[iSlot]      = iCount;
        m_GameObjects[iSlot] = new List <ObjectPoolData>();
        for (int i = 0; i < iCount; i++)
        {
            GameObject go = Instantiate(obj) as GameObject;
            if (go == null)
            {
                break;
            }
            //ShowModel(go, false);
            go.SetActive(false);
            ObjectPoolData objData = new ObjectPoolData();
            objData.m_go     = go;
            objData.m_bUsing = false;
            m_GameObjects[iSlot].Add(objData);
        }
        dict_KindIndex.Add(kind, iSlot);
    }
Example #3
0
	public GameObject InitObjectsInPool(Object obj, int iCount)
	{
		int iSlot = FindEmptySlot();
		GameObject go = null;
		// Not found, return or resize buffer.
		if(iSlot < 0) {
			return go;	
		}
		m_iCount = iCount;
		m_GameObjects[iSlot] = new List<ObjectPoolData>();
		for(int i = 0; i < iCount; i++) {
			go = Instantiate(obj) as GameObject;
			if(go == null) {
				break;
			}
			EnableModel(go, false); //不顯示建立的物件
			//將建立一個新的ObjectPoolData,存物件的狀態(開始為false)
			ObjectPoolData objData = new ObjectPoolData();
			objData.m_go = go;
			objData.m_bUsing = false;
			//將第iSlot的m_GameObjects,增加一個建立的物件
			m_GameObjects[iSlot].Add(objData);
		}
		return go;
	}
    private void Stream_ObjectToAddToPool(int viewID, string poolID)
    {
        ObjectPoolData poolData  = pool.Where(o => o.poolID == poolID).First();
        GameObject     newObject = NetworkManager.GetViewByID(viewID).gameObject;

        poolData.pooledObjects.Add(newObject);
    }
    public static void Set_ObjectFromPool(string objectIndentifier, Vector3 pos, Vector3 orientation)
    {
        ObjectPoolData data           = poolData.Where(o => o.poolID == objectIndentifier).First();
        GameObject     objectToReturn = null;

        if (data.pooledObjects.Count > 0)
        {
            for (int i = 0; i < data.pooledObjects.Count; i++)
            {
                if (data.pooledObjects[i].gameObject.activeSelf == false)
                {
                    if ((int)data.pooledObjects[i].transform.GetComponent <InteractableObject>().ownedByTeam == NetworkManager.localPlayerInformation.team)
                    {
                        objectToReturn = data.pooledObjects[i];
                    }
                    break;
                }
            }
        }

        if (objectToReturn == null)
        {
            objectToReturn = PhotonNetwork.InstantiateSceneObject(data.pooledObjects.First().name, pos, Quaternion.Euler(orientation));
            int newObjectViewID = objectToReturn.GetPhotonView().ViewID;

            OnAddObjectToPool.Invoke(newObjectViewID);
            OnSetObjectState.Invoke(true, newObjectViewID);
            OnSetObjectTransform.Invoke(newObjectViewID, pos, orientation);
            return;
        }

        OnAddObjectToPool.Invoke(objectToReturn.GetPhotonView().ViewID);
        OnSetObjectState.Invoke(true, objectToReturn.GetPhotonView().ViewID);
        OnSetObjectTransform.Invoke(objectToReturn.GetPhotonView().ViewID, pos, orientation);
    }
    private void Start()
    {
        prefab.gameObject.SetActive(false);
        this.container = this.transform;

        ObjectPoolData data = new ObjectPoolData(name, prefab, initCapacity, root);

        Game.PoolManager.AddPool(data: data, container: container);
    }
Example #7
0
        public override bool AddSubPool(ObjectPoolData data)
        {
            GameObject subPool = new GameObject(data.Name + "Pool");

            subPool.transform.SetParent(_poolRoot.transform);
            _parentDic[data.Name] = subPool;

            return(base.AddSubPool(data));
        }
Example #8
0
    //---------------------------------------------------------------------------------------------------------------
    public ObjectPool(ObjectPoolData data, GameObject parent)
    {
        this.data   = data;
        this.parent = parent;

        stack      = new Stack <APoolable>(data.initialCapacity);
        itemsInUse = new List <APoolable>();

        for (int i = 0; i < data.initialCapacity; i++)
        {
            AddInstance();
        }
    }
Example #9
0
        private void InitEffectPool()
        {
            GameObject effectParnet = new GameObject("EffectPool");

            effectParnet.transform.SetParent(transform);
            EffectPool = new GameObjectPool(effectParnet);
            GameObject     temp = null;
            ObjectPoolData data;

            for (EffectNameEnum name = 0; name < EffectNameEnum.COUNT; name++)
            {
                temp = LoadManager.Single.Load <GameObject>(Path.EFFECTS_PATH, name.ToString());
                data = new ObjectPoolData(temp, name.ToString());
                EffectPool.AddSubPool(data);
            }
        }
 // 實體化物件
 public int InstantiateObjectsInObjectPool(Object obj, int amounts) {
     // 找到空的容器
     int slotIndex = FindEmptySlot();
     // 實體化物件,並且將物件丟入容器管理
     m_containers[slotIndex] = new List<ObjectPoolData>();
     GameObject tempGameObject;
     ObjectPoolData tempObjectPoolData;
     for (int i = 0; i < amounts; i++) {
         tempGameObject = MonoBehaviour.Instantiate(obj) as GameObject;
         setShowHideObject(tempGameObject, false);
         tempObjectPoolData = new ObjectPoolData();
         tempObjectPoolData.m_gameObject = tempGameObject;
         m_containers[slotIndex].Add(tempObjectPoolData);
     }
     // 回傳容器編碼
     return slotIndex;
 }
Example #11
0
    public void DestroyPoolSlot(string kind)
    {
        int iSlot = dict_KindIndex[kind];

        if (iSlot < 0 || iSlot >= m_iNumGameObjectInType)
        {
            return;
        }
        int iCount = m_GameObjects[iSlot].Count;

        for (int i = 0; i < iCount; i++)
        {
            ObjectPoolData objData = m_GameObjects[iSlot][i];
            Destroy(objData.m_go);
            m_GameObjects[iSlot][i] = null;
        }
        m_GameObjects[iSlot] = null;
    }
    // 實體化物件
    public int InstantiateObjectsInObjectPool(Object obj, int amounts)
    {
        // 找到空的容器
        int slotIndex = FindEmptySlot();

        // 實體化物件,並且將物件丟入容器管理
        m_containers[slotIndex] = new List <ObjectPoolData>();
        GameObject     tempGameObject;
        ObjectPoolData tempObjectPoolData;

        for (int i = 0; i < amounts; i++)
        {
            tempGameObject = MonoBehaviour.Instantiate(obj) as GameObject;
            setShowHideObject(tempGameObject, false);
            tempObjectPoolData = new ObjectPoolData();
            tempObjectPoolData.m_gameObject = tempGameObject;
            m_containers[slotIndex].Add(tempObjectPoolData);
        }
        // 回傳容器編碼
        return(slotIndex);
    }
    public static bool HasPooledObjectAvailable(string identifier)
    {
        ObjectPoolData data = poolData.Where(o => o.poolID == identifier).First();

        if (data.pooledObjects.Count > 0)
        {
            for (int i = 0; i < data.pooledObjects.Count; i++)
            {
                if (data.pooledObjects[i] != null)
                {
                    if ((int)data.pooledObjects[i].GetComponent <InteractableObject>().ownedByTeam == NetworkManager.localPlayerInformation.team)
                    {
                        if (data.pooledObjects[i].gameObject.activeSelf == false)
                        {
                            return(true);
                        }
                    }
                }
            }
        }

        return(false);
    }
Example #14
0
    public GameObject LoadObjectFromPool(string kind)
    {
        int iSlot = dict_KindIndex[kind];

        if (iSlot < 0 || iSlot >= m_iNumGameObjectInType)
        {
            return(null);
        }
        GameObject go     = null;
        int        iCount = m_GameObjects[iSlot].Count;

        for (int i = 0; i < iCount; i++)
        {
            ObjectPoolData objData = m_GameObjects[iSlot][i];
            if (objData.m_bUsing == false)
            {
                go = objData.m_go;
                go.SetActive(true);
                objData.m_bUsing = true;
                break;
            }
        }
        return(go);
    }
Example #15
0
    public bool UnLoadObjectToPool(string kind, GameObject go)
    {
        int iSlot = dict_KindIndex[kind];

        if (iSlot < 0 || iSlot >= m_iNumGameObjectInType)
        {
            return(false);
        }
        bool bRet   = false;
        int  iCount = m_GameObjects[iSlot].Count;

        for (int i = 0; i < iCount; i++)
        {
            ObjectPoolData objData = m_GameObjects[iSlot][i];
            if (objData.m_go == go)
            {
                objData.m_bUsing = false;
                go.SetActive(false);
                bRet = true;
                break;
            }
        }
        return(bRet);
    }
Example #16
0
 public SubPoolItemPool(ObjectPoolData data, GameObject parent) : base(data)
 {
     _parent = parent;
     PrespawnItem();
 }
Example #17
0
 protected override SubObjectPoolBase <PoolItem> GetNewSubPool(ObjectPoolData data)
 {
     return(new SubPoolItemPool(data, _parentDic[data.Name]));
 }