Пример #1
0
    private static bool BuildAllFileToAssetBundle(string exportPath, AssetInfo asset)
    {
        bool   isIncludeAllFile = false;
        string includeFilePath  = "";

        foreach (var path in BuildAllFilePaths)
        {
            if (asset.Path.StartsWith(path))
            {
                isIncludeAllFile = true;
                includeFilePath  = path;
                break;
            }
        }

        if (isIncludeAllFile)
        {
            //判断是不是已经生成正常的AssetBundle
            var tempPath = includeFilePath.Substring(0, includeFilePath.Length - 1);
            tempPath += "/" + Path.GetFileNameWithoutExtension(tempPath);
            var assetbundlePath = exportPath + "/" + tempPath + ".pkg";
            if (HasBuildAssetBundleMap.Contains(assetbundlePath))
            {
                return(false);
            }
            EditorFileUtility.CreateParentDirecotry(assetbundlePath);

            //收集所有同一个目录下(包括子目录)的asset
            List <AssetInfo> readyToBuildAssets = new List <AssetInfo>();
            foreach (var gameAsset in GameAssets)
            {
                if (gameAsset.Value.Path.StartsWith(includeFilePath))
                {
                    readyToBuildAssets.Add(gameAsset.Value);
                }
            }

            //收集asset的所有依赖,如果依赖是同一个assetBundle下的跳过
            List <string> allChildDependences = new List <string>();
            foreach (var gameAsset in readyToBuildAssets)
            {
                foreach (var dependence in gameAsset.ChildDependences)
                {
                    if (dependence.StartsWith(includeFilePath))
                    {
                        continue;
                    }
                    allChildDependences.Add(dependence);
                }
            }

            //删除临时目录的AssetBundle
            EditorFileUtility.DeleteDirectory(exportPath + "/temp");
            HasBuildAssetBundleMapTemp.Clear();

            BuildPipeline.PushAssetDependencies();

            foreach (var dependence in allChildDependences)
            {
                if (BuildAllFileToAssetBundleTemp(allChildDependences, exportPath, dependence))
                {
                    continue;
                }

                if (BuildSingleFileToAssetBundleTemp(exportPath, dependence))
                {
                    continue;
                }

                BuildCurrentDirectoryToAssetBundleTemp(allChildDependences, exportPath, dependence);
            }

            List <Object> objects    = new List <Object>();
            List <string> assetNames = new List <string>();

            foreach (var buildAsset in readyToBuildAssets)
            {
                objects.Add(AssetDatabase.LoadMainAssetAtPath(buildAsset.Path));
                assetNames.Add(buildAsset.Path);
            }

            BuildPipeline.PushAssetDependencies();
            BuildAssetBundle(objects.ToArray(), assetNames.ToArray(), assetbundlePath);
            BuildPipeline.PopAssetDependencies();

            BuildPipeline.PopAssetDependencies();

            //配置asset的Assetbundle路径
            foreach (var buildAsset in readyToBuildAssets)
            {
                buildAsset.AssetBundlePath = tempPath + ".pkg";
            }

            HasBuildAssetBundleMap.Add(assetbundlePath);
            return(true);
        }
        return(false);
    }
Пример #2
0
    private static bool BuildSingleFileToAssetBundle(string exportPath, AssetInfo asset)
    {
        bool buildSingleFile = false;

        foreach (var path in BuildSingleFilePaths)
        {
            if (asset.Path.StartsWith(path))
            {
                buildSingleFile = true;
                break;
            }
        }

        if (buildSingleFile)
        {
            //判断是不是已经生成正常的AssetBundle
            var tempPath        = EditorFileUtility.GetPathWithoutExt(asset.Path);
            var assetbundlePath = exportPath + "/" + tempPath + ".pkg";
            if (HasBuildAssetBundleMap.Contains(assetbundlePath))
            {
                return(false);
            }
            EditorFileUtility.CreateParentDirecotry(assetbundlePath);

            //收集asset的所有依赖
            List <string> allChildDependences = new List <string>();
            foreach (var dependence in asset.ChildDependences)
            {
                if (asset.Path == dependence)
                {
                    continue;
                }
                allChildDependences.Add(dependence);
            }

            //删除临时目录的AssetBundle
            EditorFileUtility.DeleteDirectory(exportPath + "/temp");
            HasBuildAssetBundleMapTemp.Clear();

            BuildPipeline.PushAssetDependencies();

            foreach (var dependence in allChildDependences)
            {
                if (BuildAllFileToAssetBundleTemp(allChildDependences, exportPath, dependence))
                {
                    continue;
                }

                if (BuildSingleFileToAssetBundleTemp(exportPath, dependence))
                {
                    continue;
                }

                BuildCurrentDirectoryToAssetBundleTemp(allChildDependences, exportPath, dependence);
            }

            List <Object> objects    = new List <Object>();
            List <string> assetNames = new List <string>();

            objects.Add(AssetDatabase.LoadMainAssetAtPath(asset.Path));
            assetNames.Add(asset.Path);

            BuildPipeline.PushAssetDependencies();
            BuildAssetBundle(objects.ToArray(), assetNames.ToArray(), assetbundlePath);
            BuildPipeline.PopAssetDependencies();

            BuildPipeline.PopAssetDependencies();

            //配置asset的Assetbundle路径
            asset.AssetBundlePath = tempPath + ".pkg";
            HasBuildAssetBundleMap.Add(assetbundlePath);
            return(true);
        }

        return(false);
    }
