Exemplo n.º 1
0
        /// <summary>
        /// 加载一个对象并把它实例化
        /// </summary>
        /// <param name="gameObjectName">对象名</param>
        /// <param name="parent">对象的父节点,可空</param>
        /// <returns></returns>
        CPoolEntity CreatePoolObject(string gameObjectName, GameObject parent = null)
        {
            //CResourceManager.InstantiatePrefab();
            GameObject go = null;//ResourceManager.Load<GameObject>(gameObjectName);

            if (go == null)
            {
                throw new Exception("CreatPoolObject error dont find : ->" + gameObjectName + "<-");
            }

            GameObject instanceTmp = Instantiate(go);

            instanceTmp.name = go.name;

            CPoolEntity po = instanceTmp.GetComponent <CPoolEntity>();

            if (po == null)
            {
                throw new Exception("CreatPoolObject error : ->" + gameObjectName + "<- not is PoolObject !");
            }

            po.OnCreate();

            if (parent != null)
            {
                instanceTmp.transform.SetParent(parent.transform);
            }

            instanceTmp.SetActive(true);

            return(po);
        }
Exemplo n.º 2
0
        /// <summary>
        /// 将一个对象放入对象池
        /// </summary>
        /// <param name="obj">目标对象</param>
        public void DestroyPoolObject(CPoolEntity obj)
        {
            string key = obj.name.Replace("(Clone)", "");

            if (s_objectPool_new.ContainsKey(key) == false)
            {
                s_objectPool_new.Add(key, new List <CPoolEntity>());
            }

            if (s_objectPool_new[key].Contains(obj))
            {
                throw new Exception("DestroyPoolObject:-> Repeat Destroy GameObject !" + obj);
            }

            s_objectPool_new[key].Add(obj);

            if (obj.SetActive)
            {
                obj.gameObject.SetActive(false);
            }
            else
            {
                obj.transform.position = m_outOfRange;
            }

            obj.OnRecycle();

            obj.name = key;
            obj.transform.SetParent(PoolParent);
        }