Exemple #1
0
    public static string BuildDepMaterial(Material mat, float scaleTexture = 1f)
    {
        CDepCollectInfo buildResult = KDepCollectInfoCaching.GetCache(mat);

        if (buildResult != null)
        {
            return(buildResult.Path);
        }

        KSerializeMaterial sMat = __DoExportMaterial(mat, scaleTexture);

        if (sMat != null)
        {
            string path = AssetDatabase.GetAssetPath(mat);

            bool needBuild = KAssetVersionControl.TryCheckNeedBuildWithMeta(path);
            if (needBuild)
            {
                KAssetVersionControl.TryMarkBuildVersion(path);
            }

            path        = KDependencyBuild.__GetPrefabBuildPath(path);
            buildResult = KDependencyBuild.__DoBuildScriptableObject("Material/Material_" + path, sMat, needBuild);

            KDepCollectInfoCaching.SetCache(mat, buildResult);

            return(buildResult.Path);
        }

        // 可能没路径的材质,直接忽略
        return("");
    }
Exemple #2
0
    // Prefab ,  build
    public static string BuildSprite(Sprite sprite)
    {
        if (sprite.packed)
        {
            Logger.LogWarning("Sprite: {0} is packing!!!", sprite.name);
        }

        string assetPath = AssetDatabase.GetAssetPath(sprite);
        bool   needBuild = KAssetVersionControl.TryCheckNeedBuildWithMeta(assetPath);

        if (needBuild)
        {
            KAssetVersionControl.TryMarkBuildVersion(assetPath);
        }

        string path = KDependencyBuild.__GetPrefabBuildPath(assetPath);

        if (string.IsNullOrEmpty(path))
        {
            Logger.LogWarning("[BuildSprite]不是文件的Texture, 估计是Material的原始Texture?");
        }
        var result = KDependencyBuild.DoBuildAssetBundle("Common/Sprite_" + path, sprite, needBuild);

        return(result.Path);
    }
Exemple #3
0
        /// <summary>
        /// 从版本控制系统中判断是否需要Build这个Asset
        /// </summary>
        /// <param name="assetPath"></param>
        /// <returns></returns>
        private static bool CheckNeedBuildAsset(string assetPath)
        {
            // 判断是否需要打包,根据要在依赖判断之后哦
            if (!KAssetVersionControl.TryCheckNeedBuildWithMeta(assetPath))
            {
                if (!InternalEditorUtility.inBatchMode)
                {
                    Debug.LogWarning("Same file, no Need To Build " + assetPath);
                }
                return(false);
            }

            // 检查是否需要打包的后缀类型
            var extType = GetAssetExtType(assetPath);

            if (Define.IgnoreDepType.Contains(extType))
            {
                if (!InternalEditorUtility.inBatchMode)
                {
                    Logger.LogWarning("Asset {0}, Type: {1}, no need build", assetPath, extType);
                }
                return(false);
            }

            return(true);
        }
