private static void ReplaceAtlasMesh(MA_TextureAtlasserProAtlas atlas, ModelExportSettings modelExportSettings, string savePath = EXPORT_ASSET_PATH) { if (atlas == null || atlas.textureQuads == null) { return; } var quads = atlas.textureQuads; for (var index = 0; index < quads.Count; index++) { var quad = quads[index]; if (quad.meshes == null) { continue; } var meshes = quad.meshes; for (var meshIndex = 0; meshIndex < quad.meshes.Count; meshIndex++) { if (meshes[meshIndex] == null) { continue; } MA_MeshUtils.MA_UVReMap(meshes[meshIndex], atlas.textureAtlasSize, quad.guiRect, modelExportSettings.uvChannel, modelExportSettings.uvFlipY, modelExportSettings.uvWrap); EditorUtility.SetDirty(meshes[meshIndex]); } } AssetDatabase.SaveAssets(); }
public static string[] ExportAtlasModels(MA_TextureAtlasserProAtlas atlas, ModelExportSettings modelExportSettings, string materialPath = null, string savePath = EXPORT_ASSET_PATH) { switch (modelExportSettings.modelFormat) { case ModelFormat.None: break; case ModelFormat.UnityMeshPrefab: return(ExportAtlasUnityMeshPrefab(atlas, modelExportSettings, materialPath: materialPath, savePath: savePath)); //case ModelFormat.Obj: // return ExportAtlasObj(atlas, modelExportSettings, savePath: savePath); default: break; } return(null); }
private static string[] ExportAtlasObj(MA_TextureAtlasserProAtlas atlas, ModelExportSettings modelExportSettings, string savePath = EXPORT_ASSET_PATH) { if (atlas == null || atlas.textureQuads == null) { return(null); } List <string> assetPaths = new List <string>(); foreach (MA_TextureAtlasserProQuad quad in atlas.textureQuads) { //Export Mesh if (quad.meshes != null) { for (int m = 0; m < quad.meshes.Count; m++) { if (quad.meshes[m] != null) { //Create new mesh Mesh newMesh = new Mesh(); //Duplicate it from the current one newMesh = MA_MeshUtils.MA_DuplicateMesh(quad.meshes[m]); //Remap UV's newMesh = MA_MeshUtils.MA_UVReMap(newMesh, atlas.textureAtlasSize, quad.guiRect, modelExportSettings.uvChannel, modelExportSettings.uvFlipY, modelExportSettings.uvWrap); //Save it string meshName = string.IsNullOrEmpty(quad.name) ? "" : quad.name + "-"; meshName += quad.meshes[m].name; int n = m + 1; meshName += "_" + n.ToString("#000"); string asset = MA_MeshUtils.MeshToFile(newMesh, meshName, savePath); assetPaths.Add(asset); } } } } return(assetPaths.ToArray()); }
private static string[] ExportAtlasUnityMeshPrefab(MA_TextureAtlasserProAtlas atlas, ModelExportSettings modelExportSettings, string materialPath = null, string savePath = EXPORT_ASSET_PATH) { if (atlas == null || atlas.textureQuads == null) { return(null); } List <string> assetPaths = new List <string>(); //Directories. string savePathPrefab = savePath + atlas.name + "/"; string savePathMeshes = savePathPrefab + "Meshes/"; CreateFolder(savePathPrefab); CreateFolder(savePathMeshes); foreach (MA_TextureAtlasserProQuad quad in atlas.textureQuads) { foreach (MA_ModelGroup mg in quad.modelGroups) { //Validate name. if (string.IsNullOrEmpty(mg.name) || string.IsNullOrWhiteSpace(mg.name)) { mg.name = MA_StringUtils.RandomAlphabetString(6); Debug.LogWarning("No valid model name assigned!"); } //Create new prefab asset. string newPrefabPath = MA_PrefabUtils.CreatePrefab(mg.name, savePathPrefab); GameObject newPrefab = AssetDatabase.LoadAssetAtPath <GameObject>(newPrefabPath); foreach (Mesh m in mg.meshes) { if (m != null) { //Validate name. if (string.IsNullOrEmpty(m.name) || string.IsNullOrWhiteSpace(m.name)) { m.name = MA_StringUtils.RandomAlphabetString(6); Debug.LogWarning("No valid mesh name assigned!"); } //Create new mesh. //Duplicate it from the current one. Mesh newMesh = MA_MeshUtils.MA_DuplicateMesh(m); //Remap UV's. newMesh = MA_MeshUtils.MA_UVReMap(newMesh, atlas.textureAtlasSize, quad.guiRect, modelExportSettings.uvChannel, modelExportSettings.uvFlipY, modelExportSettings.uvWrap); //Set name. newMesh.name = string.Format("{0}_{1}", mg.name, m.name); //Save mesh. string savedMeshPath = MA_MeshUtils.MA_SaveMeshAsset(newMesh, savePathMeshes); //Load mesh. Mesh savedMesh = AssetDatabase.LoadAssetAtPath <Mesh>(savedMeshPath); //Load material. Material savedMaterial = AssetDatabase.LoadAssetAtPath <Material>(materialPath); //Create gameObject. GameObject newGameObject = new GameObject(m.name); //Add mesh filter. MeshFilter mf = newGameObject.AddComponent <MeshFilter>(); mf.mesh = savedMesh; //Add mesh renderer. MeshRenderer mr = newGameObject.AddComponent <MeshRenderer>(); mr.material = savedMaterial; //Add to parent gameObject (prefab). MA_PrefabUtils.AddChild(newPrefab, newGameObject); Object.DestroyImmediate(newGameObject); } } assetPaths.Add(newPrefabPath); } } return(assetPaths.ToArray()); }