Exemple #1
0
 public static void Destroy(UMemoryItem item)
 {
     if (item.IsUnderManager)
     {
         item.gameObject.SetActive(false);
     }
     else
     {
         GameObject.Destroy(item);
     }
 }
Exemple #2
0
        //外部使用接口
        public static T Instantiate <T>(string resourceName) where T : UMemoryItem
        {
            UMemoryItem ret = Instance.GetAInstance(resourceName, typeof(T).Name);

            if (ret == null)
            {
                return(null);
            }
            else
            {
                return(ret as T);
            }
        }
Exemple #3
0
        //内部实现

        /// <summary>
        /// 通过对象池创建对象
        /// </summary>
        /// <param name="resorceName"></param>
        /// Resource中 的路径
        /// <param name="itemName"></param>
        /// 该gameobject 下继承UMemoryItem的脚本名
        /// <returns></returns>
        private UMemoryItem GetAInstance(string resorceName, string itemName)
        {
            //初始池信息
            if (!memoryPools.ContainsKey(resorceName))
            {
                UMemoryItem item = tryCreateItem(resorceName, itemName);
                if (item)
                {
                    AddToPool(item.ResorceName, item);
                }
                else
                {
                    return(null);
                }
            }
            List <UMemoryItem> pool = memoryPools[resorceName];
            //尝试获得对象
            UMemoryItem it;

            if (pool.Count == 0)
            {
                it = tryCreateItem(resorceName, itemName);
            }
            else
            {
                it = pool[pool.Count - 1];

                if (it.GetType().Name == itemName)
                {
                    pool.RemoveAt(pool.Count - 1);
                }
                else
                {
                    Debug.Log("<color=red> " + itemName + "not in *" + resorceName + " </color>");
                    it = null;
                }
            }

            if (it == null)
            {
                return(null);
            }
            else
            {
                it.Reset();
                it.gameObject.SetActive(true);
                return(it);
            }
        }
Exemple #4
0
        private void RemoveFromPool(string rename, UMemoryItem item)
        {
            if (item.IsUnderManager)
            {
                item.IsUnderManager = false;
                createItems.Remove(item);
            }


            if (!memoryPools.ContainsKey(rename))
            {
                return;
            }
            List <UMemoryItem> pool = memoryPools[rename];

            pool.Remove(item);
        }
Exemple #5
0
        private void AddToPool(string rename, UMemoryItem item)
        {
            if (string.IsNullOrEmpty(rename) || !item.IsUnderManager)
            {
                return;
            }

            if (!memoryPools.ContainsKey(rename))
            {
                memoryPools.Add(rename, new List <UMemoryItem>());
            }
            List <UMemoryItem> pool = memoryPools[rename];

            if (capacity.ContainsKey(rename) && pool.Count >= capacity[rename])
            {
                GameObject.Destroy(item.gameObject);
                return;
            }

            pool.Add(item);
        }
Exemple #6
0
        private UMemoryItem tryCreateItem(string resorceName, string itemName)
        {
            if (!loaded.ContainsKey(resorceName))
            {
                GameObject obj = (GameObject)Resources.Load(resorceName);
                if (obj == null)
                {
                    Debug.Log("<color=red> " + resorceName + "   not in Resource </color>");
                    return(null);
                }
                else
                {
                    var item = obj.GetComponent(itemName) as UMemoryItem;
                    if (item == null)
                    {
                        Debug.Log("<color=red> " + itemName + " not in " + resorceName + " </color>");

                        return(null);
                    }
                    else
                    {
                        item.ResorceName    = resorceName;
                        item.IsUnderManager = true;
                        loaded.Add(resorceName, obj);
                    }
                }
            }
            GameObject  go  = MonoBehaviour.Instantiate(loaded[resorceName]) as GameObject;
            UMemoryItem ret = go.GetComponent(itemName) as UMemoryItem;

            if (ret == null)
            {
                Debug.Log("<color=red> " + itemName + "not in " + resorceName + " </color>");
                GameObject.Destroy(go);
                return(null);
            }
            createItems.Add(ret);
            return(ret);
        }
Exemple #7
0
 private void RemoveObject(UMemoryItem item)
 {
     RemoveFromPool(item.ResorceName, item);
 }
Exemple #8
0
 private void RecollectInstance(UMemoryItem item)
 {
     AddToPool(item.ResorceName, item);
 }