Exemple #4
0
    /// <summary>
    /// 图片打包工具,直接打包,不用弄成Texture!, 需要借助TexturePacker
    /// </summary>
    /// <summary>
    /// 单独打包的纹理图片, 这种图片,不在Unity目录内,用bytes读取, 指定保存的目录
    /// </summary>
    /// <param name="saveCacheFolderName"></param>
    /// <param name="tex"></param>
    /// <returns></returns>
    public static string BuildIOTexture(string saveCacheFolderName, string imageSystemFullPath, float scale = 1)
    {
        var cleanPath = imageSystemFullPath.Replace("\\", "/");

        var fileName  = Path.GetFileNameWithoutExtension(cleanPath);
        var buildPath = string.Format("{0}/{1}_{0}{2}", saveCacheFolderName, fileName,
                                      AppEngine.GetConfig(KEngineDefaultConfigs.AssetBundleExt));
        var needBuild = KAssetVersionControl.TryCheckNeedBuildWithMeta(cleanPath);

        var texture = new Texture2D(1, 1);

        if (needBuild && !IsJustCollect)
        {
            var cacheDir = "Assets/" + KEngineDef.ResourcesBuildCacheDir + "/BuildIOTexture/" + saveCacheFolderName +
                           "/";
            var cacheFilePath = cacheDir + Path.GetFileName(cleanPath);

            //CFolderSyncTool.TexturePackerScaleImage(cleanPath, cacheFilePath, Math.Min(1, GameDef.PictureScale * scale));  // TODO: do scale

            texture = AssetDatabase.LoadAssetAtPath(cacheFilePath, typeof(Texture2D)) as Texture2D;
            if (texture == null)
            {
                Logger.LogError("[BuildIOTexture]TexturePacker scale failed... {0}", cleanPath);
                return(null);
            }

            //CTextureCompressor.CompressTextureAsset(cleanPath, texture);

            //CTextureCompressor.AutoCompressTexture2D(texture, EditorUserBuildSettings.activeBuildTarget);

            //if (!texture.LoadImage(File.ReadAllBytes(cleanPath)))
            //{
            //    Logger.LogError("无法LoadImage的Texture: {0}", cleanPath);
            //    return null;
            //}
            //texture.name = fileName;

            //// 多线程快速,图像缩放,双线性过滤插值
            //TextureScaler.Bilinear(texture, (int)(texture.width * GameDef.PictureScale),
            //    (int)(texture.height * GameDef.PictureScale));
            //GC.CollectMaterial();
        }

        // card/xxx_card.box
        var result = KDependencyBuild.DoBuildAssetBundle(buildPath, texture, needBuild);

        if (needBuild)
        {
            KAssetVersionControl.TryMarkBuildVersion(cleanPath);
        }

        return(result.Path);
    }
Exemple #5
0
    public static string BuildAudioClip(AudioClip audioClip)
    {
        string assetPath = AssetDatabase.GetAssetPath(audioClip);
        bool   needBuild = KAssetVersionControl.TryCheckNeedBuildWithMeta(assetPath);

        if (needBuild)
        {
            KAssetVersionControl.TryMarkBuildVersion(assetPath);
        }

        var result = KDependencyBuild.DoBuildAssetBundle("Audio/Audio_" + audioClip.name, audioClip, needBuild);

        return(result.Path);
    }
    // 针对当前打开的单个UI
    static void Custom_ExportCurrentUI(KBuild_NGUI uiBuilder, string uiScenepath, string uiName,
                                       GameObject objToBuild)
    {
        bool reBuildPanel = KAssetVersionControl.TryCheckNeedBuildWithMeta(uiScenepath);

        KDependencyBuild.BuildGameObject(objToBuild, KBuild_NGUI.GetBuildRelPath(uiName), reBuildPanel);

        if (reBuildPanel)
        {
            KAssetVersionControl.TryMarkBuildVersion(uiScenepath);
        }

        _FindLabelLocalization(uiBuilder);
    }
Exemple #7
0
        /// <summary>
        /// 如果是内置类型/ Builtin的,只要不存在就重打
        /// </summary>
        /// <param name="unityAssetType"></param>
        /// <param name="path"></param>
        /// <returns></returns>
        private static bool CheckNeedBuildAsset(UnityAssetType unityAssetType, string path)
        {
            if (unityAssetType == UnityAssetType.Object)
            {
                return(CheckNeedBuildAsset(path));
            }

            var needBuild = !KAssetVersionControl.TryCheckExistRecord(path);

            if (!needBuild && !InternalEditorUtility.inBatchMode)
            {
                Debug.LogWarning("Builtin resource handled, no Need To Build " + path);
            }
            return(needBuild);
        }
