示例#1
0
        /// <summary>
        /// Create all of our prefab pools
        /// </summary>
        void Start()
        {
            foreach (var poolEntry in m_Prefabs)
            {
                List <PoolEntry> pool = new List <PoolEntry>(poolEntry.m_PoolSize);
                for (int i = 0; i < poolEntry.m_PoolSize; ++i)
                {
                    PoolEntry entry = CreatePoolEntry(poolEntry.m_Prefab);
                    pool.Add(entry);
                }

                m_Pool.Add(poolEntry.m_Prefab.GetInstanceID(), pool);
            }
        }
示例#2
0
        /// <summary>
        /// Create a game object from a prefab
        /// </summary>
        /// <param name="prefab">The prefab to clone</param>
        /// <param name="position">The position to place the object</param>
        /// <param name="parent">The new parent to assign to the game object</param>
        /// <returns>The cloned game object</returns>
        public GameObject Create(GameObject prefab, Vector3 position, Transform parent = null)
        {
            Debug.Assert(prefab != null);

            List <PoolEntry> pool      = null;
            PoolEntry        poolEntry = null;

            int id = prefab.GetInstanceID();

            if (m_Pool.ContainsKey(id))
            {
                pool = m_Pool[id];
                foreach (var entry in pool)
                {
                    if (!entry.m_InUse)
                    {
                        poolEntry = entry;
                        break;
                    }
                }
            }
            else
            {
                pool = new List <PoolEntry>();
                m_Pool.Add(prefab.GetInstanceID(), pool);
            }

            if (poolEntry == null)
            {
                poolEntry = CreatePoolEntry(prefab);
                pool.Add(poolEntry);
            }

            poolEntry.m_InUse = true;

            if (parent != null)
            {
                poolEntry.m_GO.transform.parent = parent;
            }

            poolEntry.m_GO.transform.position = position;

            poolEntry.m_GO.SetActive(true);
            SendMessage(poolEntry.m_GO, "Awake", true);
            SendMessage(poolEntry.m_GO, "Start", true);

            return(poolEntry.m_GO);
        }
示例#3
0
        /// <summary>
        /// Create a new pool entry
        /// </summary>
        /// <param name="prefab">The prefab to clone</param>
        /// <returns>The cloned game object</returns>
        private PoolEntry CreatePoolEntry(GameObject prefab)
        {
            PoolEntry entry = new PoolEntry();

            entry.m_GO = GameObject.Instantiate(prefab);
            entry.m_GO.SetActive(false);
            entry.m_GO.transform.parent = transform;

            entry.m_InUse = false;

            PooledObject pooledObject = entry.m_GO.AddComponent <PooledObject>();

            pooledObject.PoolID = prefab.GetInstanceID();

            return(entry);
        }