Пример #3
0
    public static void LoadAllAssets()
    {
        GameAssets.Clear();
        AssetWithoutExtMap.Clear();
        HasBuildAssetBundleMap.Clear();

        var filePaths = EditorFileUtility.FilterDirectoryIgnoreExt(Application.dataPath + "/Resources", IgnoreFileExtensionList);

        var rootPath = Path.GetDirectoryName(Application.dataPath);

        rootPath = rootPath.Replace("\\", "/") + "/";

        if (filePaths != null)
        {
            Queue <string> assetQueue = new Queue <string>();
            foreach (var filePath in filePaths)
            {
                var path = filePath.Replace("\\", "/");
                path = path.Replace(rootPath, "");

                bool canAdd = true;
                foreach (var ignoreFile in IgnoreFileList)
                {
                    if (path.StartsWith(ignoreFile))
                    {
                        canAdd = false;
                    }
                }

                if (canAdd)
                {
                    assetQueue.Enqueue(path);
                }
            }

            while (assetQueue.Count > 0)
            {
                var path = assetQueue.Dequeue();
                if (GameAssets.ContainsKey(path))
                {
                    continue;
                }

                GetAssetDependences(assetQueue, path);
            }

            ReadBuildConfig();
            ClearExportDir();
            BuildAssetBundles();
            ExportAssetDependences();
            PackScripts();

            string assetName = "";
            try
            {
                foreach (var gameAsset in GameAssets)
                {
                    assetName = gameAsset.Key;
                    CheckAssetsDenpendences(assetName);
                }
            }
            catch (Exception e)
            {
                if (e.ToString().StartsWith("System.StackOverflowException"))
                {
                    Debug.LogError("检查资源依赖关系出现栈溢出问题,可能是资源存在环形依赖问题,资源名称是:" + assetName + ",请修复问题并重新打包:" + e);
                }
                else
                {
                    Debug.LogError("检查资源依赖关系出现错误,请修复问题并重新打包:" + e);
                }
            }
        }

        var exportRootPath = Path.GetDirectoryName(Application.dataPath);

        EditorFileUtility.DeleteDirectory(exportRootPath + ExportDir + "/Temp");
    }
Пример #4
0
    private static void BuildCurrentDirectoryToAssetBundle(string exportPath, AssetInfo asset)
    {
        //判断是不是已经生成正常的AssetBundle
        var tempPath        = EditorFileUtility.GetPath(asset.Path);
        var assetBundleName = Path.GetFileName(Path.GetDirectoryName(asset.Path));
        var assetBundlePath = exportPath + "/" + tempPath + "/" + assetBundleName + ".pkg";

        if (HasBuildAssetBundleMap.Contains(assetBundlePath))
        {
            return;
        }
        EditorFileUtility.CreateParentDirecotry(assetBundlePath);

        //收集和自己同一个目录下的资源(不包括子目录)
        var currentDir = Path.GetDirectoryName(asset.Path);
        List <AssetInfo> readyToBuildAssets = new List <AssetInfo>();

        foreach (var gameAsset in GameAssets)
        {
            if (Path.GetDirectoryName(gameAsset.Value.Path) == currentDir)
            {
                readyToBuildAssets.Add(gameAsset.Value);
            }
        }

        //收集asset的所有依赖,如果依赖是同一个assetBundle下的跳过
        List <string> allChildDependences = new List <string>();

        foreach (var gameAsset in readyToBuildAssets)
        {
            foreach (var dependence in gameAsset.ChildDependences)
            {
                if (Path.GetDirectoryName(dependence) != currentDir)
                {
                    allChildDependences.Add(dependence);
                }
            }
        }

        //删除临时目录的AssetBundle
        EditorFileUtility.DeleteDirectory(exportPath + "/temp");
        HasBuildAssetBundleMapTemp.Clear();

        BuildPipeline.PushAssetDependencies();

        foreach (var dependence in allChildDependences)
        {
            if (BuildAllFileToAssetBundleTemp(allChildDependences, exportPath, dependence))
            {
                continue;
            }

            if (BuildSingleFileToAssetBundleTemp(exportPath, dependence))
            {
                continue;
            }

            BuildCurrentDirectoryToAssetBundleTemp(allChildDependences, exportPath, dependence);
        }

        List <Object> objects    = new List <Object>();
        List <string> assetNames = new List <string>();

        foreach (var buildAsset in readyToBuildAssets)
        {
            objects.Add(AssetDatabase.LoadMainAssetAtPath(buildAsset.Path));
            assetNames.Add(buildAsset.Path);
        }

        BuildPipeline.PushAssetDependencies();
        BuildAssetBundle(objects.ToArray(), assetNames.ToArray(), assetBundlePath);
        BuildPipeline.PopAssetDependencies();

        BuildPipeline.PopAssetDependencies();

        //配置asset的Assetbundle路径
        foreach (var buildAsset in readyToBuildAssets)
        {
            buildAsset.AssetBundlePath = tempPath + "/" + assetBundleName + ".pkg";
        }
        HasBuildAssetBundleMap.Add(assetBundlePath);
    }