/// <summary> /// 从缓存池中得到指定类型的组件 /// </summary> /// <typeparam name="T">继承于MonoBehaviour的组件</typeparam> /// <param name="isAutoActive">是否激获取到的GameObject,默认为true</param> /// <returns></returns> public T GetComponentItem <T>(bool isAutoActive = true, bool isAddIfNotFind = false) where T : MonoBehaviour { if (m_InstanceOrPrefabTemplate.GetComponent <T> () == null && !isAddIfNotFind) { return(null); } GameObject gObj = GetPoolItem(isAutoActive); T component = null; if (gObj != null) { component = gObj.GetComponent <T>(); if (component == null) { component = gObj.AddComponent <T>(); GameObjectPoolItem poolItem = component as GameObjectPoolItem; if (poolItem != null) { poolItem.SpawnName = m_SpawnPool.PoolName; poolItem.AssetPath = m_AssetPath; poolItem.DoSpawned(); } } } return(component); }
/// <summary> /// 从缓存池中得到一个GameObject对象 /// </summary> /// <param name="isAutoSetActive">是否激获取到的GameObject,默认为true</param> /// <returns></returns> public GameObject GetPoolItem(bool isAutoSetActive = true) { if (LimitMaxAmount != 0 && m_UsedItemList.Count > LimitMaxAmount) { DebugUtility.LogWarning(LOG_TAG, "GameObjectPool::GetItem->Large than Max Amount"); return(null); } GameObject item = null; if (m_UnusedItemQueue.Count > 0) { item = m_UnusedItemQueue.Dequeue(); } else { item = CreateNewItem(); } if (item != null) { GameObjectPoolItem poolItem = item.GetComponent <GameObjectPoolItem>(); if (poolItem != null) { poolItem.DoSpawned(); } } if (isAutoSetActive) { item.SetActive(true); } m_UsedItemList.Add(new WeakReference <GameObject>(item)); return(item); }