Exemple #8
0
    private static string _BuildShader(Shader shader)
    {
        Shader fileShader;
        string shaderAssetPath = AssetDatabase.GetAssetPath(shader);

        if (shaderAssetPath.Contains("unity_builtin_extra"))
        {
            shaderAssetPath = "Assets/" + KEngineDef.ResourcesBuildCacheDir + "/BuiltinShader";

            fileShader = AssetDatabase.LoadAssetAtPath(shaderAssetPath, typeof(Shader)) as Shader;
            if (fileShader == null)
            {
                AssetDatabase.CreateAsset(shader, shaderAssetPath);
                AssetDatabase.ImportAsset(shaderAssetPath);
                fileShader = AssetDatabase.LoadAssetAtPath(shaderAssetPath, typeof(Shader)) as Shader;

                if (fileShader == null)
                {
                    Logger.LogError("Cannot Build Builtin Shader: {0}", shader.name);
                }
            }
        }
        else
        {
            fileShader = shader;
        }

        //var shaderFlag = string.Format("Shader:{0}:{1}", fileShader.name, shaderAssetPath);  // 构造一个标记

        bool needBuild = KAssetVersionControl.TryCheckFileBuild(shaderAssetPath);

        if (needBuild)
        {
            KAssetVersionControl.TryMarkBuildVersion(shaderAssetPath);
        }

        var cleanShaderName = GetShaderNameToBuild(fileShader);
        var result          = KDependencyBuild.DoBuildAssetBundle("Shader/Shader_" + cleanShaderName, fileShader, needBuild);

        return(result.Path);
    }
Exemple #9
0
        /// <summary>
        /// NGUI 的字体集
        /// </summary>
        public static string BuildUIFont(UIFont uiFont)
        {
            CDepCollectInfo result;

            if (KDepCollectInfoCaching.HasCache(uiFont))
            {
                result = KDepCollectInfoCaching.GetCache(uiFont);
                return(result.Path);
            }
            if (uiFont.atlas == null)
            {
                Logger.LogError("[BuildUIFont]uiFont Null Atlas: {0}, Scene: {1}", uiFont.name, EditorApplication.currentScene);
                return("");
            }
            string uiFontPrefabPath = AssetDatabase.GetAssetPath(uiFont.gameObject);
            bool   needBuild        = KAssetVersionControl.TryCheckNeedBuildWithMeta(uiFontPrefabPath);

            if (needBuild)
            {
                KAssetVersionControl.TryMarkBuildVersion(uiFontPrefabPath);
            }

            var copyUIFontObj = GameObject.Instantiate(uiFont.gameObject) as GameObject;
            var copyUIFont    = copyUIFontObj.GetComponent <UIFont>();

            var uiAtlas = BuildUIAtlas(copyUIFont.atlas); // 依赖的UI Atlas

            copyUIFont.atlas    = null;                   // 清空依赖
            copyUIFont.material = null;
            //CResourceDependencies.Create(copyUIFont, CResourceDependencyType.NGUI_UIFONT, uiAtlas);
            KAssetDep.Create <KUIFontDep>(copyUIFont, uiAtlas);

            result = KDependencyBuild.DoBuildAssetBundle("UIFont/UIFont_" + uiFont.name, copyUIFontObj, needBuild);

            GameObject.DestroyImmediate(copyUIFontObj);
            KDepCollectInfoCaching.SetCache(uiFont, result);
            return(result.Path);
        }
Exemple #10
0
    /// <summary>
    /// 对依赖的字体进行打包,返回打包结果路径
    /// </summary>
    /// <param name="font"></param>
    /// <returns></returns>
    public static string BuildFont(Font font)
    {
        string fontAssetPath = AssetDatabase.GetAssetPath(font);

        if (string.IsNullOrEmpty(fontAssetPath) || fontAssetPath == "Library/unity default resources")
        {
            Logger.LogError("[BuildFont]无法打包字体...{0}", font);
            return(null);
        }
        //fontAssetPath = __GetPrefabBuildPath(fontAssetPath).Replace("Atlas_", "");
        //string[] splitArr = fontAssetPath.Split('/');

        bool needBuild = KAssetVersionControl.TryCheckNeedBuildWithMeta(fontAssetPath);

        if (needBuild)
        {
            KAssetVersionControl.TryMarkBuildVersion(fontAssetPath);
        }

        var result = DoBuildAssetBundle("Font/Font_" + font.name, font, needBuild);

        return(result.Path);
    }
