Example #1
0
        /// <summary>
        /// 放入缓存
        /// </summary>
        /// <param name="cop"></param>
        public static bool StoreCache(ReferGameObjects refer)
        {
            HashSet <ReferGameObjects> prefabRefers = null;
            int keyHash = refer.cacheHash;

            if (prefabRefCaches.TryGetValue(keyHash, out prefabRefers))
            {//从引用列表寻找
                bool isremove = prefabRefers.Remove(refer);
                if (isremove)
                {
                    Queue <ReferGameObjects> prefabFree = null;
                    if (prefabFreeQueue.TryGetValue(keyHash, out prefabFree) && !prefabFree.Contains(refer))
                    {
                        prefabFree.Enqueue(refer);
#if HUGULA_CACHE_DEBUG
                        Debug.LogFormat("StoreCache key={1},keystr={0},freeQueue.count={2},refers.count={3},frame={4}", refer.name + "   " + refer.cacheHash.ToString(), GetStringKey(keyHash), prefabFree.Count, prefabRefers.Count, Time.frameCount);
#endif
                        if (prefabRefers.Count == 0 && prefabsType[keyHash] < SegmentSize)
                        {
                            AddRemoveMark(keyHash);                                                               //如果引用为0标记回收
                        }
                        return(true);
                    }
                    else
                    {
                        Debug.LogWarningFormat("StoreCache refer(name={0},hash={1}) prefabFreeQueue is null ", refer.name, GetStringKey(refer.cacheHash));
                    }
                }
            }
            return(false);
        }
Example #2
0
        /// <summary>
        /// 清理当前key的对象
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        internal static void ClearKey(int key)
        {
            HashSet <ReferGameObjects> refers = null;

            if (prefabRefCaches.TryGetValue(key, out refers) && refers.Count == 0)
            {
                Queue <ReferGameObjects> freequeue;
                ReferGameObjects         refer = null;
                if (prefabFreeQueue.TryGetValue(key, out freequeue))
                {
                    while (freequeue.Count > 0)
                    {
                        refer = freequeue.Dequeue();
                        if (refer)
                        {
                            GameObject.Destroy(refer.gameObject);
                        }
                    }
                }

                Remove(key); //移除原始项目
            }
#if UNITY_EDITOR
            else
            {
                Debug.LogFormat("<color=yellow>the object(name={1},hash={0}) has refers</color>", key, Get(key) != null ? Get(key).name : "null");
            }
#endif
        }
Example #3
0
        /// <summary>
        /// 放入缓存
        /// </summary>
        /// <param name="cop"></param>
        public static bool StoreCache(ReferGameObjects refer)
        {
            HashSet <ReferGameObjects> prefabCachesMe = null;
            int keyHash = refer.cacheHash;

            if (prefabRefCaches.TryGetValue(keyHash, out prefabCachesMe))
            {//从引用列表寻找
                bool isremove = prefabCachesMe.Remove(refer);
                if (isremove)
                {
                    Queue <ReferGameObjects> prefabQueueMe = null;
                    if (prefabFreeQueue.TryGetValue(keyHash, out prefabQueueMe))
                    {
                        prefabQueueMe.Enqueue(refer);//入列

                        if (prefabCachesMe.Count == 0 && prefabsType[keyHash] < SegmentSize)
                        {
                            AddRemoveMark(keyHash);                                                                 //如果引用为0标记回收
                        }
                        return(true);
                    }
                }
            }
            return(false);
        }
Example #4
0
 void OnDisable()
 {
     if (isStart)
     {
         //Debug.Log(string.Format("AutoStoreCache name = {0} cacheHash={1} enable = {2} ", this.name, refer != null ? refer.cacheHash : 0,this.enabled));
         if (refer == null) refer = GetComponent<ReferGameObjects>();
         if (refer != null) PrefabPool.StoreCache(refer);
     }
 }
