Exemplo n.º 1
0
        public override void Init(string url, params object[] args)
        {
#if UNITY_EDITOR
            if (KResourceModule.IsEditorLoadAsset)
            {
                base.Init(url);
                LoadInEditor(url);
                return;
            }
#endif
#if UNITY_5 || UNITY_2017_1_OR_NEWER
            PreLoadManifest();
#endif
            base.Init(url);
            _loaderMode = (LoaderMode)args[0];

            if (NewAssetBundleLoaderEvent != null)
            {
                NewAssetBundleLoaderEvent(url);
            }

            RelativeResourceUrl = url;
            if (AppConfig.IsLogAbLoadCost)
            {
                Log.Info("[Start] Load AssetBundle, {0}", RelativeResourceUrl);
            }
            KResourceModule.Instance.StartCoroutine(LoadAssetBundle(url));
        }
Exemplo n.º 2
0
        protected IEnumerator InitCo(string url, LoaderMode mode)
        {
            //获取资源的真实路径
            string           actualUrl;
            ResourcePathType type;

            ResourceLoaderSingleton.Instance.GetResoureActualPath(url, out actualUrl, out type);
            //如果资源路径无效,且允许运行时下载,尝试从服务端下载
            if (type == ResourcePathType.Invalid && ResourceLoaderSingleton.Instance.HotFixType == ResourceHotFixType.Runtime)
            {
                if (mode == LoaderMode.Sync)
                {
                    ResourceLoaderSingleton.Instance.DownloadFile(url);
                }
                else
                {
                    yield return(ResourceLoaderSingleton.Instance.DownloadFileAsync(url));
                }
            }
            if (mode == LoaderMode.Sync)
            {
                Bytes = ResourceLoaderSingleton.Instance.ReadAllBytes(actualUrl);
            }
            else
            {
                //TODO 异步加载
                yield return(null);
            }
            OnFinish(null);
        }
Exemplo n.º 3
0
        protected override void Init(string url, params object[] args)
        {
            base.Init(url, args);

            _loaderMode = (LoaderMode)args[0];
            KResourceModule.Instance.StartCoroutine(CoLoad(url));
        }
Exemplo n.º 4
0
        protected override void Init(string url, params object[] args)
        {
            base.Init(url, args);

            _loaderMode = (LoaderMode)args[0];
            KResourceModule.Instance.StartCoroutine(CoLoad(url));
        }
Exemplo n.º 5
0
        protected override void Init(string url, params object[] args)
        {
            base.Init(url, args);
            LoaderMode mode = (LoaderMode)args[0];

            //TODO
            ResourceLoaderSingleton.Instance.StartCoroutine(InitCo(url, mode));
        }
Exemplo n.º 6
0
        protected override void Init(string url, params object[] args)
        {
            base.Init(url, args);

            _mode      = (LoaderMode)args[0];
            _url       = url;
            _sceneName = Path.GetFileNameWithoutExtension(_url);
            KResourceModule.Instance.StartCoroutine(Start());
        }
Exemplo n.º 7
0
        protected override void Init(string url, params object[] args)
        {
            base.Init(url, args);

            _mode = (LoaderMode)args[0];
            _url = url;
            _sceneName = Path.GetFileNameWithoutExtension(_url);
            KResourceModule.Instance.StartCoroutine(Start());
        }
Exemplo n.º 8
0
        internal static void LogLoadTime(string resType, LoaderMode loadMode, string resPath, System.DateTime begin)
        {
            if (LogLevel < LoadingLogLevel.ShowTime)
            {
                return;
            }

            Debug.LogFormat("[Load] {0}, {1}, {2}, {3}s", resType, loadMode, resPath, (System.DateTime.Now - begin).TotalSeconds);
        }
Exemplo n.º 9
0
 public static SceneLoader Load(string url, System.Action<bool> callback = null,
     LoaderMode mode = LoaderMode.Async)
 {
     LoaderDelgate newCallback = null;
     if (callback != null)
     {
         newCallback = (isOk, obj) => callback(isOk);
     }
     return AutoNew<SceneLoader>(url, newCallback, false, mode);
 }