Exemple #11
0
        /// <summary>
        /// 图集 打包结果缓存起来加速
        /// </summary>
        /// <param name="atlas"></param>
        /// <returns></returns>
        public static string BuildUIAtlas(UIAtlas atlas)
        {
            CDepCollectInfo result;

            // 使用缓存,确保Atlas不会重复处理,浪费性能
            if (KDepCollectInfoCaching.HasCache(atlas))
            {
                result = KDepCollectInfoCaching.GetCache(atlas);
                return(result.Path);
            }
            var        scale       = 1f; // TODO: scale read
            GameObject atlasPrefab = PrefabUtility.FindPrefabRoot(atlas.gameObject) as GameObject;

            Logger.Assert(atlasPrefab);
            string path      = AssetDatabase.GetAssetPath(atlasPrefab); // prefab只用来获取路径,不打包不挖空
            bool   needBuild = KAssetVersionControl.TryCheckNeedBuildWithMeta(path);

            if (needBuild)
            {
                KAssetVersionControl.TryMarkBuildVersion(path);
            }

            Logger.Assert(path);

            path = KDependencyBuild.__GetPrefabBuildPath(path);

            GameObject copyAtlasObj = GameObject.Instantiate(atlasPrefab) as GameObject;

            UIAtlas copyAtlas = copyAtlasObj.GetComponent <UIAtlas>();

            if (BeforeBuildUIAtlasFilter != null)
            {
                BeforeBuildUIAtlasFilter(copyAtlas);
            }

            Material cacheMat = copyAtlas.spriteMaterial;
            string   matPath  = KDepBuild_Material.BuildDepMaterial(cacheMat, scale); // 缩放

            // 缩放
            copyAtlas.pixelSize = 1 / PictureScale;
            foreach (var spriteData in copyAtlas.spriteList)
            {
                spriteData.x            = Mathf.FloorToInt(spriteData.x * PictureScale);
                spriteData.y            = Mathf.FloorToInt(spriteData.y * PictureScale);
                spriteData.width        = Mathf.FloorToInt(spriteData.width * PictureScale);
                spriteData.height       = Mathf.FloorToInt(spriteData.height * PictureScale);
                spriteData.borderLeft   = Mathf.FloorToInt(spriteData.borderLeft * PictureScale);
                spriteData.borderRight  = Mathf.FloorToInt(spriteData.borderRight * PictureScale);
                spriteData.borderTop    = Mathf.FloorToInt(spriteData.borderTop * PictureScale);
                spriteData.borderBottom = Mathf.FloorToInt(spriteData.borderBottom * PictureScale);
                // padding 不变, ngui bug
                spriteData.paddingBottom = Mathf.FloorToInt(spriteData.paddingBottom * PictureScale);
                spriteData.paddingTop    = Mathf.FloorToInt(spriteData.paddingTop * PictureScale);
                spriteData.paddingLeft   = Mathf.FloorToInt(spriteData.paddingLeft * PictureScale);
                spriteData.paddingRight  = Mathf.FloorToInt(spriteData.paddingRight * PictureScale);
            }

            KAssetDep.Create <KUIAtlasDep>(copyAtlas, matPath);

            copyAtlas.spriteMaterial = null;                                                                  // 挖空atlas

            result = KDependencyBuild.DoBuildAssetBundle("UIAtlas/UIAtlas_" + path, copyAtlasObj, needBuild); // Build主对象, 被挖空Material了的

            if (AfterBuildUIAtlasFilter != null)
            {
                AfterBuildUIAtlasFilter(copyAtlas);
            }
            GameObject.DestroyImmediate(copyAtlasObj);

            KDepCollectInfoCaching.SetCache(atlas, result);
            return(result.Path);
        }
