Example #1
0
    /// <summary>
    /// 通用资源AssetBundle卸载方法[Unload(true)];
    /// </summary>
    /// <param name="type">资源类型</param>
    /// <param name="assetName">资源名字</param>
    public void UnloadAsset(AssetType type, string assetName)
    {
        if (type == AssetType.Non || type == AssetType.Shader || type == AssetType.Scripts || string.IsNullOrEmpty(assetName))
        {
            return;
        }

        string assetBundleName = FilePathUtil.GetAssetBundleFileName(type, assetName);

        string[] DependentAssetBundle = Manifest.GetAllDependencies(assetBundleName);
        foreach (string tempAssetBundle in DependentAssetBundle)
        {
            if (tempAssetBundle == FilePathUtil.GetAssetBundleFileName(AssetType.Shader, "Shader"))
            {
                continue;
            }
            string tempPtah = FilePathUtil.assetBundlePath + tempAssetBundle;
            UnloadAsset(tempPtah, true);
        }
        string assetBundlePath = FilePathUtil.GetAssetBundlePath(type, assetName);

        if (assetBundlePath != null)
        {
            UnloadAsset(assetBundlePath, true);
        }
    }
Example #2
0
    /// <summary>
    /// AssetBundle同步加载;
    /// </summary>
    /// <param name="type">资源类型</param>
    /// <param name="assetName">资源名字</param>
    /// <returns>AssetBundle</returns>
    public AssetBundle LoadAssetBundleSync(AssetType type, string assetName)
    {
        if (type == AssetType.Non || string.IsNullOrEmpty(assetName))
        {
            return(null);
        }

        string assetBundlePath = FilePathUtil.GetAssetBundlePath(type, assetName);

        if (assetBundlePath == null)
        {
            return(null);
        }
        string assetBundleName = FilePathUtil.GetAssetBundleFileName(type, assetName);

        AssetBundle assetBundle = LoadSingleAssetBundleSync(assetBundlePath);

        if (assetBundle == null)
        {
            return(null);
        }

        //返回AssetBundleName;
        string[] DependentAssetBundle = Manifest.GetAllDependencies(assetBundleName);
        foreach (string tempAssetBundle in DependentAssetBundle)
        {
            if (tempAssetBundle == FilePathUtil.GetAssetBundleFileName(AssetType.Shader, "Shader"))
            {
                continue;
            }
            string tempPtah = FilePathUtil.assetBundlePath + tempAssetBundle;
            LoadSingleAssetBundleSync(tempPtah);
        }
        return(assetBundle);
    }
Example #3
0
    public static void BuildLua()
    {
        ToLuaMenu.ClearLuaFilesFromSrcPath();
        ToLuaMenu.CopyLuaFilesToSrcPath();

        string[] allPath = Directory.GetFiles(FilePathUtil.resPath + "Lua/", "*.*", SearchOption.AllDirectories);

        //剔除.meta文件;
        List <string> allLuaAsset = new List <string>();

        foreach (string tempPath in allPath)
        {
            string path = tempPath.Replace("\\", "/");
            if (Path.GetExtension(path) == ".meta")
            {
                continue;
            }
            allLuaAsset.Add(path);
        }
        int index = 0;

        foreach (string tempPath in allLuaAsset)
        {
            index++;
            EditorUtility.DisplayProgressBar("Set Asset AssetBundle Name", "AssetBundle Name Setting Progress", (index / allLuaAsset.Count));
            AssetImporter importer = AssetImporter.GetAtPath(tempPath);
            if (importer != null)
            {
                importer.assetBundleName = FilePathUtil.GetAssetBundleFileName(AssetType.Lua, "Lua");
                AssetDatabase.ImportAsset(tempPath);
            }
        }
        EditorUtility.ClearProgressBar();

        //tips:Unity5.x Scripts not need to build AssetBundle
        //analysiser.BuildAllScripts();
        BuildAssetBundle();
    }
