Inheritance: AbstractResourceLoader
Ejemplo n.º 1
0
        private IEnumerator _Init(string path, LoaderMode loaderMode)
        {
            IsLoadAssetBundle = AppEngine.GetConfig("KEngine", "IsLoadAssetBundle").ToInt32() != 0;

            Object getAsset = null;

            if (IsEditorLoadAsset)
            {
#if UNITY_EDITOR
                if (path.EndsWith(".unity"))
                {
                    // scene
                    getAsset = KResourceModule.Instance;
                    Log.LogWarning("Load scene from Build Settings: {0}", path);
                }
                else
                {
                    getAsset = UnityEditor.AssetDatabase.LoadAssetAtPath("Assets/" + KEngineDef.ResourcesBuildDir + "/" + path, typeof(UnityEngine.Object));
                    if (getAsset == null)
                    {
                        Log.Error("Asset is NULL(from {0} Folder): {1}", KEngineDef.ResourcesBuildDir, path);
                    }
                }
#else
                Log.Error("`IsEditorLoadAsset` is Unity Editor only");
#endif
                OnFinish(getAsset);
            }
            else if (!IsLoadAssetBundle)
            {
                string extension = Path.GetExtension(path);
                path = path.Substring(0, path.Length - extension.Length); // remove extensions

                getAsset = Resources.Load <Object>(path);
                if (getAsset == null)
                {
                    Log.Error("Asset is NULL(from Resources Folder): {0}", path);
                }
                OnFinish(getAsset);
            }
            else
            {
                _bundleLoader = AssetBundleLoader.Load(path, null, loaderMode);

                while (!_bundleLoader.IsCompleted)
                {
                    if (IsReadyDisposed) // 中途释放
                    {
                        _bundleLoader.Release();
                        OnFinish(null);
                        yield break;
                    }
                    yield return(null);
                }

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

                var assetBundle = _bundleLoader.Bundle;

                DateTime beginTime = DateTime.Now;
#if UNITY_5 || UNITY_2017_1_OR_NEWER
                // Unity 5 下,不能用mainAsset, 要取对象名
                var abAssetName = Path.GetFileNameWithoutExtension(Url).ToLower();
                if (!assetBundle.isStreamedSceneAssetBundle)
                {
                    if (loaderMode == LoaderMode.Sync)
                    {
                        getAsset = assetBundle.LoadAsset(abAssetName);
                        Debuger.Assert(getAsset);
                        _bundleLoader.PushLoadedAsset(getAsset);
                    }
                    else
                    {
                        var request = assetBundle.LoadAssetAsync(abAssetName);
                        while (!request.isDone)
                        {
                            yield return(null);
                        }
                        Debuger.Assert(getAsset = request.asset);
                        _bundleLoader.PushLoadedAsset(getAsset);
                    }
                }
                else
                {
                    // if it's a scene in asset bundle, did nothing
                    // but set a fault Object the result
                    getAsset = KResourceModule.Instance;
                }
#else
                // 经过AddWatch调试,.mainAsset这个getter第一次执行时特别久,要做序列化
                //AssetBundleRequest request = assetBundle.LoadAsync("", typeof(Object));// mainAsset
                //while (!request.isDone)
                //{
                //    yield return null;
                //}
                try
                {
                    Debuger.Assert(getAsset = assetBundle.mainAsset);
                }
                catch
                {
                    Log.Error("[OnAssetBundleLoaded:mainAsset]{0}", path);
                }
#endif

                KResourceModule.LogLoadTime("AssetFileBridge", path, beginTime);

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

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

                // 编辑器环境下,如果遇到GameObject,对Shader进行Fix
                if (getAsset is GameObject)
                {
                    var go = getAsset as GameObject;
                    foreach (var r in go.GetComponentsInChildren <Renderer>(true))
                    {
                        RefreshMaterialsShaders(r);
                    }
                }
            }

            if (getAsset != null)
            {
                // 更名~ 注明来源asset bundle 带有类型
                getAsset.name = String.Format("{0}~{1}", getAsset, Url);
            }
            OnFinish(getAsset);
        }
