public static void Export(string baseDir, string exportPath)
        {
            {
                var relativePath = ExportSetting.instance.GetExportPath(exportPath);
                var filePath     = PathHelper.CheckFileName(System.IO.Path.Combine(baseDir, relativePath));
                var fileDir      = PathHelper.GetFileDirectory(filePath);
                if (!System.IO.Directory.Exists(fileDir))
                {
                    System.IO.Directory.CreateDirectory(fileDir);
                }
                var gltfFile = File.CreateText(filePath);
                SerializeObject.currentData.Serialize(gltfFile);
                gltfFile.Close();
                MyLog.Log("---导出文件:" + relativePath);
            }

            {
                foreach (var asset in SerializeObject.assetsData.Values)
                {
                    var relativePath = ExportSetting.instance.GetExportPath(asset.uri);
                    var filePath     = PathHelper.CheckFileName(System.IO.Path.Combine(baseDir, relativePath));
                    var fileDir      = PathHelper.GetFileDirectory(filePath);
                    if (!System.IO.Directory.Exists(fileDir))
                    {
                        System.IO.Directory.CreateDirectory(fileDir);
                    }
                    System.IO.File.WriteAllBytes(filePath, asset.buffer);
                }
            }
        }
Пример #2
0
        /**
         * 计算相对路径
         * Assets/res/elong/1525_firedragon02_d.png
         * Assets/res/elong/e/1525_firedragon02_d.imgdesc.json
         */
        public static string GetRelativePath(string targetPath, string sourcePath)
        {
            string relPath = "";

            targetPath = targetPath.Replace("\\", "/");
            sourcePath = sourcePath.Replace("\\", "/");
            string[] targetPathArr = targetPath.Split(new char[] { '/' });
            string[] sourcePathArr = sourcePath.Split(new char[] { '/' });
            int      targetPathLen = targetPathArr.Length;
            int      sourcePathLen = sourcePathArr.Length;
            int      i             = 0;

            while (targetPathArr[i] == sourcePathArr[i] && i < targetPathLen && i < sourcePathLen)
            {
                i++;
            }
            for (int j = 0; j < sourcePathLen - i - 1; j++)
            {
                relPath += "../";
            }
            relPath = relPath + string.Join("/", targetPathArr.Skip(i).ToArray());
            relPath = PathHelper.CheckFileName(relPath);

            return(relPath);
        }
        public static void SetAnimation(this MyJson_Object jsonNode, GameObject obj, UnityEngine.AnimationClip[] animationClips)
        {
            var exportAnimations = new MyJson_Array();

            jsonNode["_animations"] = exportAnimations;

            foreach (var animationClip in animationClips)
            {
                var gltfHash = animationClip.GetInstanceID();
                var url      = UnityEditor.AssetDatabase.GetAssetPath(animationClip);
                url = url.Substring(0, url.LastIndexOf(".")) + "_" + animationClip.name + ".ani.bin";
                url = PathHelper.CheckFileName(url);
                //
                var assetIndex = ResourceManager.instance.AddAssetUrl(url);
                if (!ResourceManager.instance.HaveCache(gltfHash))
                {
                    var glTFWriter = new AnimationXWriter(obj.transform, animationClip);
                    ResourceManager.instance.AddFileBuffer(url, glTFWriter.WriteGLTF());
                    //
                    ResourceManager.instance.SaveCache(gltfHash, url);
                }
                var aniItem = new MyJson_Tree();
                aniItem.SetInt("asset", assetIndex);
                exportAnimations.Add(aniItem);
            }
        }
        public string SaveMaterial(Material mat, bool isParticle = false, bool isAnimation = false)
        {
            int id = mat.GetInstanceID();

            if (this.HaveCache(id))
            {
                return(this.GetCache(id));
            }

            string tempMatPath = UnityEditor.AssetDatabase.GetAssetPath(mat);

            if (tempMatPath == "Resources/unity_builtin_extra")
            {
                tempMatPath = "Library/" + mat.name + ".mat";
            }
            if (!tempMatPath.Contains(".mat"))
            {
                tempMatPath += "." + mat.name + ".mat";//.obj文件此时应该导出不同的材质文件,GetAssetPath获取的确实同一个
            }
            var name = PathHelper.CheckFileName(tempMatPath + ".json");

            var gltf = new MaterialWriter(mat, isParticle, isAnimation);

            byte[] bs = gltf.WriteGLTF();
            this.AddFileBuffer(name, bs);
            this.SaveCache(id, name);
            return(name);
        }
        public string SaveMesh(Transform target, Mesh mesh)
        {
            int id = mesh.GetInstanceID();

            if (this.HaveCache(id))
            {
                return(this.GetCache(id));
            }

            var gltf = new MeshWriter(target);

            byte[] bs  = gltf.WriteGLTF();
            var    url = UnityEditor.AssetDatabase.GetAssetPath(mesh);
            //obj
            var extendName = "";
            var extend     = url.Substring(url.LastIndexOf(".") + 1);

            if (extend == "obj")
            {
                extendName = url.Substring(url.LastIndexOf("/") + 1);
                extendName = extendName.Substring(0, extendName.LastIndexOf(".")) + "_";
            }
            url = url.Substring(0, url.LastIndexOf("/") + 1);

            var name = PathHelper.CheckFileName(url + extendName + mesh.name + ".mesh.bin");

            this.AddFileBuffer(name, bs);
            this.SaveCache(id, name);
            return(name);
        }
 public void ExportFiles(string sceneOrPrefabPath, string exportPath = "")
 {
     //得到.prefab.json和.scene.json的数据
     System.Text.StringBuilder sb = new System.Text.StringBuilder();
     this.ConvertJsonToString(sb);
     //
     byte[] bs = System.Text.Encoding.UTF8.GetBytes(sb.ToString());
     this.AddFileBuffer(sceneOrPrefabPath, bs);
     //
     if (!System.IO.Directory.Exists(exportPath))
     {
         System.IO.Directory.CreateDirectory(exportPath);
     }
     foreach (var fileBuffer in ResourceManager.instance.fileBuffers)
     {
         if (string.IsNullOrEmpty(fileBuffer.Key))
         {
             continue;
         }
         //写入文件
         var filePath = PathHelper.CheckFileName(System.IO.Path.Combine(exportPath, fileBuffer.Key));
         MyLog.Log("---导出文件:" + fileBuffer.Key);
         //创建路径
         var fileDirectory = filePath.Substring(0, filePath.LastIndexOf("/") + 1);
         if (!System.IO.Directory.Exists(fileDirectory))
         {
             System.IO.Directory.CreateDirectory(fileDirectory);
         }
         System.IO.File.WriteAllBytes(filePath, fileBuffer.Value);
     }
 }
