Ejemplo n.º 1
0
        private IEnumerator _Init(string path)
        {
            Object getAsset = null;

            {
                // 添加扩展名
                string abPath = path;
                if (!abPath.EndsWith(GEngineDef.AssetBundleExt))
                {
                    abPath = abPath + GEngineDef.AssetBundleExt;
                }

                //if(abPath.StartsWith("avatar"))
                {
                    //abPath = "avatar" + GEngineDef.AssetBundleExt;
                }

                if (abPath.IndexOf("kuijiabing_1_1") > 0)
                {
                    abPath = "avatar/soldier/kuijiabing_1_1" + GEngineDef.AssetBundleExt;
                }
                else if (abPath.IndexOf("kuijiabing_1_2") > 0)
                {
                    abPath = "avatar/soldier/kuijiabing_1_2" + GEngineDef.AssetBundleExt;
                }

                _bundleLoader = AssetBundleLoader.Load(abPath);

                while (!_bundleLoader.IsCompleted)
                {
                    yield return(null);
                }

                if (!_bundleLoader.IsSuccess)
                {
                    Debug.LogErrorFormat("[AssetLoader]Load BundleLoader Failed(Error) when Finished: {0}", path);
                    //_bundleLoader.Release();
                    OnFinish(null);
                    yield break;
                }

                var assetBundle = _bundleLoader.Bundle;

                DateTime beginTime = DateTime.Now;

                //var abAssetName = Path.GetFileNameWithoutExtension(Url).ToLower();
                var abAssetName = AssetManager.GetAssetRelativePath(path);

                //Debug.Log("Try to load asset name: " + abAssetName);

                foreach (string name in assetBundle.GetAllAssetNames())
                {
                    //Debug.Log("------------------------------------>: " + name);
                }

                if (!assetBundle.isStreamedSceneAssetBundle)
                {
                    AssetBundleRequest request = null;
                    if (abAssetName.EndsWith(".png"))
                    {
                        request = assetBundle.LoadAssetAsync(abAssetName, typeof(Sprite));
                    }
                    else
                    {
                        request = assetBundle.LoadAssetAsync(abAssetName);
                    }

                    //var request = assetBundle.LoadAssetAsync(abAssetName);
                    while (!request.isDone)
                    {
                        yield return(null);
                    }
                    Debuger.Assert(getAsset = request.asset);
                    //_bundleLoader.PushLoadedAsset(getAsset); // TODO: finish me
                }
                else
                {
                    Debug.LogError("URL: " + Url + " --> isStreamedSceneAssetBundle");
                    // if it's a scene in asset bundle, did nothing
                    // but set a fault Object the result
                    //getAsset = KResourceModule.Instance;
                }


                ResourceModule.LogLoadTime("AssetLoader: load asset time: ", path, beginTime);

                if (getAsset == null)
                {
                    Debug.LogErrorFormat("Asset is NULL: {0}", path);
                }
            }


            if (Application.isEditor && getAsset != null && getAsset is GameObject)
            {
                RefreshMaterialsShadersForEditorEnv(getAsset as GameObject);
                //if (getAsset != null)
                //    KResoourceLoadedAssetDebugger.Create(getAsset.GetType().Name, Url, getAsset as Object);
            }


            if (getAsset != null)
            {
                // 更名~ 注明来源asset bundle 带有类型
                //getAsset.name = String.Format("{0}~{1}", getAsset, Url);
            }
            OnFinish(getAsset);
        }
Ejemplo n.º 2
0
        private IEnumerator LoadAssetBundle(string relativeUrl)
        {
            while (_assetBundleManifest == null)
            {
                if (_assetBundleManifestLoadFailure)
                {
                    yield break;
                }
                yield return(null);
            }

            if (_assetBundleManifestLoadFailure)
            {
                yield break;
            }

            var abPath = relativeUrl.ToLower();
            var deps   = _assetBundleManifest.GetAllDependencies(abPath);

            AssetBundleLoader[] _depLoaders = new AssetBundleLoader[deps.Length];
            for (var d = 0; d < deps.Length; d++)
            {
                var dep = deps[d];
                _depLoaders[d] = AssetBundleLoader.Load(dep, null);
            }
            for (var l = 0; l < _depLoaders.Length; l++)
            {
                var loader = _depLoaders[l];
                while (!loader.IsCompleted)
                {
                    yield return(null);
                }

                // after bundle load finished, load asset
            }

            relativeUrl = relativeUrl.ToLower();

            var bytesLoader = BytesLoader.Load(AssetManager.GetAssetBundleAbsoluteURL(relativeUrl));

            while (!bytesLoader.IsCompleted)
            {
                yield return(null);
            }
            if (!bytesLoader.IsSuccess)
            {
                Debug.LogErrorFormat("[AssetBundleLoader]Error Load AssetBundle: {0}", relativeUrl);
                OnFinish(null);
                yield break;
            }

            byte[] bundleBytes = bytesLoader.Bytes;
            //Progress = 1 / 2f;
            //bytesLoader.Release(); // 字节用完就释放

            AssetBundleParser bundleParser = new AssetBundleParser(RelativeResourceUrl, bundleBytes);

            while (!bundleParser.IsFinished)
            {
                yield return(null);
            }

            //Progress = 1f;
            var assetBundle = bundleParser.Bundle;

            if (assetBundle == null)
            {
                Debug.LogErrorFormat("WWW.assetBundle is NULL: {0}", RelativeResourceUrl);
            }
            else
            {
                //yield return LoadAllAssetsAsync(assetBundle);
            }

            OnFinish(assetBundle);

            //Debug.Log("Loaded ab: " + assetBundle.name + ", assets --> " +assetBundle.GetAllAssetNames().Length);
            foreach (string str in assetBundle.GetAllAssetNames())
            {
                // Debug.Log("Loaded ab: " + assetBundle.name + ", asset --> " + str);
            }
            //Array.Clear(cloneBytes, 0, cloneBytes.Length);  // 手工释放内存

            //GC.Collect(0);// 手工释放内存
        }