Exemple #12
0
        /// <summary>
        /// ResourceDep系统专用的打包AssetBundle函数
        /// </summary>
        /// <param name="asset"></param>
        /// <param name="path"></param>
        /// <param name="depFileRelativeBuildPath">依赖文件列表,相对的AssetBundle打包路径</param>
        /// <param name="buildTarget"></param>
        /// <param name="quality"></param>
        /// <returns></returns>
        private static BuildBundleResult BuildAssetBundle(Object asset, string path,
                                                          IList <string> depFileRelativeBuildPath, BuildTarget buildTarget, KResourceQuality quality)
        {
            //是否是Level / Scene
            var isScene = asset.ToString().Contains("SceneAsset");

            uint crc  = 0;
            var  time = DateTime.Now;
            // 最终完整路径
            var buildToFullPath = KBuildTools.MakeSureExportPath(path, buildTarget, quality) +
                                  AppEngine.GetConfig(KEngineDefaultConfigs.AssetBundleExt);
            var buildToRelativePath = AbsPath2RelativePath(buildToFullPath);//buildToFullPath.Replace(workdirPath, "").Substring(1); // 转换成相对路径,避免路径过程无法打包的问题

            var assetPath = AssetDatabase.GetAssetPath(asset);

            // 版本标记
            var unityAssetType = GetUnityAssetType(assetPath);

            if (unityAssetType == UnityAssetType.Builtin || unityAssetType == UnityAssetType.Memory)
            {
                var buildAssetPath = GetBuildAssetPath(asset);
                KAssetVersionControl.TryMarkRecord(buildAssetPath);
                BuildedPool.Add(buildAssetPath);
            }
            else
            {
                KAssetVersionControl.TryMarkBuildVersion(assetPath);
                BuildedPool.Add(assetPath);
            }

            bool result = false;

            if (isScene)
            {
                var resultStr = BuildPipeline.BuildStreamedSceneAssetBundle(new string[] { assetPath }, buildToRelativePath,
                                                                            buildTarget, out crc);
                result = String.IsNullOrEmpty(resultStr);
                if (!String.IsNullOrEmpty(resultStr))
                {
                    Debug.LogError(resultStr);
                }
            }
            else
            {
                result = BuildPipeline.BuildAssetBundle(asset, null, buildToRelativePath,
                                                        out crc,
                                                        BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.DeterministicAssetBundle |
                                                        BuildAssetBundleOptions.CompleteAssets,
                                                        buildTarget);
            }

            // 创建依赖记录文件
            string fullManifestPath = null;

            //if (depFileRelativeBuildPath != null && depFileRelativeBuildPath.Any())
            {
                //var manifestFileContent = string.Join("\n", depFileRelativeBuildPath.KToArray());
                if (depFileRelativeBuildPath == null)
                {
                    depFileRelativeBuildPath = new List <string>();
                }

                var manifestPath = path + ".manifest";
                fullManifestPath = KBuildTools.MakeSureExportPath(manifestPath, buildTarget, quality) +
                                   AppEngine.GetConfig(KEngineDefaultConfigs.AssetBundleExt);
                var relativeManifestPath = AbsPath2RelativePath(fullManifestPath); // 转成相对路径

                var utf8NoBom = new UTF8Encoding(false);
                //try
                //{
                File.WriteAllLines(relativeManifestPath, depFileRelativeBuildPath.KToArray(), utf8NoBom);
                //}
                //catch (Exception e)
                //{
                //    // 会出现DirectoryNotFound,但是目录明明就存在! 先Catch
                //    Logger.LogError("Exception: {0}", e.Message);
                //    var dirPath = Path.GetDirectoryName(fullManifestPath);
                //    if (Directory.Exists(dirPath))
                //        Logger.LogError("Directory Exists: {0}", dirPath);
                //    else
                //    {
                //        Logger.LogError("Directory not exists: {0}", dirPath);
                //    }
                //}
            }

            if (result)
            {
                var abInfo = new FileInfo(buildToFullPath);
                Logger.Log("Build AssetBundle: {0} / CRC: {1} / Time: {2:F4}s / Size: {3:F3}KB / FullPath: {4} / RelPath: {5}", path, crc, (DateTime.Now - time).TotalSeconds,
                           abInfo.Length / 1024d, buildToFullPath, buildToRelativePath);
            }
            else
            {
                Logger.LogError("生成文件失败: {0}, crc: {1} 耗时: {2:F5}, 完整路径: {3}", path, crc,
                                (DateTime.Now - time).TotalSeconds, buildToFullPath);
            }
            return(new BuildBundleResult
            {
                Crc = crc,
                FullPath = buildToFullPath,
                RelativePath = path,
                IsSuccess = result,
                ManifestFullPath = fullManifestPath,
            });
        }