Пример #7
0
        public static string GetAnimationClipPath(UnityEngine.AnimationClip clip)
        {
            var path = UnityEditor.AssetDatabase.GetAssetPath(clip);

            path = path.Substring(0, path.LastIndexOf(".")) + "_" + clip.name + ".ani.bin";
            path = PathHelper.CheckFileName(path);
            return(path);
        }
Пример #8
0
        public static string GetTextureDescPath(UnityEngine.Texture texture)
        {
            //TODO
            var path = GetTexturePath(texture);

            // //相对路径
            path = path.Substring(0, path.LastIndexOf("/") + 1) + texture.name + ".image.json";
            path = PathHelper.CheckFileName(path);
            return(path);
        }
Пример #9
0
        public static string GetTextureDescPath(Texture2DArrayData tex)
        {
            //Texture2DArray 以第一个图片为目录,  以材质名称为名称
            var path = GetTexturePath(tex.textures[0]);

            // //相对路径
            path = path.Substring(0, path.LastIndexOf("/") + 1) + tex.materialName + ".image.json";
            path = PathHelper.CheckFileName(path);
            return(path);
        }
        /**
         * 保存纹理图片相关信息
         */
        public string SaveTextureFormat(Texture2D tex, string texPath, string matPath, bool closemipmap = false, string ext = "png", bool normal = false)
        {
            string name = PathHelper.CheckFileName(tex.name + ".image.json");

            MyJson_Tree textureItem = new MyJson_Tree();
            var         fileName    = texPath.Substring(0, texPath.LastIndexOf(".") + 1) + ext;

            textureItem.SetString("name", PathHelper.CheckFileName(texPath));
            textureItem.SetEnum("filterMode", tex.filterMode, true);
            textureItem.SetEnum("wrap", tex.wrapMode, true);
            textureItem.SetBool("mipmap", !closemipmap && tex.mipmapCount > 1);

            if (tex.anisoLevel > 1)
            {
                textureItem.SetNumber("anisotropy", tex.anisoLevel);
            }

            if (tex.format == TextureFormat.Alpha8)
            {
                textureItem.SetString("format", "Gray");
            }
            else if (ext == "jpg" ||
                     tex.format == TextureFormat.RGB24 ||
                     tex.format == TextureFormat.PVRTC_RGB2 ||
                     tex.format == TextureFormat.PVRTC_RGB4 ||
                     tex.format == TextureFormat.RGB565 ||
                     tex.format == TextureFormat.ETC_RGB4 ||
                     tex.format == TextureFormat.ATC_RGB4 ||
                     tex.format == TextureFormat.ETC2_RGB ||
                     tex.format == TextureFormat.ASTC_RGB_4x4 ||
                     tex.format == TextureFormat.ASTC_RGB_5x5 ||
                     tex.format == TextureFormat.ASTC_RGB_6x6 ||
                     tex.format == TextureFormat.ASTC_RGB_8x8 ||
                     tex.format == TextureFormat.ASTC_RGB_10x10 ||
                     tex.format == TextureFormat.ASTC_RGB_12x12
                     )
            {
                textureItem.SetString("format", "RGB");
            }

            textureItem.SetInt("version", 2);

            //得到.imgdesc.json数据,并保存到bufs中
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            textureItem.CovertToStringWithFormat(sb, 4);
            byte[] bs = System.Text.Encoding.UTF8.GetBytes(sb.ToString());
            //相对路径
            var imgdescPath = texPath.Substring(0, texPath.LastIndexOf("/") + 1) + name;

            this.AddFileBuffer(imgdescPath, bs);

            return(imgdescPath);
        }
        /**
         * 保存纹理图片
         */
        public string SaveTextureEditor(Texture2D tex, string matPath)
        {
            string localp = System.IO.Path.GetDirectoryName(Application.dataPath);
            string path   = AssetDatabase.GetAssetPath(tex);

            TextureImporter importer = (TextureImporter)TextureImporter.GetAtPath(path);
            bool            isNormal = importer && importer.textureType == TextureImporterType.NormalMap;
            string          filename = localp + "/" + path;
            int             i        = filename.LastIndexOf(".");
            string          ext      = "png";

            if (i >= 0)
            {
                ext = filename.Substring(i + 1);
            }
            byte[] bs;
            string name = tex.name + "." + ext;

            name = PathHelper.CheckFileName(name);
            string rename;

            if (path == "Resources/unity_builtin_extra" || string.IsNullOrEmpty(path))
            {
                path = "Library/" + tex.name + "." + ext;
            }
            {
                var isSupported = true;
                if (ext != "png" && ext != "jpg" && ext != "jpeg")
                {
                    path = path.Substring(0, path.LastIndexOf(".") + 1) + "png";
                    //非png、jpg都导出为png
                    ext         = "png";
                    isSupported = false;
                }

                //只有jpg、png可以原始图片导出,其他类型不支持
                if (ExportToolsSetting.instance.exportOriginalImage && isSupported && System.IO.File.Exists(filename))
                {
                    MyLog.Log("原始图片:" + filename);
                    bs = System.IO.File.ReadAllBytes(filename);
                }
                else
                {
                    bs = ExportImageTools.instance.EncodeToPNG(tex, ext);
                }

                path = PathHelper.CheckFileName(path);
                this.AddFileBuffer(path, bs);
                rename = SaveTextureFormat(tex, path, matPath, false, ext, isNormal);
            }

            return(rename);
        }