Example #5
0
        /// <summary>
        /// 获取可用的实例
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        internal static ReferGameObjects GetCache(int key)
        {
            ReferGameObjects refer = null;
            //空闲队列
            Queue <ReferGameObjects> prefabQueueMe = null;

            if (!prefabFreeQueue.TryGetValue(key, out prefabQueueMe))
            {
                return(null);
            }

            //引用列表
            HashSet <ReferGameObjects> prefabCachesMe = null;

            if (!prefabRefCaches.TryGetValue(key, out prefabCachesMe))
            {
                return(null);
            }

            //移除回收引用
            DeleteRemveMark(key);

            if (prefabQueueMe.Count > 0)
            {
                refer = prefabQueueMe.Dequeue();//出列
                //保持引用
                prefabCachesMe.Add(refer);
                return(refer);
            }
            else
            {
                GameObject prefab = Get(key);
                if (prefab == null) //如果没有 返回NUll
                {
                    return(null);
                }

                GameObject clone = (GameObject)GameObject.Instantiate(prefab);
                var        comp  = clone.GetComponent <ReferGameObjects>();
                if (comp == null)
                {
                    comp = clone.AddComponent <ReferGameObjects>();
                }
                refer           = comp;
                refer.cacheHash = key;
                //保持引用
                prefabCachesMe.Add(refer); //放入引用列表
            }

            return(refer);
        }
Example #6
0
 /// <summary>
 /// 真正销毁对象
 /// </summary>
 /// <param name="count"></param>
 void DisposeRefer(int count)
 {
     if (count > willGcList.Count)
     {
         count = willGcList.Count;
     }
     while (count > 0)
     {
         referRemove = willGcList.Dequeue();
         //Debug.Log("Dispose " + referRemove.name);
         if (referRemove)
         {
             GameObject.Destroy(referRemove.gameObject);
         }
         count--;
     }
     referRemove = null;
 }
Example #7
0
        /// <summary>
        /// 清理当前key的对象
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        internal static void ClearKey(int key)
        {
            Queue <ReferGameObjects> freequeue;
            ReferGameObjects         refer = null;

            if (prefabFreeQueue.TryGetValue(key, out freequeue))
            {
                while (freequeue.Count > 0)
                {
                    refer = freequeue.Dequeue();
                    willGcList.Enqueue(refer);//入队
                }
            }
#if UNITY_EDITOR
            //Debug.LogFormat("<color=yellow>ClearCache {0} key{1}</color>", refer == null ? "" : refer.name, refer.GetComponent<ReferenceCount>().assetBundleName);
#endif
            Remove(key); //移除原始项目
        }
Example #8
0
        /// <summary>
        /// 放入缓存
        /// </summary>
        /// <param name="cop"></param>
        public static bool StoreCache(ReferGameObjects refer)
        {
            HashSet <ReferGameObjects> prefabCachesMe = null;

            if (prefabRefCaches.TryGetValue(refer.cacheHash, out prefabCachesMe))
            {//从引用列表寻找
                bool isremove = prefabCachesMe.Remove(refer);
                if (isremove)
                {
                    Queue <ReferGameObjects> prefabQueueMe = null;
                    if (prefabFreeQueue.TryGetValue(refer.cacheHash, out prefabQueueMe))
                    {
                        prefabQueueMe.Enqueue(refer);//入列
                        return(true);
                    }
                }
            }
            return(false);
        }
    public void AddMonos(ReferGameObjects refer, int i, UnityEngine.Object obj)
    {
        List<UnityEngine.Object> monos = null;

        if (refer.monos != null)
            monos = new List<UnityEngine.Object>(refer.monos);
        else
            monos = new List<UnityEngine.Object>();

        //monos.Add(obj);
        if (i < 0)
        {
            monos.Add(obj);
        }
        else
        {
            while (monos.Count <= i)
                monos.Add(null);
            monos[i] = obj;
        }
        refer.monos = monos.ToArray();
    }
 public void SetReferName(ReferGameObjects refer, int i)
 {
     while (refer.names.Count <= i)
         refer.names.Add(null);
     refer.names[i] = GUILayout.TextField(refer.names[i], GUILayout.Width(60));
 }
 public UnityEngine.Object GetMonos(ReferGameObjects refer, int index)
 {
     if (index >= 0 && index < refer.monos.Length)
         return refer.monos[index];
     return null;
 }
 public void RemoveAtMonos(ReferGameObjects refer,int index)
 {
     List<UnityEngine.Object> monos = new List<UnityEngine.Object>(refer.monos);
     monos.RemoveAt(index);
     refer.monos = monos.ToArray();
 }