Ejemplo n.º 2
0
        private IEnumerator LoadAssetBundle(string relativeUrl)
        {
#if UNITY_5 || UNITY_2017_1_OR_NEWER
            // Unity 5 Manifest中管理了依赖
            var abPath = relativeUrl.ToLower();
            var deps   = _assetBundleManifest.GetAllDependencies(abPath);
            _depLoaders = new AssetBundleLoader[deps.Length];
            for (var d = 0; d < deps.Length; d++)
            {
                var dep = deps[d];
                _depLoaders[d] = AssetBundleLoader.Load(dep, null, _loaderMode);
            }
            for (var l = 0; l < _depLoaders.Length; l++)
            {
                var loader = _depLoaders[l];
                while (!loader.IsCompleted)
                {
                    yield return(null);
                }
            }
#endif

#if UNITY_5 || UNITY_2017_1_OR_NEWER
            // Unity 5 AssetBundle自动转小写
            relativeUrl = relativeUrl.ToLower();
#endif
            var bytesLoader = HotBytesLoader.Load(KResourceModule.BundlesPathRelative + relativeUrl, _loaderMode);
            while (!bytesLoader.IsCompleted)
            {
                yield return(null);
            }
            if (!bytesLoader.IsSuccess)
            {
                if (AssetBundlerLoaderErrorEvent != null)
                {
                    AssetBundlerLoaderErrorEvent(this);
                }
                Log.Error("[AssetBundleLoader]Error Load Bytes AssetBundle: {0}", relativeUrl);
                OnFinish(null);
                yield break;
            }

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

            BundleParser = new KAssetBundleParser(RelativeResourceUrl, bundleBytes);
            while (!BundleParser.IsFinished)
            {
                if (IsReadyDisposed) // 中途释放
                {
                    OnFinish(null);
                    yield break;
                }
                Progress = BundleParser.Progress / 2f + 1 / 2f; // 最多50%, 要算上WWWLoader的嘛
                yield return(null);
            }

            Progress = 1f;
            var assetBundle = BundleParser.Bundle;
            if (assetBundle == null)
            {
                Log.Error("WWW.assetBundle is NULL: {0}", RelativeResourceUrl);
            }

            OnFinish(assetBundle);

            //Array.Clear(cloneBytes, 0, cloneBytes.Length);  // 手工释放内存

            //GC.Collect(0);// 手工释放内存
        }
Ejemplo n.º 3
0
        private IEnumerator LoadAssetBundle(string relativeUrl)
        {
            #if UNITY_5
            // Unity 5下,自动进行依赖加载
            var abPath = relativeUrl.ToLower();
            var deps = _assetBundleManifest.GetAllDependencies(abPath);
            _depLoaders = new AssetBundleLoader[deps.Length];
            for (var d = 0; d < deps.Length; d++)
            {
                var dep = deps[d];
                _depLoaders[d] = AssetBundleLoader.Load(dep, null, _loaderMode);
            }
            for (var l = 0; l < _depLoaders.Length; l++)
            {
                var loader = _depLoaders[l];
                while (!loader.IsCompleted)
                {
                    yield return null;
                }
            }
            #endif

            #if UNITY_5
            // Unity 5 AssetBundle自动转小写
            relativeUrl = relativeUrl.ToLower();
            #endif
            var bytesLoader = HotBytesLoader.Load(relativeUrl, _loaderMode);
            while (!bytesLoader.IsCompleted)
            {
                yield return null;
            }
            if (!bytesLoader.IsSuccess)
            {
                if (AssetBundlerLoaderErrorEvent != null)
                {
                    AssetBundlerLoaderErrorEvent(this);
                }
                Log.Error("[AssetBundleLoader]Error Load Bytes AssetBundle: {0}", relativeUrl);
                OnFinish(null);
                yield break;
            }

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

            BundleParser = new KAssetBundleParser(RelativeResourceUrl, bundleBytes);
            while (!BundleParser.IsFinished)
            {
                if (IsReadyDisposed) // 中途释放
                {
                    OnFinish(null);
                    yield break;
                }
                Progress = BundleParser.Progress / 2f + 1 / 2f; // 最多50%, 要算上WWWLoader的嘛
                yield return null;
            }

            Progress = 1f;
            var assetBundle = BundleParser.Bundle;
            if (assetBundle == null)
                Log.Error("WWW.assetBundle is NULL: {0}", RelativeResourceUrl);

            OnFinish(assetBundle);

            //Array.Clear(cloneBytes, 0, cloneBytes.Length);  // 手工释放内存

            //GC.Collect(0);// 手工释放内存
        }
