Пример #1
0
        public IEnumerator PreloadShaderAndFont()
        {
            if (!AppConst.AssetBundleMode)
            {
                yield break;
            }
            string dataPath = Util.DataPath;                                    //数据目录
            string resPath  = AppPathUtils.StreamingPathFormat(LuaConst.osDir); //游戏包资源目录
            //加载shader.ab
            string   shaderABName = "shader.ab";
            string   shaderFile   = Path.Combine(resPath, shaderABName);
            string   outFile      = Path.Combine(dataPath, shaderABName);
            AssetMap map          = new AssetMap();

            map.name = shaderABName;
            map.type = AssetMap.LoadType.Preload;
            bundleLoadTypeMap.Add(shaderABName, map);
            yield return(preload(shaderFile, outFile, shaderABName));

            //加载font.ab
            string fontABName = "font.ab";
            string fontFile   = Path.Combine(resPath, fontABName);

            outFile  = Path.Combine(dataPath, fontABName);
            map      = new AssetMap();
            map.name = fontABName;
            map.type = AssetMap.LoadType.Preload;
            bundleLoadTypeMap.Add(fontABName, map);
            yield return(preload(fontFile, outFile, fontABName));

            font         = GetBundleFromLoaded(fontABName).LoadAsset("FZCuYuan") as Font;
            shaderAssets = GetBundleFromLoaded(shaderABName).LoadAllAssets();
            Shader.WarmupAllShaders();
        }
Пример #2
0
    /// <summary>
    /// 异步加载资源
    /// </summary>
    /// <param name="path">资源的相对路径,相对于Assets的根目录 eg: CustomRes/XXX.prefab</param>
    /// <param name="assetType">其它的基础数据类型</param>
    /// <param name="callback">加载完成时的回调</param>
    /// <returns></returns>
    public IEnumerator AsyncLoadAtPath(string path, EAssetBaseType assetType, Action <object> callback)
    {
        string finalPath = AppPathUtils.IsSeachExist(path, true);
        WWW    _www      = new WWW(finalPath);

        yield return(_www);

        if (_www.error != null)
        {
            Debugger.LogError(_www.error + " , path -->> " + finalPath);
            callback(null);
            yield break;
        }

        object obj = null;

        if (assetType == EAssetBaseType.Text)
        {
            obj = _www.text;
        }
        else if (assetType == EAssetBaseType.ByteArray)
        {
            obj = _www.bytes;
        }
        else if (assetType == EAssetBaseType.Texture2D)
        {
            obj = _www.texture;
        }
        if (callback != null)
        {
            callback(obj);
        }
    }
Пример #3
0
    /// <summary>
    /// 异步加载资源
    /// </summary>
    /// <param name="bundName">未加密的Bundle文件名</param>
    /// <param name="callback">加载完成时的回调</param>
    public IEnumerator AsyncLoadBundleAtPath(string bundName, Action <AssetBundle> callback, Action <float, float> progress)
    {
        string finalPath = AppPathUtils.IsSeachExist(GetBundleEncrypeName(bundName), true);
        WWW    _www      = new WWW(finalPath);

        if (progress == null)
        {
            yield return(_www);
        }
        else
        {
            int totalProgress = 0, curTotalProgress;
            int offset = 0;
            while (!_www.isDone)
            {
                curTotalProgress = (int)(_www.progress * 100);
                offset           = curTotalProgress - totalProgress;
                totalProgress    = curTotalProgress;
                progress.Invoke(offset, totalProgress);
                yield return(null);
            }

            curTotalProgress = totalProgress == 0 ? (int)(_www.progress * 100) : totalProgress;
            offset           = 100 - curTotalProgress;
            totalProgress    = 100;
            progress.Invoke(offset, totalProgress);
            while (curTotalProgress < totalProgress)
            {
                curTotalProgress++;
                yield return(null);
            }

            yield return(null);
        }
        if (_www.error != null)
        {
            Debugger.LogError(_www.error + " , path -->> " + finalPath);
            callback(null);
            yield break;
        }

        if (callback != null)
        {
            callback(_www.assetBundle);
        }
    }