Exemplo n.º 10
0
        protected override void Init(string url, params object[] args)
        {
            ResManager.PreLoadManifest();
            base.Init(url);

            _loaderMode = (LoaderMode)args[0];

            ResManager.LogRequest("AssetBundle", url);
            ResManager.Instance.StartCoroutine(_LoadCoroutine(url));
        }
Exemplo n.º 11
0
        public static StaticAssetLoader Load(string url, AssetFileLoader.AssetFileBridgeDelegate callback = null, LoaderMode loaderMode = LoaderMode.Async)
        {
            LoaderDelgate newCallback = null;
            if (callback != null)
            {
                newCallback = (isOk, obj) => callback(isOk, obj as UnityEngine.Object);
            }

            return AutoNew<StaticAssetLoader>(url, newCallback, false, loaderMode);
        }
Exemplo n.º 12
0
        public static SceneLoader Load(string url, System.Action <bool> callback = null,
                                       LoaderMode mode = LoaderMode.Async)
        {
            LoaderDelgate newCallback = null;

            if (callback != null)
            {
                newCallback = (isOk, obj) => callback(isOk);
            }
            return(AutoNew <SceneLoader>(url, newCallback, false, mode));
        }
Exemplo n.º 13
0
        protected override void Init(string bundlePath, params object[] args)
        {
            base.Init(bundlePath, args);
            LoaderMode mode           = (LoaderMode)args[0];
            string     fullBundlePath = string.Format(@"{0}/{1}/{2}",
                                                      ResourceLoaderSingleton.Instance.PlatformDir,
                                                      ResourceLoaderSingleton.Instance.Version,
                                                      bundlePath);

            ResourceLoaderSingleton.Instance.StartCoroutine(InitCo(fullBundlePath, mode));
        }
Exemplo n.º 14
0
        protected override void Init(string url, params object[] args)
        {
            base.Init(url, args);

            _loaderMode = (LoaderMode)args[0];
            var resMgr = AppFacade.Instance.GetManager <KResourceManager>();

            if (resMgr != null)
            {
                resMgr.StartCoroutine(CoLoad(url));
            }
        }
Exemplo n.º 15
0
        public static AssetFileLoader Load(string path, AssetFileBridgeDelegate assetFileLoadedCallback = null, LoaderMode loaderMode = LoaderMode.Async)
        {
            // 添加扩展名
            path = path + AppEngine.GetConfig(KEngineDefaultConfigs.AssetBundleExt);

            LoaderDelgate realcallback = null;
            if (assetFileLoadedCallback != null)
            {
                realcallback = (isOk, obj) => assetFileLoadedCallback(isOk, obj as Object);
            }

            return AutoNew<AssetFileLoader>(path, realcallback, false, loaderMode);
        }
Exemplo n.º 16
0
        /// <summary>
        /// AssetBundle读取原字节目录
        /// </summary>
        //private KResourceInAppPathType _inAppPathType;
        public static AssetBundleLoader Load(string url, CAssetBundleLoaderDelegate callback = null,
                                             LoaderMode loaderMode = LoaderMode.Async)
        {
            LoaderDelgate newCallback = null;

            if (callback != null)
            {
                newCallback = (isOk, obj) => callback(isOk, obj as AssetBundle);
            }
            var newLoader = AutoNew <AssetBundleLoader>(url, newCallback, false, loaderMode);

            return(newLoader);
        }
Exemplo n.º 17
0
        protected override void Init(string url, params object[] args)
        {
            base.Init(url, args);
            LoaderMode mode = (LoaderMode)args[0];
            //transform url to bundlename
            string bundlePath = ResourceLoaderSingleton.Instance.GetBundlePathByPath(url);

            if (bundlePath == null)
            {
                OnError();
                return;
            }
            ResourceLoaderSingleton.Instance.StartCoroutine(LoadAssetBundle(bundlePath, mode));
        }