Пример #12
0
        public static string GetMaterialPath(UnityEngine.Material material)
        {
            var    mat  = material;
            string path = UnityEditor.AssetDatabase.GetAssetPath(mat);

            if (path == "Resources/unity_builtin_extra")
            {
                path = "Library/" + mat.name + ".mat";
            }
            if (!path.Contains(".mat"))
            {
                path += "." + mat.name + ".mat";//.obj文件此时应该导出不同的材质文件,GetAssetPath获取的确实同一个
            }
            path = PathHelper.CheckFileName(path + ".json");
            return(path);
        }
Пример #13
0
        public static string GetMeshPath(UnityEngine.Mesh mesh)
        {
            var path = UnityEditor.AssetDatabase.GetAssetPath(mesh);
            //obj
            var extendName = "";
            var extend     = path.Substring(path.LastIndexOf(".") + 1);

            if (extend == "obj")
            {
                extendName = path.Substring(path.LastIndexOf("/") + 1);
                extendName = extendName.Substring(0, extendName.LastIndexOf(".")) + "_";
            }
            path = path.Substring(0, path.LastIndexOf("/") + 1);

            path = PathHelper.CheckFileName(path + extendName + mesh.name + ".mesh.bin");
            return(path);
        }
Пример #14
0
        public static string GetTexturePath(Texture tex)
        {
            var path = AssetDatabase.GetAssetPath(tex);
            var ext  = GetTextureExt(tex);

            if (path == "Resources/unity_builtin_extra" || string.IsNullOrEmpty(path))
            {
                path = "Library/" + tex.name + "." + ext;
            }

            if (ext != "png" && ext != "jpg" && ext != "jpeg")
            {
                //非png、jpg都导出为png
                path = path.Substring(0, path.LastIndexOf(".") + 1) + "png";
            }

            return(PathHelper.CheckFileName(path));
        }