Exemple #13
0
    //static HashSet<string> _depTextureScaleList = new HashSet<string>();  // 进行过Scale的图片
    public static string BuildDepTexture(Texture tex, float scale = 1f)
    {
        Debuger.Assert(tex);
        CDepCollectInfo result = KDepCollectInfoCaching.GetCache(tex);

        if (result != null)
        {
            return(result.Path);
        }

        string assetPath = AssetDatabase.GetAssetPath(tex);
        bool   needBuild = KAssetVersionControl.TryCheckNeedBuildWithMeta(assetPath);

        if (needBuild)
        {
            KAssetVersionControl.TryMarkBuildVersion(assetPath);
        }

        Texture newTex;

        if (tex is Texture2D)
        {
            var tex2d = (Texture2D)tex;

            if (needBuild &&
                !scale.Equals(1f)) // 需要进行缩放,才进来拷贝
            {
                var cacheDir      = "Assets/" + KEngineDef.ResourcesBuildCacheDir + "/BuildDepTexture/";
                var cacheFilePath = cacheDir + Path.GetFileName(assetPath);

                //var needScale = !BuildedCache.ContainsKey("BuildDepTexture:" + assetPath);
                //if (needScale && !IsJustCollect)
                //{
                //    CFolderSyncTool.TexturePackerScaleImage(assetPath, cacheFilePath, scale);  // do scale
                //    var actionName = "BuildDepTexture:" + assetPath;
                //    //BuildedCache[actionName] = true;  // 下次就别scale了!蛋痛
                //    AddCache(actionName);
                //}

                newTex = AssetDatabase.LoadAssetAtPath(cacheFilePath, typeof(Texture2D)) as Texture2D;
                if (newTex == null)
                {
                    Logger.LogError("TexturePacker scale failed... {0}", assetPath);
                    newTex = tex2d;
                }

                SyncTextureImportSetting(tex2d, newTex as Texture2D);

                // TODO: mark to write
                //var texPath = AssetDatabase.GetAssetPath(tex2d);

                //var newTex2D = new Texture2D(tex2d.width, tex2d.height);
                //if (!string.IsNullOrEmpty(texPath)) // Assets内的纹理
                //{
                //    var bytes = File.ReadAllBytes(texPath);
                //    newTex2D.LoadImage(bytes);
                //}
                //else
                //{
                //    var bytes = tex2d.EncodeToPNG();
                //    newTex2D.LoadImage(bytes);
                //}

                //newTex2D.Apply();
                //GC.CollectMaterial();
                //// 进行缩放
                //TextureScaler.Bilinear(newTex2D, (int) (tex.width*scale), (int) (tex.height*scale));
                //GC.CollectMaterial();

                //newTex = newTex2D;
            }
            else
            {
                newTex = tex2d;
            }
        }
        else
        {
            newTex = tex;
            if (!scale.Equals(1f))
            {
                Logger.LogWarning("[BuildDepTexture]非Texture2D: {0}, 无法进行Scale缩放....", tex);
            }
        }
        //if (!IsJustCollect)
        //    CTextureCompressor.CompressTextureAsset(assetPath, newTex as Texture2D);

        string path = __GetPrefabBuildPath(assetPath);

        if (string.IsNullOrEmpty(path))
        {
            Logger.LogWarning("[BuildTexture]不是文件的Texture, 估计是Material的原始Texture?");
        }
        result = DoBuildAssetBundle("Texture/Texture_" + path, newTex, needBuild);

        KDepCollectInfoCaching.SetCache(tex, result);
        GC.Collect(0);
        return(result.Path);
    }