Exemplo n.º 18
0
        public static SceneLoader Load(string url, System.Action <bool> callback = null,
                                       LoaderMode mode = LoaderMode.Async)
        {
            UnloadPreScene();
            LoaderDelgate newCallback = null;

            if (callback != null)
            {
                newCallback = (isOk, obj) => callback(isOk);
            }
            var loader = AutoNew <SceneLoader>(url, newCallback, true, mode);

            preSceneLoader = loader;
            return(preSceneLoader);
        }
Exemplo n.º 19
0
        IEnumerator InitCo(string url, LoaderMode mode)
        {
            Object resObj = null;

            if (!ResourceLoaderSingleton.Instance.RunBundle && Application.isEditor)
            {
#if UNITY_EDITOR
                resObj = AssetDatabase.LoadAssetAtPath(ResourceLoaderSingleton.Instance.EditorAssetsPath + url, typeof(Object));
#endif
                if (resObj == null)
                {
                    Debug.Log(string.Format("url[{0}] cant find in ResourcesAssets", url));
                }
            }
            else
            {
                //if not in editor, read from Rsources or AssetBundle
                if (!ResourceLoaderSingleton.Instance.RunBundle)
                {
                    //remove extension
                    url = url.Substring(0, url.Length - Path.GetExtension(url).Length);
                    //read from resources
                    resObj = Resources.Load(url);
                    if (resObj == null)
                    {
                        Debug.Log(string.Format("url[ {0}] cant find in Resources"));
                    }
                }
                else
                {
                    //load from bundle
                    var loader = AssetBundleLoader.Load(url, null, mode);
                    while (!loader.IsComplete)
                    {
                        yield return(null);
                    }
                    AssetBundle bundle = loader.ResultObj as AssetBundle;
                    resObj = bundle.LoadAsset(url);
                }
            }
            if (resObj == null)
            {
                Debug.Log(string.Format("AssetPath [{0}] is NULL or cannot load", url));
            }


            OnFinish(resObj);
        }
Exemplo n.º 20
0
        /// <summary>
        /// AssetBundle读取原字节目录
        /// </summary>
        //private KResourceInAppPathType _inAppPathType;

        public static AssetBundleLoader Load(string url, CAssetBundleLoaderDelegate callback = null,
                                             LoaderMode loaderMode = LoaderMode.Async)
        {
#if UNITY_5 || UNITY_2017_1_OR_NEWER
            url = url.ToLower();
#endif
            LoaderDelgate newCallback = null;
            if (callback != null)
            {
                newCallback = (isOk, obj) => callback(isOk, obj as AssetBundle);
            }
            var newLoader = AutoNew <AssetBundleLoader>(url, newCallback, false, loaderMode);


            return(newLoader);
        }
Exemplo n.º 21
0
        void _loader_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (e.Cancelled)
            {
                if (_mainFormClosing)
                {
                    this._loader = null;
                    this.Close();
                    return;
                }
                else
                {
                    Utils.WarningMessage("Операция прервана пользователем!");
                }
            }
            else if (e.Error != null)
            {
                MessageBox.Show(e.Error.Message);
            }
            else
            {
                if (_mode == LoaderMode.DownloadProgram || _mode == LoaderMode.DownloadProgramAndConfiguration)
                {
                    _progBuf           = _loader.ProgramBinBuffer;
                    lProgram.Text      = "Не сохранён";
                    lProgram.ForeColor = Color.SteelBlue;
                }
                if (_mode == LoaderMode.DownloadConfiguration || _mode == LoaderMode.DownloadProgramAndConfiguration)
                {
                    _confBuf          = _loader.ConfigurationBinBuffer;
                    lConfig.Text      = "Не сохранён";
                    lConfig.ForeColor = Color.SteelBlue;
                }
            }


            _loader.ProgressChanged    -= new ProgressChangedEventHandler(_loader_ProgressChanged);
            _loader.RunWorkerCompleted -= new RunWorkerCompletedEventHandler(_loader_RunWorkerCompleted);


            _mode = LoaderMode.None;

            GlobProcProgressDefault();
            UpdateControlStatus();
        }