Example #4
0
    /// <summary>
    /// AssetBundle异步加载;
    /// </summary>
    /// <param name="type">资源类型</param>
    /// <param name="assetName">资源名字</param>
    /// <param name="action">AssetBundle回调</param>
    /// <param name="progress">progress回调</param>
    /// <returns></returns>
    public IEnumerator LoadAssetBundleAsync(AssetType type, string assetName, Action <AssetBundle> action, Action <float> progress)
    {
        if (type == AssetType.Non || string.IsNullOrEmpty(assetName))
        {
            yield break;
        }
        string assetBundlePath = FilePathUtil.GetAssetBundlePath(type, assetName);

        if (assetBundlePath == null)
        {
            yield break;
        }
        string assetBundleName = FilePathUtil.GetAssetBundleFileName(type, assetName);

        //先加载依赖的AssetBundle;
        string[] DependentAssetBundle = Manifest.GetAllDependencies(assetBundleName);
        foreach (string tempAssetBundle in DependentAssetBundle)
        {
            if (tempAssetBundle == FilePathUtil.GetAssetBundleFileName(AssetType.Shader, "Shader"))
            {
                continue;
            }
            string      tempPtah = FilePathUtil.assetBundlePath + tempAssetBundle;
            IEnumerator itor     = LoadSingleAssetBundleAsync(tempPtah, null, null);
            while (itor.MoveNext())
            {
                yield return(null);
            }
        }
        //加载目标AssetBundle;
        IEnumerator itorTarget = LoadSingleAssetBundleAsync(assetBundlePath, action, progress);

        while (itorTarget.MoveNext())
        {
            yield return(null);
        }
    }
