Exemplo n.º 1
0
    public IEnumerator LoadAssetAsync <T>(string url, Action <T> callback) where T : Object
    {
        string path = url;

        string localPath = CacheManager.instance.GetLocalPath(url);

        if (!string.IsNullOrEmpty(localPath))
        {
            path = localPath;
        }
        //Debug.Log("==开始使用WWW下载==:" + path);

        path = FilePathTools.normalizePath(path);
        WWW www = new WWW(path);

        yield return(www);

        if (string.IsNullOrEmpty(www.error))
        {
            object res;
            Type   type = typeof(T);
            if (type == typeof(Texture2D))
            {
                Texture2D tex = new Texture2D(4, 4);
                www.LoadImageIntoTexture(tex);
                res = tex;
            }
            else if (type == typeof(AssetBundle))
            {
                res = www.assetBundle;
            }
            else if (type == typeof(string))
            {
                res = www.text;
            }
            else if (type == typeof(TextAsset))
            {
                res = www.text;
            }
            else
            {
                res = www.bytes;
            }

            CacheManager.instance.AddCache(url, www.bytes);

            www.Dispose();
            callback((T)res);
        }
        else
        {
            Debug.Log(url);
            Debug.Log(www.error);
            www.Dispose();
        }
    }
Exemplo n.º 2
0
    public void LoadAssetWithWWW(string url, Action <WWW> callback)
    {
        string path = url;

        path = FilePathTools.normalizePath(path);
        queue.Enqueue(() =>
        {
            TryClearCache();
            StartCoroutine(LoadWithWWW(url, callback));
        });
    }
Exemplo n.º 3
0
    /// <summary>
    /// 加载一个本地资源(file)
    /// </summary>
    /// <param name="path"></param>
    public void LoadAsset <T>(string url, Action <T> callback, bool isCache = true)
    {
        string path = url;

        path = FilePathTools.normalizePath(path);
        //为了保证后请求的图片就在后面刷新,避免列表复用时下载没结束被新的缓存图片覆盖,加载结束又被之前的图片覆盖掉,这里即使有缓存图片也要加入队列排队
        queue.Enqueue(() =>
        {
            TryClearCache();
            StartCoroutine(loadAsync <T>(path, callback, isCache));
        });
    }
Exemplo n.º 4
0
    /// <summary>
    /// 加载一个本地资源(file)
    /// </summary>
    /// <param name="path"></param>
    public void LoadAsset <T>(string url, Action <T> callback) where T : Object
    {
        string path = url;

        path = FilePathTools.normalizePath(path);

        CacheObject co;

        if (callback != null && dicCacheObject.TryGetValue(path, out co) && co != null)
        {
            callback(co.obj as T);
            co.time = Time.time;
        }
        else
        {
            queue.Enqueue(() =>
            {
                TryClearCache();
                StartCoroutine(loadAsync <T>(path, callback));
            });
        }
    }
Exemplo n.º 5
0
    /// <summary>
    /// 异步加载资源
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="path">资源的相对路径</param>
    /// <param name="callback"></param>
    /// <returns></returns>
    public IEnumerator LoadAssetAsync <T>(string path, Action <T> callback) where T : Object
    {
        path = FilePathTools.normalizePath(path);
        Debug.Log("===AssetBundleLoader.loadAsync:" + path);
        string assetBundleName = path;

        path = FilePathTools.root + path;//改成绝对路径

#if UNITY_EDITOR
        bool isUseAssetBundle = GameSetting.isUseAssetBundle;
        if (!isUseAssetBundle)
        {
            path = FilePathTools.getRelativePath(path);            //绝对路径转为相对Assets文件夹的相对路径
            Object Obj = UnityEditor.AssetDatabase.LoadAssetAtPath(path, typeof(T));


            if (Obj == null)
            {
                Debug.LogError("Asset not found at path:" + path);
            }
            callback((T)Obj);
            yield break;
        }
#endif
        //打的ab包都资源名称和文件名都是小写的
        AssetBundleRequest       assetRequest;
        AssetBundleCreateRequest createRequest;
        //1加载Manifest文件
        if (manifest == null)
        {
            string manifestPath = FilePathTools.manifestPath;
            Debug.Log("start load Manifest:" + manifestPath);
            createRequest = AssetBundle.LoadFromFileAsync(manifestPath);
            yield return(createRequest);

            if (createRequest.isDone)
            {
                AssetBundle manifestAB = createRequest.assetBundle;
                yield return(assetRequest = manifestAB.LoadAssetAsync <AssetBundleManifest>("AssetBundleManifest"));

                manifest = assetRequest.asset as AssetBundleManifest;
                manifestAB.Unload(false);
            }
            else
            {
                Debug.Log("Manifest加载出错");
            }
        }


        //2获取文件依赖列表
        string[] dependencies = manifest.GetAllDependencies(assetBundleName);

        //3加载依赖资源
        Dictionary <string, AssetBundle> dependencyAssetBundles = new Dictionary <string, AssetBundle>();
        //Debug.Log("---开始加载依赖资源:" + dependencies.Length.ToString());
        foreach (string fileName in dependencies)
        {
            string dependencyPath = FilePathTools.root + "/" + fileName;
            Debug.Log("开始加载依赖资源:" + dependencyPath);

            createRequest = AssetBundle.LoadFromFileAsync(dependencyPath);
            yield return(createRequest);

            if (createRequest.isDone)
            {
                dependencyAssetBundles.Add(dependencyPath, createRequest.assetBundle);
            }
            else
            {
                Debug.Log("加载依赖资源出错");
            }
        }
        //4加载目标资源
        obj = null;
        Debug.Log("---开始加载目标资源:" + path);
        createRequest = AssetBundle.LoadFromFileAsync(path);
        yield return(createRequest);

        List <AssetBundle> abList = new List <AssetBundle>();
        if (createRequest.isDone)
        {
            AssetBundle assetBundle = createRequest.assetBundle;
            yield return(assetRequest = assetBundle.LoadAssetAsync(Path.GetFileNameWithoutExtension(path), typeof(T)));

            obj = assetRequest.asset;


            //5释放目标资源
            //Debug.Log("---释放目标资源:" + path);
            abList.Add(assetBundle);
        }
        else
        {
            Debug.Log("加载目标资源出错 ");
        }



        if (dependencyAssetBundles != null)
        {
            //6释放依赖资源
            foreach (string key in dependencyAssetBundles.Keys)
            {
                //Debug.Log("---释放依赖资源:" + key);
                AssetBundle dependencyAB = dependencyAssetBundles[key];
                abList.Add(dependencyAB);
            }
        }


        callback((T)obj);
        UnloadAssetbundle(abList);
        //Debug.Log("---end loadAsync:AssetBundleLoader.loadAsync" + path);
        yield return(null);
    }
