コード例 #1
0
        public virtual T GetPooledInstance <T>() where T : Component
        {
            if (!IsResourceLoaded)
            {
                throw new NullReferenceException("Please load the resources on this PoolTool" +
                                                 " before attempting to get an instance.");
            }

            if (Pool.PoolCount == 0)
            {
                Pool.AddToPool();
            }
            var pooledInstance = Pool.GetObject();

            pooledInstance.transform.SetParent(null);
            pooledInstance.gameObject.SetActive(true);

            try { return((T)pooledInstance); }

            catch (InvalidCastException)
            {
                Debug.LogErrorFormat(this, $"Invalid type {typeof(T).Name} requested.");
                return(null);
            }
        }
コード例 #2
0
    /// <summary>
    /// 先从总池子里获取 获取不到就开始执行加载 因为加载问题无法立即返回 所以只能用ACTION
    /// </summary>
    /// <param name="path"></param>
    /// <param name="assetName"></param>
    /// <param name="callback"></param>
    public void Get(string path, string assetName, Action <Object> callback)
    {
        //获取池子 如果存在那就拿出来
        AssetID id = Util.GetAssetID(path);
        Object  target;
        IPool   pool = GetPool(id);

        if (pool == null)
        {
            //创建并进行加载
            Debug.LogError("池子不存在");
            CreatePool(id, out pool);
            //因为创建不一定能直接拿到资源
            //pool = new ResourcePool(id);

            //target = pool.GetObject ();

            // callback(target);
            AddLoadQueue(pool, callback);  //加载完调用
        }
        else
        {
            //直接回调 这里也可以改成加到加载队列
            //两种方式的区别在于 refcount的变化
            if (callback != null)
            {
                Debug.LogError("存在池子");
                //考虑在池子拿出去的都放在一个空obj下
                target = pool.GetObject();
                Debug.LogError("获取到对象");
                AddActivePoolInfo(target, id);
                callback(target);
                //AddLoadQueue (pool, callback); //加载完调用
            }
        }
        //Instance.StartCheckGC ();
    }