Example #13
0
 // Use this for initialization
 void Start()
 {
     if (refer == null) refer = GetComponent<ReferGameObjects>();
     isStart = true;
 }
Example #14
0
 /// <summary>
 /// 
 /// </summary>
 /// <param name="parent"></param>
 /// <param name="eachFn"></param>
 public static void ForeachChild(ReferGameObjects parent, LuaFunction eachFn)
 {
     List<GameObject> lists = parent.refers;
     int count = lists.Count;
     GameObject child = null;
     for (int i = 0; i < count; i++)
     {
         child = lists[i];
         eachFn.call(i, child);
     }
 }
Example #15
0
 /// <summary>
 /// 放入缓存
 /// </summary>
 /// <param name="cop"></param>
 public static bool StoreCache(ReferGameObjects refer)
 {
     HashSet<ReferGameObjects> prefabCachesMe = null;
     if (prefabRefCaches.TryGetValue(refer.cacheHash, out prefabCachesMe))
     {//从引用列表寻找
         bool isremove = prefabCachesMe.Remove(refer);
         if (isremove)
         {
             Queue<ReferGameObjects> prefabQueueMe = null;
             if (prefabFreeQueue.TryGetValue(refer.cacheHash, out prefabQueueMe))
             {
                 prefabQueueMe.Enqueue(refer);//入列
                 return true;
             }
         }
     }
     return false;
 }
Example #16
0
        /// <summary>
        /// 获取可用的实例
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        internal static ReferGameObjects GetCache(int key)
        {
            ReferGameObjects refer = null;
            //空闲队列
            Queue <ReferGameObjects> prefabQueueMe = null;

            if (!prefabFreeQueue.TryGetValue(key, out prefabQueueMe))
            {
                var keyString = GetStringKey(key);
                Debug.LogErrorFormat("prefabFreeQueue is null key = {0},keystr = {1},frame={2}", key, keyString, Time.frameCount);
                return(null);
            }

            //引用列表
            HashSet <ReferGameObjects> prefabCachesMe = null;

            if (!prefabRefCaches.TryGetValue(key, out prefabCachesMe))
            {
                var keyString = GetStringKey(key);
                Debug.LogErrorFormat("prefabRefCaches is null key = {0},keystr={1},frame={2}", key, keyString, Time.frameCount);
                return(null);
            }

            //移除回收引用
            DeleteRemveMark(key);

            if (prefabQueueMe.Count > 0)
            {
                refer           = prefabQueueMe.Dequeue();//出列
                refer.cacheHash = key;
                if (!refer)
                {
                    var keyString = GetStringKey(key);
                    Debug.LogErrorFormat("prefabQueueMe.Dequeue() is null key = {0},keystr={1},freeQueue.Count={2},reference.count={3},frame={4}", key, keyString, prefabQueueMe.Count, prefabCachesMe.Count, Time.frameCount);
                }

                //保持引用
                prefabCachesMe.Add(refer);
                return(refer);
            }
            else
            {
                GameObject prefab = Get(key);
                if (!prefab) //如果没有 返回NUll
                {
                    var keyString = GetStringKey(key);
                    Debug.LogErrorFormat("FATAL EXCEPTION : original gameobject is null key = {0},keystr={1},frame={2}", key, keyString, Time.frameCount);
                    return(null);
                }

                GameObject clone = (GameObject)GameObject.Instantiate(prefab);
                var        comp  = clone.GetComponent <ReferGameObjects>();
                if (comp == null)
                {
                    comp = clone.AddComponent <ReferGameObjects>();
                }
                refer           = comp;
                refer.cacheHash = key;
                //保持引用
                prefabCachesMe.Add(refer); //放入引用列表
            }

            if (!refer)
            {
                var keyString = GetStringKey(key);
                Debug.LogErrorFormat("FATAL EXCEPTION :reference is null, key is {0},keystring={1},reference.count is {2},frame={3}", key, keyString, prefabCachesMe.Count, Time.frameCount);
            }

            return(refer);
        }