Example #5
0
    /// <summary>
    /// 分析全部资源依赖关系;
    /// </summary>
    public void AnalysisAllAsset()
    {
        Stopwatch watch = Stopwatch.StartNew();//开启计时;

        string[] allPath = Directory.GetFiles(FilePathUtil.resPath, "*.*", SearchOption.AllDirectories);

        //剔除.meta文件;
        List <string> allAssetPath = new List <string>();

        foreach (string tempPath in allPath)
        {
            string path = tempPath.Replace("\\", "/");
            if (Path.GetExtension(path) == ".meta")
            {
                continue;
            }
            allAssetPath.Add(path);
        }

        //开始分析资源依赖关系;
        for (int i = 0; i < allAssetPath.Count; i++)
        {
            EditorUtility.DisplayProgressBar("Start Analysis All Asset", "Analysis Progress", (i / allAssetPath.Count));

            //还未遍历到该资源;
            if (!allAsset.ContainsKey(allAssetPath[i]))
            {
                allAsset[allAssetPath[i]] = CreateNewAssetNode(allAssetPath[i]);
            }
            //获取依赖关系;
            string[] allDirectDependencies = AssetDatabase.GetDependencies(allAssetPath[i], false);
            foreach (string tempPath in allDirectDependencies)
            {
                //依赖脚本直接添加到脚本队列;
                if (BuildDefine.GetAssetType(tempPath) == AssetType.Scripts)
                {
                    continue;
                }
                //依赖Shader直接添加到Shader队列;
                if (BuildDefine.GetAssetType(tempPath) == AssetType.Shader)
                {
                    allShaderAsset.Add(tempPath);
                    continue;
                }
                //Lua文件直接添加到Lua队列;
                if (BuildDefine.GetAssetType(tempPath) == AssetType.Lua)
                {
                    allLuaAsset.Add(tempPath);
                    continue;
                }
                if (tempPath.Contains(FilePathUtil.resPath))
                {
                    //添加依赖的资源信息;
                    allAsset[allAssetPath[i]].sonDependentAssets.Add(tempPath);
                    //添加被依赖的资源信息;
                    if (!allAsset.ContainsKey(tempPath))
                    {
                        allAsset[tempPath] = CreateNewAssetNode(tempPath);
                    }
                    allAsset[tempPath].parentDependentAssets.Add(allAssetPath[i]);
                }
                else
                {
                    //需要打包AssetBundle的资源目录下的资源,引用非该目录下的资源!!!
                    Debug.LogError("[error Reference] path:" + allAssetPath[i] + " Reference--->: " + tempPath);
                }
            }
        }
        EditorUtility.ClearProgressBar();

        //找出需要打包的资源;
        for (int i = 0; i < allAssetPath.Count; i++)
        {
            EditorUtility.DisplayProgressBar("Start Search Independence Asset", "Search Progress", (i / allAssetPath.Count));

            if (allAssetPath[i].Contains("Atlas") && Path.GetExtension(allAssetPath[i]) == ".prefab")  //图集特殊处理;
            {
                independenceAsset[allAssetPath[i]] = allAsset[allAssetPath[i]];
                continue;
            }
            if (allAssetPath[i].Contains("Shaders") && Path.GetExtension(allAssetPath[i]) == ".shader")
            {
                allShaderAsset.Add(allAssetPath[i]);
                continue;
            }
            if (allAssetPath[i].Contains("Lua") && Path.GetExtension(allAssetPath[i]) == ".bytes")
            {
                allLuaAsset.Add(allAssetPath[i]);
                continue;
            }
            if (allAsset[allAssetPath[i]].parentDependentAssets.Count == 0 || //没有被依赖的资源;
                allAsset[allAssetPath[i]].parentDependentAssets.Count > 1 ||  //被超过一个资源依赖的资源;
                allAssetPath[i].Contains(FilePathUtil.singleResPath))         //指定要求单独打包的资源;
            {
                independenceAsset[allAssetPath[i]] = allAsset[allAssetPath[i]];
            }
        }
        EditorUtility.ClearProgressBar();

        //设置资源AssetBundle Name;
        for (int i = 0; i < allAssetPath.Count; i++)
        {
            EditorUtility.DisplayProgressBar("Set Asset AssetBundle Name", "AssetBundle Name Setting Progress", (i / allAssetPath.Count));

            AssetImporter importer = AssetImporter.GetAtPath(allAssetPath[i]);
            if (importer != null)
            {
                if (independenceAsset.ContainsKey(allAssetPath[i]))
                {
                    importer.assetBundleName = FilePathUtil.GetAssetBundleFileName(independenceAsset[allAssetPath[i]].type, independenceAsset[allAssetPath[i]].assetName);
                }
                else
                {
                    importer.assetBundleName = null;
                }
                AssetDatabase.ImportAsset(allAssetPath[i]);
            }
        }
        EditorUtility.ClearProgressBar();

        int index = 0;

        //设置Shader AssetBundle Name;
        foreach (string tempPath in allShaderAsset)
        {
            index++;
            EditorUtility.DisplayProgressBar("Set Asset AssetBundle Name", "AssetBundle Name Setting Progress", (index / allShaderAsset.Count));
            AssetImporter importer = AssetImporter.GetAtPath(tempPath);
            if (importer != null)
            {
                importer.assetBundleName = FilePathUtil.GetAssetBundleFileName(AssetType.Shader, "Shader");
                AssetDatabase.ImportAsset(tempPath);
            }
        }
        index = 0;
        //设置Lua文件 AssetBundle Name;
        foreach (string tempPath in allLuaAsset)
        {
            index++;
            EditorUtility.DisplayProgressBar("Set Asset AssetBundle Name", "AssetBundle Name Setting Progress", (index / allShaderAsset.Count));
            AssetImporter importer = AssetImporter.GetAtPath(tempPath);
            if (importer != null)
            {
                importer.assetBundleName = FilePathUtil.GetAssetBundleFileName(AssetType.Lua, "Lua");
                AssetDatabase.ImportAsset(tempPath);
            }
        }
        EditorUtility.ClearProgressBar();

        watch.Stop();
        Debug.LogWarning(string.Format("[AssetDependenciesAnalysis]Asset Dependencies Analysis Spend Time:{0}s", watch.Elapsed.TotalSeconds));

        AssetDatabase.Refresh();
    }