コード例 #1
0
 /// <summary>
 /// 异步资源加载,外部直接调用,仅加载不需要实例化的资源,例如Texture,音频等等
 /// </summary>
 /// <param name="path"></param>
 /// <returns></returns>
 public void LoadAsset(string path, OnAssetLoadComplete complete)
 {
     if (m_EditorResource)
     {
         LoadAssetFromEditor(path, complete);
     }
     else
     {
         //LoadAssetFromAssetBundle(path, complete);
         AssetLoadAsyncOp op = ReferencePool.Acquire <AssetLoadAsyncOp>();
         op.Path           = path;
         op.CompleteAction = complete;
         waitingList.Add(op);
     }
 }
コード例 #2
0
        /// <summary>
        /// 缓存太多,清除最早没有使用的资源
        /// </summary>
        //protected void WashOut()
        //{
        //    //当大于缓存个数时,进行一半释放
        //    Debug.Log("washout " + m_NoRefrenceAssetMapList.Size() + "  " + m_MaxCacheCount);
        //    while (m_NoRefrenceAssetMapList.Size() >= m_MaxCacheCount)
        //    {
        //        for (int i = 0; i < m_MaxCacheCount / 2; i++)
        //        {
        //            AssetItem item = m_NoRefrenceAssetMapList.Back();
        //            DestoryAssetItem(item, true);
        //        }
        //    }
        //}

        public void OnUpdate(float elapseSeconds, float realElapseSeconds)
        {
            m_AssetBundleManager.OnUpdate(elapseSeconds, realElapseSeconds);

            if (loadingList.Count > 0)
            {
                AssetLoadAsyncOp op = loadingList[0];
                if (op.isLoading == true)
                {
                    if (op.Oper.isDone)
                    {
                        AssetItem item = ReferencePool.Acquire <AssetItem>();
                        item.Asset = op.Oper.asset;
                        CacheAsset(op.Path, item);
                        if (op.CompleteAction != null)
                        {
                            op.CompleteAction(op.Path, op.Oper.asset);
                        }

                        loadingList.RemoveAt(0);
                        ReferencePool.Release(op);
                    }
                }
                else
                {
                    op.isLoading = true;
                    //Debug.Log("load asset " + op.AssetName + "  " + op.AssetBundle);
                    op.Oper = op.AssetBundle.LoadAssetAsync(op.AssetName);
                }
            }
            else
            {
                if (waitingList.Count > 0)
                {
                    AssetLoadAsyncOp op = waitingList[0];
                    LoadAssetFromAssetBundle(op.Path, op.CompleteAction);
                    waitingList.RemoveAt(0);
                }
            }
        }
コード例 #3
0
        /// <summary>
        /// 从AssetBundle读取资源
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        private void LoadAssetFromAssetBundle(string path, OnAssetLoadComplete complete)
        {
            if (string.IsNullOrEmpty(path))
            {
                if (complete != null)
                {
                    complete(path, null);
                }
                return;
            }

            uint      crc  = Crc32.GetCrc32(path);
            AssetItem item = GetCacheAssetItem(crc);

            if (item != null && item.Asset != null)
            {
                if (complete != null)
                {
                    complete(path, item.Asset);
                }
                return;
            }

            ConfigItem  configItem = m_AssetBundleManager.LoadConfigItem(crc);
            AssetBundle bundle     = m_AssetBundleManager.LoadAssetBundle(crc);

            if (bundle != null)
            {
                //bundle.LoadAssetAsync(configItem.AssetName);
                AssetLoadAsyncOp op = ReferencePool.Acquire <AssetLoadAsyncOp>();
                op.AssetBundle    = bundle;
                op.CompleteAction = complete;
                op.isLoading      = false;
                op.Oper           = null;
                op.Path           = path;
                op.AssetName      = configItem.AssetName;
                loadingList.Add(op);
            }
        }