Ejemplo n.º 1
0
    private IEnumerator LoadAssetBundle(string relativeUrl)
    {
        var bytesLoader = KBytesLoader.Load(relativeUrl, _inAppPathType, _loaderMode);

        while (!bytesLoader.IsCompleted)
        {
            yield return(null);
        }
        if (!bytesLoader.IsSuccess)
        {
            if (AssetBundlerLoaderErrorEvent != null)
            {
                AssetBundlerLoaderErrorEvent(this);
            }
            Logger.LogError("[KAssetBundleLoader]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)
        {
            Logger.LogError("WWW.assetBundle is NULL: {0}", RelativeResourceUrl);
        }

        OnFinish(assetBundle);

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

        //GC.Collect(0);// 手工释放内存
    }
Ejemplo n.º 2
0
        /// <summary>
        /// Default load setting strategry,  editor load file, runtime resources.load
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static byte[] DefaultLoadSetting(string path)
        {
            //NOTE 在Editor下Reload 配置表失败
#if UNITY_EDITOR
            return(LoadSettingFromFile(path));
#else
            byte[] fileContent;
            var    loader = KBytesLoader.Load(SettingFolderName + "/" + path, LoaderMode.Sync);
            Debuger.Assert(!loader.IsError);
            fileContent = loader.Bytes;

            loader.Release();
            return(fileContent);
#endif
        }
Ejemplo n.º 3
0
    public IEnumerator LoadUIAsset(CUILoadState openState, UILoadRequest request)
    {
        var name = openState.TemplateName;
        // 具体加载逻辑
        // manifest
        string manifestPath = ResourceDepUtils.GetBuildPath(string.Format("BundleResources/NGUI/{0}.prefab.manifest{1}", name,
                                                                          AppEngine.GetConfig(KEngineDefaultConfigs.AssetBundleExt)));
        var manifestLoader = KBytesLoader.Load(manifestPath, KResourceInAppPathType.PersistentAssetsPath, KAssetBundleLoaderMode.PersitentDataPathSync);

        while (!manifestLoader.IsCompleted)
        {
            yield return(null);
        }
        var manifestBytes = manifestLoader.Bytes;

        manifestLoader.Release(); // 释放掉文本字节
        var utf8NoBom    = new UTF8Encoding(false);
        var manifestList = utf8NoBom.GetString(manifestBytes).Split(new char[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries);

        for (var i = 0; i < manifestList.Length; i++)
        {
            var depPath   = manifestList[i] + AppEngine.GetConfig(KEngineDefaultConfigs.AssetBundleExt);
            var depLoader = KAssetFileLoader.Load(depPath);
            while (!depLoader.IsCompleted)
            {
                yield return(null);
            }
        }
        string path = ResourceDepUtils.GetBuildPath(string.Format("BundleResources/NGUI/{0}.prefab{1}", name, KEngine.AppEngine.GetConfig("AssetBundleExt")));

        var assetLoader = KStaticAssetLoader.Load(path);

        openState.UIResourceLoader = assetLoader; // 基本不用手工释放的
        while (!assetLoader.IsCompleted)
        {
            yield return(null);
        }
        request.Asset = assetLoader.TheAsset;
    }
Ejemplo n.º 4
0
        private static IEnumerator CoLoadAssetBundleAsync(string relativePath, ResourceDepRequest request)
        {
            // manifest
            string manifestPath = ResourceDepUtils.GetBuildPath(String.Format("{0}.manifest{1}", relativePath,
                                                                              AppEngine.GetConfig(KEngineDefaultConfigs.AssetBundleExt)));
            var manifestLoader = KBytesLoader.Load(manifestPath, LoaderMode.Sync);

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

            // manifest读取失败,可能根本没有manifest,是允许的
            if (manifestLoader.IsSuccess)
            {
                var manifestBytes = manifestLoader.Bytes;
                manifestLoader.Release(); // 释放掉文本字节
                string[] manifestList = GetManifestList(manifestBytes);
                for (var i = 0; i < manifestList.Length; i++)
                {
                    var depPath   = manifestList[i] + AppEngine.GetConfig(KEngineDefaultConfigs.AssetBundleExt);
                    var depLoader = KAssetFileLoader.Load(depPath);
                    if (request.Loaders == null)
                    {
                        request.Loaders = new List <KAbstractResourceLoader>();
                    }
                    request.Loaders.Add(depLoader);
                    while (!depLoader.IsCompleted)
                    {
                        yield return(null);
                    }
                }
            }
            string path =
                GetBuildPath(String.Format("{0}{1}", relativePath,
                                           AppEngine.GetConfig(KEngineDefaultConfigs.AssetBundleExt)));

            // 获取后缀名
            var ext = Path.GetExtension(relativePath);

            if (ext == ".unity" || ext == ".shader")
            {
                // Scene
                var sceneLoader = KAssetBundleLoader.Load(path);

                if (request.Loaders == null)
                {
                    request.Loaders = new List <KAbstractResourceLoader>();
                }
                request.Loaders.Add(sceneLoader);
                while (!sceneLoader.IsCompleted)
                {
                    yield return(null);
                }
            }
            else
            {
                var assetLoader = KAssetFileLoader.Load(path);
                while (!assetLoader.IsCompleted)
                {
                    yield return(null);
                }
                request.Asset = assetLoader.Asset;
            }

            request.IsDone = true;
        }
Ejemplo n.º 5
0
        /// <summary>
        /// 同步加载AssetBundle
        /// </summary>
        /// <param name="relativePath"></param>
        /// <returns></returns>
        public static Object LoadAssetBundleSync(string relativePath)
        {
            //CheckLoadShadersPrefab();
            // manifest
            string manifestPath = ResourceDepUtils.GetBuildPath(String.Format("{0}.manifest{1}", relativePath,
                                                                              AppEngine.GetConfig(KEngineDefaultConfigs.AssetBundleExt)));
            var manifestLoader = KBytesLoader.Load(manifestPath, LoaderMode.Sync);
            //while (!manifestLoader.IsCompleted)
            //    yield return null;
            var manifestBytes = manifestLoader.Bytes;

            manifestLoader.Release(); // 释放掉文本字节
            if (manifestBytes != null)
            {
                var manifestList = GetManifestList(manifestBytes);
                for (var i = 0; i < manifestList.Length; i++)
                {
                    var depPath = manifestList[i] + AppEngine.GetConfig(KEngineDefaultConfigs.AssetBundleExt);
                    //var depLoader =
                    KAssetFileLoader.Load(depPath);
                    //while (!depLoader.IsCompleted)
                    //{
                    //    yield return null;
                    //}

                    /*if (Application.isEditor)
                     * {
                     *  Log.Info("Load dep sync:{0}, from: {1}", depPath, relativePath);
                     * }*/
                }
            }
            else
            {
                Log.Warning("Cannot find Manifest: {0}", relativePath);
            }

            string path =
                GetBuildPath(String.Format("{0}{1}", relativePath,
                                           AppEngine.GetConfig(KEngineDefaultConfigs.AssetBundleExt)));

            //while (!assetLoader.IsCompleted)
            //    yield return null;
            // 获取后缀名
            var ext = Path.GetExtension(relativePath);

            if (ext == ".unity" || ext == ".shader")
            {
                // Scene
                //var sceneLoader =
                KAssetBundleLoader.Load(path);
                //while (!sceneLoader.IsCompleted)
                //    yield return null;
                return(null);
            }
            else
            {
                var assetLoader = KAssetFileLoader.Load(path);
                //while (!assetLoader.IsCompleted)
                //    yield return null;
                return(assetLoader.Asset);
            }
        }