Exemplo n.º 22
0
        protected override void Init(string url, params object[] args)
        {
#if UNITY_5
            PreLoadManifest();
#endif
            base.Init(url);
            _loaderMode = (LoaderMode)args[0];
            if (NewAssetBundleLoaderEvent != null)
            {
                NewAssetBundleLoaderEvent(url);
            }
            RelativeResourceUrl = url;
            KResourceManager.LogRequest("AssetBundle", RelativeResourceUrl);
            var resMgr = AppFacade.Instance.GetManager <KResourceManager>();
            if (resMgr != null)
            {
                resMgr.StartCoroutine(LoadAssetBundle(url));
            }
        }
Exemplo n.º 23
0
        /// <summary>
        /// 加载ab
        /// </summary>
        /// <param name="url">资源路径</param>
        /// <param name="callback">加载完成的回调</param>
        /// <param name="loaderMode">Async异步,sync同步</param>
        /// <returns></returns>
        public static AssetBundleLoader Load(string url, CAssetBundleLoaderDelegate callback = null,
                                             LoaderMode loaderMode = LoaderMode.Async)
        {
            if (!KResourceModule.IsEditorLoadAsset && !url.EndsWith(AppConfig.AssetBundleExt))
            {
                url = url + AppConfig.AssetBundleExt;
            }
#if UNITY_5 || UNITY_2017_1_OR_NEWER
            url = url.ToLower();
#endif
            LoaderDelgate newCallback = null;
            if (callback != null)
            {
                newCallback = (isOk, obj) => callback(isOk, obj as AssetBundle);
            }
            var newLoader = AutoNew <AssetBundleLoader>(url, newCallback, false, loaderMode);

            return(newLoader);
        }
Exemplo n.º 24
0
        protected override void Init(string url, params object[] args)
        {
#if UNITY_5 || UNITY_2017_1_OR_NEWER
            PreLoadManifest();
#endif

            base.Init(url);

            _loaderMode = (LoaderMode)args[0];

            if (NewAssetBundleLoaderEvent != null)
            {
                NewAssetBundleLoaderEvent(url);
            }

            RelativeResourceUrl = url;
            KResourceModule.LogRequest("AssetBundle", RelativeResourceUrl);
            KResourceModule.Instance.StartCoroutine(LoadAssetBundle(url));
        }
Exemplo n.º 25
0
        private IEnumerator LoadAssetBundle(string bundlePath, LoaderMode mode)
        {
            //Load bytes via HotBytesLoader
            var bytesLoader = HotBytesLoader.Load(bundlePath, mode);

            while (!bytesLoader.IsComplete)
            {
                yield return(null);
            }
            byte[] btsData = bytesLoader.Bytes;
            //Parse bytes, get assetbundle
            Object assetBundleObj = null;

            if (mode == LoaderMode.Sync)
            {
                assetBundleObj = AssetBundle.LoadFromMemory(btsData);
            }
            if (assetBundleObj != null)
            {
                OnFinish(assetBundleObj);
            }
        }
Exemplo n.º 26
0
        public static HotBytesLoader Load(string path, LoaderMode loaderMode)
        {
            var newLoader = AutoNew <HotBytesLoader>(path, null, false, loaderMode);

            return(newLoader);
        }
Exemplo n.º 27
0
 public static HotBytesLoader Load(string path, LoaderMode loaderMode)
 {
     var newLoader = AutoNew<HotBytesLoader>(path, null, false, loaderMode);
     return newLoader;
 }