Exemplo n.º 6
0
    /// <summary>
    /// 加载资源
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="path">资源的相对路径</param>
    /// <returns></returns>
    public T LoadAsset <T>(string path) where T : Object
    {
        path = FilePathTools.normalizePath(path);
        Debug.Log("===AssetBundleLoader.Load:" + path);
        string assetBundleName = path;

        path = FilePathTools.root + path;//改成绝对路径
#if UNITY_EDITOR
        if (!GameSetting.isUseAssetBundle)
        {
            path = FilePathTools.getRelativePath(path);
            Object Obj = UnityEditor.AssetDatabase.LoadAssetAtPath(path, typeof(T));


            if (Obj == null)
            {
                Debug.LogError("Asset not found at path:" + path);
            }
            return((T)Obj);
        }
#endif
        //打的ab包都资源名称和文件名都是小写的

        //1加载Manifest文件
        if (manifest == null)
        {
            string manifestPath = FilePathTools.manifestPath;
            Debug.Log("start load Manifest:" + manifestPath);

            AssetBundle manifestAB = AssetBundle.LoadFromFile(manifestPath);
            manifest = manifestAB.LoadAsset <AssetBundleManifest>("AssetBundleManifest");
            manifestAB.Unload(false);
        }


        //2获取文件依赖列表
        string[] dependencies = manifest.GetAllDependencies(assetBundleName);

        //3加载依赖资源
        Dictionary <string, AssetBundle> dependencyAssetBundles = new Dictionary <string, AssetBundle>();
        //Debug.Log("---开始加载依赖资源:" + dependencies.Length.ToString());
        foreach (string fileName in dependencies)
        {
            string dependencyPath = FilePathTools.root + "/" + fileName;
            Debug.Log("开始加载依赖资源:" + dependencyPath);

            dependencyAssetBundles.Add(dependencyPath, AssetBundle.LoadFromFile(dependencyPath));
        }
        //4加载目标资源
        //Object obj = null;
        Debug.Log("---开始加载目标资源:" + path);
        AssetBundle assetBundle = AssetBundle.LoadFromFile(path);
        obj = assetBundle.LoadAsset <T>(Path.GetFileNameWithoutExtension(path));;

        //5释放目标资源
        //Debug.Log("---释放目标资源:" + path);
        List <AssetBundle> abList = new List <AssetBundle>();
        abList.Add(assetBundle);

        if (dependencyAssetBundles != null)
        {
            //6释放依赖资源
            foreach (string key in dependencyAssetBundles.Keys)
            {
                //Debug.Log("---释放依赖资源:" + key);
                AssetBundle dependencyAB = dependencyAssetBundles[key];
                abList.Add(dependencyAB);
            }
        }
        UnloadAssetbundle(abList);

        return((T)obj);
    }