Пример #15
0
        private static MyJson_Array AddLightmaps(string exportPath)
        {
            var lightmapsJson = new MyJson_Array();

            foreach (var lightmapData in LightmapSettings.lightmaps)
            {
                Texture2D lmTexture = lightmapData.lightmapColor;
                int       lmID      = lmTexture.GetInstanceID();
                if (!ResourceManager.instance.HaveCache(lmID))
                {
                    //格式转换
                    string suffix       = "png";
                    string relativePath = UnityEditor.AssetDatabase.GetAssetPath(lmTexture);
                    MyLog.Log("导出lightmap:" + relativePath);
                    string exrPath    = Path.Combine(Application.dataPath, relativePath.Replace("Assets/", ""));
                    string ralPngPath = PathHelper.CheckFileName(relativePath.Substring(0, relativePath.LastIndexOf('/') + 1) + lmTexture.name + "." + suffix);
                    string pngPath    = PathHelper.CheckFileName(Path.Combine(exportPath, ralPngPath));
                    // string ralPngPath = relativePath.Substring(0, relativePath.LastIndexOf('/') + 1) + lmTexture.name + "." + suffix;
                    // string pngPath = Path.Combine(exportPath, ralPngPath);

                    exrPath = exrPath.Replace("\\", "/");
                    pngPath = pngPath.Replace("\\", "/");
                    var bs = ExportImageTools.instance.EncodeToPNG(lmTexture);
                    ResourceManager.instance.AddFileBuffer(ralPngPath, bs);
                    // ExportImageTools.instance.AddExr(exrPath, pngPath);
                    //添加到 compList 和 assetsList
                    ResourceManager.instance.SaveTextureFormat(lmTexture, ralPngPath, ralPngPath + ".image", false);
                    string name        = lmTexture.name + ".image.json";
                    var    imgdescPath = PathHelper.CheckFileName(ralPngPath.Substring(0, ralPngPath.LastIndexOf("/") + 1) + name);

                    var assetIndex = ResourceManager.instance.AddAssetUrl(imgdescPath);

                    var assetItem = new MyJson_Tree();
                    assetItem.SetInt("asset", assetIndex);
                    lightmapsJson.Add(assetItem);
                }
            }
            // ExportImageTools.instance.ExrToPng();

            return(lightmapsJson);
        }