Exemplo n.º 28
0
        private void StartLoader()
        {
            if (_mode == LoaderMode.DownloadProgramAndConfiguration)
            {
                if (cBUseProgram.Checked && cBUseConfig.Checked)
                {
                    _mode = LoaderMode.DownloadProgramAndConfiguration;
                }
                else
                {
                    if (cBUseProgram.Checked)
                    {
                        _mode = LoaderMode.DownloadProgram;
                    }
                    else
                    {
                        _mode = LoaderMode.DownloadConfiguration;
                    }
                }
            }
            else //_mode = LoaderMode.UploadProgramAndConfiguration
            {
                bool b1 = cBUseProgram.Checked && _progBuf != null;
                bool b2 = cBUseConfig.Checked && _confBuf != null;

                if (b1 && b2)
                {
                    _mode = LoaderMode.UploadProgramAndConfiguration;
                    _loader.ProgramBinBuffer       = _progBuf;
                    _loader.ConfigurationBinBuffer = _confBuf;
                }
                else
                {
                    if (b1)
                    {
                        _loader.ProgramBinBuffer = _progBuf;
                        _mode = LoaderMode.UploadProgram;
                    }
                    else
                    {
                        _loader.ConfigurationBinBuffer = _confBuf;
                        _mode = LoaderMode.UploadConfiguration;
                    }
                }
            }

            mmiSaveProgramAs.Enabled       = false;
            mmiSaveConfigurationAs.Enabled = false;
            mmiUpload.Enabled      = btnUpload.Enabled = false;
            mmiDownload.Enabled    = btnDownload.Enabled = false;
            mmiClearBuffer.Enabled = false;
            cBPortNumber.Enabled   = false;

            byte[] req = null;
            if (_currentPort != null)
            {
                try
                {
                    _currentPort.Open();
                    req = _currentPort.SendRequest(new byte[] { 0x00, 0xA0 }, 2);
                }
                finally
                {
                    _currentPort.Close();
                }
            }

            if (req != null)
            {
                _loader.ProgressChanged    += new ProgressChangedEventHandler(_loader_ProgressChanged);
                _loader.RunWorkerCompleted += new RunWorkerCompletedEventHandler(_loader_RunWorkerCompleted);
                _loader.Start(_currentPort, _mode);
            }
            else
            {
                _searcher.RunWorkerAsync(cBPortNumber.SelectedItem.ToString());
            }
        }
Exemplo n.º 29
0
 private void btnDownload_Click(object sender, EventArgs e)
 {
     _loader = new Downloader();
     _mode   = LoaderMode.DownloadProgramAndConfiguration;
     StartLoader();
 }
Exemplo n.º 30
0
        private IEnumerator _LoadCoroutine(string path, LoaderMode loaderMode, string package,
                                           LoadSceneMode loadSceneMode)
        {
            string scenePath = ResourceModuleConfig.GameResourcesDir + "/" + path;
            object getAsset  = null;

            if (ResManager.IsEdiotrMode && Application.isEditor)
            {
#if UNITY_EDITOR
                var allScenes = UnityEditor.EditorBuildSettings.scenes;
                UnityEditor.EditorBuildSettingsScene buildScene = null;
                foreach (var scene in allScenes)
                {
                    if (scene.path == scenePath)
                    {
                        buildScene = scene;
                        break;
                    }
                }

                if (buildScene == null || !buildScene.enabled)
                {
                    Debug.LogError("EditorMode must add scene to build settings and active:" + path);
                    OnFinish(null);
                    yield break;
                }

                ////编辑器状态下模拟异步加载延迟等待指定秒数
                //if (loaderMode == LoaderMode.Async)
                //{
                //    yield return new WaitForSecondsRealtime(ResManager.EditorModeLoadDelay);
                //}
#else
                Debug.LogErrorFormat("`IsEditorLoadAsset` is Unity Editor only");
#endif
            }
            else
            {
                DateTime beginTime  = DateTime.Now;
                string   bundlePath = string.IsNullOrEmpty(package) ? path : package;
                _bundleLoader =
                    AssetBundleLoader.Load(bundlePath + ResourceModuleConfig.AssetBundleExt, null, loaderMode);

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

                    this.Progress = _bundleLoader.Progress / 2f;
                    yield return(null);
                }

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

                ResManager.LogLoadTime("AssetFileBridge", loaderMode, path, beginTime);
            }

            this.Progress = 0.5f;
            if (loaderMode == LoaderMode.Async)
            {
                var asyncOp = SceneManager.LoadSceneAsync(scenePath, loadSceneMode);
                while (!asyncOp.isDone)
                {
                    this.Progress = 0.5f + asyncOp.progress / 2f;
                    yield return(null);
                }
            }
            else
            {
                SceneManager.LoadScene(scenePath, loadSceneMode);
            }

            this.Progress = 1f;
            getAsset      = true;

            OnFinish(getAsset);

            //加载完立即释放Loader
            ReleaseImmediate();
        }