Ejemplo n.º 4
0
        private IEnumerator _Init(string path, LoaderMode loaderMode)
        {
            IsLoadAssetBundle = AppEngine.GetConfig("KEngine", "IsLoadAssetBundle").ToInt32() != 0;

            Object getAsset = null;
            if (!IsLoadAssetBundle)
            {
                string extension = Path.GetExtension(path);
                path = path.Substring(0, path.Length - extension.Length); // remove extensions

                getAsset = Resources.Load<Object>(path);
                if (getAsset == null)
                {
                    Log.Error("Asset is NULL(from Resources Folder): {0}", path);
                }
                OnFinish(getAsset);
            }
            else
            {
                _bundleLoader = AssetBundleLoader.Load(path, null, loaderMode);

                while (!_bundleLoader.IsCompleted)
                {
                    if (IsReadyDisposed) // 中途释放
                    {
                        _bundleLoader.Release();
                        OnFinish(null);
                        yield break;
                    }
                    yield return null;
                }

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

                var assetBundle = _bundleLoader.Bundle;

                DateTime beginTime = DateTime.Now;
            #if UNITY_5
                // Unity 5 下,不能用mainAsset, 要取对象名
                var abAssetName = Path.GetFileNameWithoutExtension(Url).ToLower();
                if (!assetBundle.isStreamedSceneAssetBundle)
                {
                    if (loaderMode == LoaderMode.Sync)
                    {
                        getAsset = assetBundle.LoadAsset(abAssetName);
                        Debuger.Assert(getAsset);
                    }
                    else
                    {
                        var request = assetBundle.LoadAssetAsync(abAssetName);
                        while (!request.isDone)
                        {
                            yield return null;
                        }
                        Debuger.Assert(getAsset = request.asset);
                    }
                }
                else
                {
                    // if it's a scene in asset bundle, did nothing
                    // but set a fault Object the result
                    getAsset = KResourceModule.Instance;
                }
            #else
                // 经过AddWatch调试,.mainAsset这个getter第一次执行时特别久,要做序列化
                //AssetBundleRequest request = assetBundle.LoadAsync("", typeof(Object));// mainAsset
                //while (!request.isDone)
                //{
                //    yield return null;
                //}
                try
                {
                    Debuger.Assert(getAsset = assetBundle.mainAsset);
                }
                catch
                {
                    Log.Error("[OnAssetBundleLoaded:mainAsset]{0}", path);
                }
            #endif

                KResourceModule.LogLoadTime("AssetFileBridge", path, beginTime);

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

            }

            if (Application.isEditor)
            {
                if (getAsset != null)
                    KResoourceLoadedAssetDebugger.Create(getAsset.GetType().Name, Url, getAsset as Object);

                // 编辑器环境下,如果遇到GameObject,对Shader进行Fix
                if (getAsset is GameObject)
                {
                    var go = getAsset as GameObject;
                    foreach (var r in go.GetComponentsInChildren<Renderer>(true))
                    {
                        RefreshMaterialsShaders(r);
                    }
                }
            }

            if (getAsset != null)
            {
                // 更名~ 注明来源asset bundle 带有类型
                getAsset.name = String.Format("{0}~{1}", getAsset, Url);
            }
            OnFinish(getAsset);
        }
Ejemplo n.º 5
0
        private IEnumerator LoadAssetBundle(string relativeUrl)
        {
#if UNITY_5 || UNITY_2017_1_OR_NEWER
            // Unity 5 Manifest中管理了依赖
            var abPath = relativeUrl.ToLower();
            var deps   = _assetBundleManifest.GetAllDependencies(abPath);
            _depLoaders = new AssetBundleLoader[deps.Length];
            for (var d = 0; d < deps.Length; d++)
            {
                var dep = deps[d];
                _depLoaders[d] = AssetBundleLoader.Load(dep, null, _loaderMode);
                if (_depLoaders[d].dependFrom == string.Empty)
                {
                    _depLoaders[d].dependFrom = relativeUrl;
                }
            }
            for (var l = 0; l < _depLoaders.Length; l++)
            {
                var loader = _depLoaders[l];
                while (!loader.IsCompleted)
                {
                    yield return(null);
                }
            }
#endif

#if UNITY_5 || UNITY_2017_1_OR_NEWER
            // Unity 5 AssetBundle自动转小写
            relativeUrl = relativeUrl.ToLower();
#endif
            if (AppConfig.IsLogAbLoadCost)
            {
                beginTime = Time.realtimeSinceStartup;
            }

            string _fullUrl = KResourceModule.GetAbFullPath(relativeUrl);

            if (string.IsNullOrEmpty(_fullUrl))
            {
                OnFinish(null);
                yield break;
            }

            AssetBundle assetBundle = null;
            if (_loaderMode == LoaderMode.Sync)
            {
                assetBundle = AssetBundle.LoadFromFile(_fullUrl);
            }
            else
            {
                var request = AssetBundle.LoadFromFileAsync(_fullUrl);
                while (!request.isDone)
                {
                    if (IsReadyDisposed) // 中途释放
                    {
                        OnFinish(null);
                        yield break;
                    }
                    Progress = request.progress;
                    yield return(null);
                }
                assetBundle = request.assetBundle;
            }
            if (assetBundle == null)
            {
                Log.Error("assetBundle is NULL: {0}", RelativeResourceUrl);
            }
            if (AppConfig.IsLogAbLoadCost)
            {
                Log.Info("[Finish] Load AssetBundle {0}, CostTime {1}s {2}", relativeUrl, Time.realtimeSinceStartup - beginTime, dependFrom);
            }
            if (AppConfig.IsSaveCostToFile && !relativeUrl.StartsWith("ui/"))
            {
                LogFileManager.WriteLoadAbLog(relativeUrl, Time.realtimeSinceStartup - beginTime);
            }
            OnFinish(assetBundle);
        }