Exemplo n.º 7
0
    private IEnumerator loadAsync <T>(string path, Action <T> callback) where T : Object
    {
        CacheObject co;

        if (callback != null && dicCacheObject.TryGetValue(path, out co) && co != null)
        {
            callback(co.obj as T);
            co.time = Time.time;
            yield break;
        }

        isLoading = true;

        path = FilePathTools.normalizePath(path);
        Debug.Log("===AssetBundleLoader.loadAsync:" + path);
        string assetBundleName = FilePathTools.getAssetBundleNameWithPath(path);

        bool isUseAssetBundle = GameSetting.isUseAssetBundle;

        if (!isUseAssetBundle)
        {
#if UNITY_EDITOR
            path = FilePathTools.getRelativePath(path);
            Object Obj = UnityEditor.AssetDatabase.LoadAssetAtPath(path, typeof(T));


            if (Obj == null)
            {
                Debug.LogError("Asset not found at path:" + path);
            }
            callback((T)Obj);
#endif
        }
        else
        {
            //打的ab包都资源名称和文件名都是小写的
            AssetBundleRequest       assetRequest;
            AssetBundleCreateRequest createRequest;
            //1加载Manifest文件
            if (manifest == null)
            {
                string manifestPath = FilePathTools.manifestPath;
                Debug.Log("start load Manifest:" + manifestPath);
                createRequest = AssetBundle.LoadFromFileAsync(manifestPath);
                yield return(createRequest);

                if (createRequest.isDone)
                {
                    AssetBundle manifestAB = createRequest.assetBundle;
                    yield return(assetRequest = manifestAB.LoadAssetAsync <AssetBundleManifest>("AssetBundleManifest"));

                    manifest = assetRequest.asset as AssetBundleManifest;
                    manifestAB.Unload(false);
                }
                else
                {
                    Debug.Log("Manifest加载出错");
                }
            }


            //2获取文件依赖列表
            string[] dependencies = manifest.GetAllDependencies(assetBundleName);

            //3加载依赖资源
            Dictionary <string, AssetBundle> dependencyAssetBundles = new Dictionary <string, AssetBundle>();
            //Debug.Log("---开始加载依赖资源:" + dependencies.Length.ToString());
            foreach (string fileName in dependencies)
            {
                string dependencyPath = FilePathTools.root + "/" + fileName;
                if (!GameMainManager.instance.preloader.Contains(dependencyPath))
                {
                    Debug.Log("开始加载依赖资源:" + dependencyPath);

                    createRequest = AssetBundle.LoadFromFileAsync(dependencyPath);
                    yield return(createRequest);

                    if (createRequest.isDone)
                    {
                        dependencyAssetBundles.Add(dependencyPath, createRequest.assetBundle);
                    }
                    else
                    {
                        Debug.Log("加载依赖资源出错");
                    }
                }
            }
            //4加载目标资源
            Object obj = null;
            Debug.Log("---开始加载目标资源:" + path);
            createRequest = AssetBundle.LoadFromFileAsync(path);
            yield return(createRequest);

            List <AssetBundle> abList = new List <AssetBundle>();
            if (createRequest.isDone)
            {
                AssetBundle assetBundle = createRequest.assetBundle;
                yield return(assetRequest = assetBundle.LoadAssetAsync(Path.GetFileNameWithoutExtension(path), typeof(T)));

                obj = assetRequest.asset;

                AddCache(path, obj);
                //5释放目标资源
                //Debug.Log("---释放目标资源:" + path);
                abList.Add(assetBundle);
            }
            else
            {
                Debug.Log("加载目标资源出错 ");
            }



            if (dependencyAssetBundles != null)
            {
                //6释放依赖资源
                foreach (string key in dependencyAssetBundles.Keys)
                {
                    //Debug.Log("---释放依赖资源:" + key);
                    AssetBundle dependencyAB = dependencyAssetBundles[key];
                    abList.Add(dependencyAB);
                }
            }


            callback((T)obj);
            StartCoroutine(UnloadAssetbundle(abList));
        }
        //Debug.Log("---end loadAsync:AssetBundleLoader.loadAsync" + path);
        yield return(null);

        isLoading = false;
    }
Exemplo n.º 8
0
    private IEnumerator loadAsync <T>(string url, Action <T> callback, bool isCache)
    {
        string path = url;

        if (isCache)
        {
            CacheObject obj;
            if (callback != null && cache.TryGetValue(url, out obj) && obj != null)
            {
                callback((T)obj.obj);
                obj.time = Time.time;
                yield break;
            }

            string localPath = CacheManager.instance.GetLocalPath(url);
            if (!string.IsNullOrEmpty(localPath))
            {
                path = localPath;
            }
        }

        isLoading = true;


        //Debug.Log("==开始使用WWW下载==:" + path);

        path = FilePathTools.normalizePath(path);
        WWW www = new WWW(path);

        yield return(www);

        if (string.IsNullOrEmpty(www.error))
        {
            object res;
            Type   type = typeof(T);
            if (type == typeof(Texture2D))
            {
                Texture2D tex = new Texture2D(4, 4);
                www.LoadImageIntoTexture(tex);
                res = tex;
            }
            else if (type == typeof(AssetBundle))
            {
                res = www.assetBundle;
            }
            else if (type == typeof(string))
            {
                res = www.text;
            }
            else
            {
                res = www.bytes;
            }

            if (isCache)
            {
                if (!cache.ContainsKey(url))
                {
                    AddCache(url, res);
                }
                CacheManager.instance.AddCache(url, www.bytes);
            }

            www.Dispose();
            isLoading = false;
            callback((T)res);
        }
        else
        {
            Debug.Log(url);
            Debug.Log(www.error);
            www.Dispose();
            isLoading = false;
        }
    }