Exemplo n.º 31
0
        /// <summary>
        /// AssetBundle读取原字节目录
        /// </summary>
        //private KResourceInAppPathType _inAppPathType;
        public static AssetBundleLoader Load(string url, CAssetBundleLoaderDelegate callback = null,
            LoaderMode loaderMode = LoaderMode.Async)
        {
            LoaderDelgate newCallback = null;
            if (callback != null)
            {
                newCallback = (isOk, obj) => callback(isOk, obj as AssetBundle);
            }
            var newLoader = AutoNew<AssetBundleLoader>(url, newCallback, false, loaderMode);

            return newLoader;
        }
Exemplo n.º 32
0
        public static StaticAssetLoader Load(string url, AssetFileLoader.AssetFileBridgeDelegate callback = null, LoaderMode loaderMode = LoaderMode.Async)
        {
            LoaderDelgate newCallback = null;

            if (callback != null)
            {
                newCallback = (isOk, obj) => callback(isOk, obj as UnityEngine.Object);
            }

            return(AutoNew <StaticAssetLoader>(url, newCallback, false, loaderMode));
        }
Exemplo n.º 33
0
        public static AssetFileLoader Load(string path, AssetFileBridgeDelegate assetFileLoadedCallback = null, LoaderMode loaderMode = LoaderMode.Async)
        {
            // 添加扩展名
            if (!IsEditorLoadAsset)
            {
                path = path + EngineConfig.instance.ABExtName;
            }

            LoaderDelgate realcallback = null;

            if (assetFileLoadedCallback != null)
            {
                realcallback = (isOk, obj) => assetFileLoadedCallback(isOk, obj as Object);
            }

            return(AutoNew <AssetFileLoader>(path, realcallback, false, loaderMode));
        }
Exemplo n.º 34
0
        public static AssetFileLoader Load(string path, AssetFileBridgeDelegate assetFileLoadedCallback = null, LoaderMode loaderMode = LoaderMode.Async)
        {
            // 添加扩展名
            if (!IsEditorLoadAsset)
            {
                path = path + AppEngine.GetConfig(KEngineDefaultConfigs.AssetBundleExt);
            }

            LoaderDelgate realcallback = null;

            if (assetFileLoadedCallback != null)
            {
                realcallback = (isOk, obj) => assetFileLoadedCallback(isOk, obj as Object);
            }

            return(AutoNew <AssetFileLoader>(path, realcallback, false, loaderMode));
        }
Exemplo n.º 35
0
        public static T AutoNew <T>(string url, LoaderDelegate callback, bool forceCreateNew = false, LoaderMode mode = LoaderMode.Sync)
            where T : AbstractResourceLoader, new()
        {
            if (callback != null)
            {
                mLoaderCallback = callback;
            }
            T loader = new T();

            loader.Init(url, mode);
            return(loader);
        }
Exemplo n.º 36
0
 public static SceneResolveLoader Load(string sceneName, string package = null,
                                       LoadSceneMode loadSceneMode      = LoadSceneMode.Single, LoaderDelgate onFinish = null,
                                       LoaderMode loaderMode            = LoaderMode.Async)
 {
     return(AutoNew <SceneResolveLoader>(sceneName, onFinish, false, loaderMode, package, loadSceneMode));
 }
Exemplo n.º 37
0
        protected override void Init(string url, params object[] args)
        {
            #if UNITY_5
            PreLoadManifest();
            #endif

            base.Init(url);

            _loaderMode = (LoaderMode)args[0];

            if (NewAssetBundleLoaderEvent != null)
                NewAssetBundleLoaderEvent(url);

            RelativeResourceUrl = url;
            KResourceModule.LogRequest("AssetBundle", RelativeResourceUrl);
            KResourceModule.Instance.StartCoroutine(LoadAssetBundle(url));
        }
Exemplo n.º 38
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);
        }
Exemplo n.º 39
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);
        }