コード例 #1
0
 /// <summary>
 /// 判断是不是固定打包资源
 /// </summary>
 /// <returns><c>true</c>, if fixed build asset was ised, <c>false</c> otherwise.</returns>
 /// <param name="path">相对地址.</param>
 static bool isFixedBuildAsset(string path)
 {
     if (path.IndexOf(FilePathTools.getRelativePath(buildRootPath)) == -1)
     {
         return(false);
     }
     return(true);
 }
コード例 #2
0
    /// <summary>
    /// 通过相对地址获取assetbundle的名字
    /// </summary>
    /// <returns>The asset bundle name with path.</returns>
    /// <param name="path">相对地址.</param>
    static string getAssetBundleNameWithPath(string path)
    {
        string p = Path.GetDirectoryName(path) + "/" + Path.GetFileNameWithoutExtension(path);

        //判断是依赖资源还是固定资源
        if (!isFixedBuildAsset(p))
        {
            p = FilePathTools.replaceFirst(p, "Assets", "Dependencie");
            //p = p.Replace ("Assets","Dependencie");
        }
        else
        {
            p = FilePathTools.replaceFirst(p, FilePathTools.getRelativePath(buildRootPath) + "/", "");
            //p = p.Replace (buildRoot + "/","");
        }
        return(p);
    }
コード例 #3
0
    static AssetBundleBuild[] GetBuildFileListNew(string buildRoot)
    {
        //获取所有固定打包的文件
        FileInfo[] files = FilePathTools.getFiles(buildRoot);
        //List<string> fixedPaths = new List<string>();
        List <AssetBundleBuild> buildMap = new List <AssetBundleBuild>();

        for (int i = 0; i < files.Length; i++)
        {
            //获取相对于asset目录的相对路径
            string path = FilePathTools.getRelativePath(files[i].FullName);

            AssetBundleBuild build = new AssetBundleBuild();
            build.assetBundleName = getAssetBundleNameWithPath(path);
            build.assetNames      = new string[1] {
                path
            };
            buildMap.Add(build);
        }

        Debug.Log("获取所有打包资源完毕:" + buildMap.Count);
        return(buildMap.ToArray());
    }
コード例 #4
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);
    }
コード例 #5
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);
    }
コード例 #6
0
    //获取所有需要打包的文件
    static AssetBundleBuild[] getBuildFileList(string buildRoot)
    {
        //获取所有固定打包的文件
        FileInfo[]    files      = FilePathTools.getFiles(buildRoot);
        List <string> fixedPaths = new List <string>();      //固定打包资源(必定被打包的资源)

        for (int i = 0; i < files.Length; i++)
        {
            //获取相对于asset目录的相对路径
            fixedPaths.Add(FilePathTools.getRelativePath(files[i].FullName));
        }
        //找出固定打包的文件以及其所有依赖文件 排重
        string[] allDependencies = AssetDatabase.GetDependencies(fixedPaths.ToArray(), true);        //所有的依赖关系,包括自己并已排重

        //寻找固定打包文件和依赖文件的第一层依赖关系并进行依赖计数
        Dictionary <string, int> dependenciesCount = new Dictionary <string, int> ();     //依赖计数用字典

        foreach (string path in allDependencies)
        {
            if (Path.GetExtension(path) == ".spriteatlas")
            {
                continue;
            }

            string[] dependencie = AssetDatabase.GetDependencies(path, false);
            foreach (string p in dependencie)
            {
                if (!dependenciesCount.ContainsKey(p))
                {
                    dependenciesCount.Add(p, 1);
                }
                else
                {
                    dependenciesCount[p]++;
                }
            }
        }
        //将计数器大于1的资源提取出来,剔除掉固定打包的资源
        List <AssetBundleBuild> buildMap          = new List <AssetBundleBuild> ();
        List <string>           dependenciesPaths = new List <string> ();//被引用次数大于1的依赖资源 需要进行打包

        foreach (string key in dependenciesCount.Keys)
        {
            int count = dependenciesCount[key];
            if (count > 1 && !isFixedBuildAsset(key))
            {
                dependenciesPaths.Add(key);
            }
        }
        //合并固定打包资源和依赖打包资源
        List <string> allBuildPaths = new List <string> (fixedPaths);

        allBuildPaths.AddRange(dependenciesPaths);

        foreach (string path in allBuildPaths)
        {
            //去掉脚本资源
            if (Path.GetExtension(path) == ".cs")
            {
                continue;
            }

            AssetBundleBuild build = new AssetBundleBuild();
            build.assetBundleName = getAssetBundleNameWithPath(path);
            build.assetNames      = new string[1] {
                path
            };
            buildMap.Add(build);
            Debug.Log(build.assetBundleName + " | " + build.assetNames[0]);
        }

        return(buildMap.ToArray());
    }
コード例 #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;
    }