public static MA_TextureAtlasserProQuad CreateTextureQuad(MA_TextureAtlasserProAtlas atlas, string name, Rect rect, bool focus = true) { if (atlas != null) { //Create new list if we haven't one already if (atlas.textureQuads == null) { atlas.textureQuads = new List <MA_TextureAtlasserProQuad>(); } //Create new quad MA_TextureAtlasserProQuad _quad = ScriptableObject.CreateInstance <MA_TextureAtlasserProQuad>(); //Add quad to asset if (_quad != null) { //Set quad settings _quad.name = name; _quad.rect = rect; SetTextureGroups(atlas, _quad); atlas.textureQuads.Add(_quad); AssetDatabase.AddObjectToAsset(_quad, atlas); AssetDatabase.SaveAssets(); AssetDatabase.Refresh(); if (focus) { atlas.selectedTextureQuad = atlas.textureQuads[atlas.textureQuads.Count - 1]; } return(_quad); } else { Debug.LogError("CreateTextureQuad Failed: _TextureQuad"); } } else { Debug.LogError("CreateTextureQuad Failed: textureAtlas"); } return(null); }
public static void MA_CheckTextureAtlas(MA_TextureAtlasserProAtlas atlas) { if (atlas.textureGroupRegistration == null) { atlas.textureGroupRegistration = new List <MA_TextureGroupRegistration>(); MA_TextureGroupRegistration groupRegistration = new MA_TextureGroupRegistration(); groupRegistration.name = DEFAULTTEXTUREGROUPNAME; atlas.textureGroupRegistration.Add(groupRegistration); } if (atlas.textureQuads == null) { atlas.textureQuads = new List <MA_TextureAtlasserProQuad>(); } else { bool _sameCount = true; foreach (MA_TextureAtlasserProQuad q in atlas.textureQuads) { if (q.textureGroups.Count != atlas.textureGroupRegistration.Count) { _sameCount = false; Debug.LogWarning("TextureAtlasser: " + q.name + " doesn't have the right amount of texture groups!"); } } if (_sameCount) { foreach (MA_TextureAtlasserProQuad q in atlas.textureQuads) { for (int i = 0; i < atlas.textureQuads.Count; i++) { for (int j = 0; j < atlas.textureGroupRegistration.Count; j++) { if (atlas.textureQuads[i].textureGroups[j].name != atlas.textureGroupRegistration[j].name) { Debug.LogWarning("TextureAtlasser: " + q.name + " doesn't have the right texture group name!"); } } } } } } }
public static void SetTextureGroups(MA_TextureAtlasserProAtlas atlas, MA_TextureAtlasserProQuad quad) { if (quad.textureGroups == null) { quad.textureGroups = new List <MA_TextureGroup>(); } //Add texture groups foreach (MA_TextureGroupRegistration tgr in atlas.textureGroupRegistration) { MA_TextureGroup textureGroup = new MA_TextureGroup { name = tgr.name }; quad.textureGroups.Add(textureGroup); } }
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); }
public static void CreateTextureGroup(MA_TextureAtlasserProAtlas atlas, string name) { MA_TextureGroupRegistration _textureGroupRegistration = new MA_TextureGroupRegistration { name = name }; atlas.textureGroupRegistration.Add(_textureGroupRegistration); foreach (MA_TextureAtlasserProQuad q in atlas.textureQuads) { MA_TextureGroup _textureGroup = new MA_TextureGroup { name = name }; q.textureGroups.Add(_textureGroup); } }
public static void DuplicateTextureQuad(MA_TextureAtlasserProAtlas atlas, bool focus = true, bool copyData = false, string namePrefix = "new ") { if (atlas != null && atlas.selectedTextureQuad != null) { CreateTextureQuad(atlas, namePrefix + atlas.selectedTextureQuad.name, atlas.selectedTextureQuad.rect, false); if (copyData) { atlas.textureQuads[atlas.textureQuads.Count - 1].meshes = atlas.selectedTextureQuad.meshes; atlas.textureQuads[atlas.textureQuads.Count - 1].textureGroups = atlas.selectedTextureQuad.textureGroups; } if (focus) { atlas.selectedTextureQuad = atlas.textureQuads[atlas.textureQuads.Count - 1]; } } }
public static void RemoveTextureQuad(MA_TextureAtlasserProAtlas atlas, bool focus = true) { if (atlas != null && atlas.selectedTextureQuad != null) { int _index = atlas.textureQuads.IndexOf(atlas.selectedTextureQuad); atlas.textureQuads.RemoveAt(_index); Object.DestroyImmediate(atlas.selectedTextureQuad, true); AssetDatabase.SaveAssets(); AssetDatabase.Refresh(); if (focus && atlas.textureQuads.Count > 0) { _index = Mathf.Clamp(_index, 0, atlas.textureQuads.Count - 1); atlas.selectedTextureQuad = atlas.textureQuads[_index]; } } }
public static string ExportAtlasMaterial(MA_TextureAtlasserProAtlas atlas, MaterialExportSettings materialExportSettings, string[] textures = null, string savePath = EXPORT_ASSET_PATH) { if (atlas == null || atlas.textureQuads == null || atlas.textureGroupRegistration == null) { return(null); } //Directories. string savePathMaterial = savePath + atlas.name + "/Materials/"; CreateFolder(savePathMaterial); Shader shader = materialExportSettings.shader; if (shader) { Material material = new Material(shader) { name = atlas.name }; if (textures != null) { for (int i = 0; i < (int)Mathf.Min(materialExportSettings.shaderPropertyNames.Count, textures.Length); i++) { Texture t = AssetDatabase.LoadAssetAtPath <Texture>(textures[i]); if (t != null) { material.SetTexture(materialExportSettings.shaderPropertyNames[i], t); } } } string assetPath = savePathMaterial + material.name + ".mat"; //Save material AssetDatabase.CreateAsset(material, assetPath); AssetDatabase.Refresh(); return(assetPath); } return(null); }
private static void SetAtlasSpriteSettings(MA_TextureAtlasserProAtlas atlas, TextureExportSettings textureExportSettings, string savePath = EXPORT_ASSET_PATH) { //Foreach texture group for (int i = 0; i < atlas.textureGroupRegistration.Count; i++) { //Convert string textureName = atlas.name + "_" + atlas.textureGroupRegistration[i].name + '.' + textureExportSettings.textureFormat.ToString(); TextureImporter textureImporter = (TextureImporter)AssetImporter.GetAtPath(savePath + textureName); textureImporter.textureType = TextureImporterType.Sprite; textureImporter.alphaIsTransparency = true; //Slice sprites. if (textureExportSettings.textureType == TextureType.SpriteSliced) { textureImporter.spriteImportMode = SpriteImportMode.None; //Reset it to update? textureImporter.spriteImportMode = SpriteImportMode.Multiple; List <SpriteMetaData> spriteMetaData = new List <SpriteMetaData>(); foreach (MA_TextureAtlasserProQuad q in atlas.textureQuads) { if (q.textureGroups != null && q.textureGroups[i].texture != null) { //Create new SpriteMetaData. SpriteMetaData smd = new SpriteMetaData { name = q.name, rect = new Rect(q.guiRect.x, atlas.textureAtlasSize.y - q.guiRect.y - q.guiRect.height, q.guiRect.width, q.guiRect.height) }; spriteMetaData.Add(smd); } } textureImporter.spritesheet = spriteMetaData.ToArray(); } else { textureImporter.spriteImportMode = SpriteImportMode.Single; } textureImporter.SaveAndReimport(); } }
public static void ExportAtlasModels(MA_TextureAtlasserProAtlas atlas, ModelFormat modelFormat, string savePath = EXPORTASSETPATH) { switch (modelFormat) { case ModelFormat.None: break; case ModelFormat.Obj: ExportAtlasObj(atlas, savePath); break; case ModelFormat.ReplaceObj: ModifyAtlasObj(atlas); break; default: break; } }
public static MA_TextureAtlasserProAtlas CreateTextureAtlas(string name, Vector2 size) { MA_TextureAtlasserProAtlas _atlas = ScriptableObject.CreateInstance <MA_TextureAtlasserProAtlas>(); if (_atlas != null) { _atlas.CreateAtlas(name, size); MA_CheckTextureAtlas(_atlas); AssetDatabase.CreateAsset(_atlas, SAVEASSETPATH + name + ".asset"); AssetDatabase.SaveAssets(); AssetDatabase.Refresh(); return(_atlas); } else { return(null); } }
public static void DuplicateTextureQuad(MA_TextureAtlasserProAtlas atlas, bool focus = true, string namePrefix = "new ") { if (atlas != null && atlas.selectedTextureQuad != null) { //MA_TextureAtlasserProQuad q = CreateTextureQuad(atlas, namePrefix + atlas.selectedTextureQuad.name, atlas.selectedTextureQuad.rect, false); MA_TextureAtlasserProQuad q = Object.Instantiate(atlas.selectedTextureQuad); q.name = string.Format("{0}{1}", namePrefix, atlas.selectedTextureQuad.name); atlas.textureQuads.Add(q); AssetDatabase.AddObjectToAsset(q, atlas); if (focus) { atlas.selectedTextureQuad = q; } AssetDatabase.SaveAssets(); AssetDatabase.Refresh(); } }
private static string[] ExportAtlasUnityMeshPrefab(MA_TextureAtlasserProAtlas atlas, ModelExportSettings modelExportSettings, string material = null, 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); //Set name string meshName = string.IsNullOrEmpty(quad.name) ? "" : quad.name + "-"; meshName += quad.meshes[m].name; int n = m + 1; meshName += "_" + n.ToString("#000"); newMesh.name = meshName; //Save it string asset = MA_MeshUtils.MA_SaveMeshPrefab(newMesh, meshName, savePath, materialPath: material); assetPaths.Add(asset); } } } } return(assetPaths.ToArray()); }
// public static void ExportAtlasTexturePNG(MA_TextureAtlasserProAtlas atlas, string savePath = EXPORTASSETPATH) // { // if(atlas != null && atlas.textureQuads != null) // { // //Create new Texture Atlas // Texture2D newTexture = new Texture2D((int)atlas.textureAtlasSize.x, (int)atlas.textureAtlasSize.y); // newTexture.name = atlas.name; // foreach (MA_TextureAtlasserProQuad ta in atlas.textureQuads) // { // //Export Texture Atlas // //TODO: Replace with texture groups (foreacht ...) // if(ta.texture != null) // { // //Create new texture part // Texture2D newTexturePart = (Texture2D)MA_Texture.MA_TextureUtils.ConvertToReadableTexture(ta.texture); // //Scale it // newTexturePart = newTexturePart.MA_Scale2D((int)ta.guiRect.width, (int)ta.guiRect.height); // //Add it // newTexture = newTexture.MA_Combine2D(newTexturePart, (int)ta.guiRect.x, (int)ta.guiRect.y); // } // } // //Save it // newTexture.MA_Save2D("MA_" + newTexture.name, savePath); // //Refresh // AssetDatabase.Refresh(); // } // } public static void ExportAtlasTexturesPNG(MA_TextureAtlasserProAtlas atlas, string savePath = EXPORTASSETPATH) { if (atlas != null && atlas.textureQuads != null && atlas.textureGroupRegistration != null) { //Foreach texture group for (int i = 0; i < atlas.textureGroupRegistration.Count; i++) { //Create new Texture Atlas Texture2D newTexture = new Texture2D((int)atlas.textureAtlasSize.x, (int)atlas.textureAtlasSize.y); newTexture.name = atlas.name + "_" + atlas.textureGroupRegistration[i].name; foreach (MA_TextureAtlasserProQuad q in atlas.textureQuads) { if (q.textureGroups != null && q.textureGroups[i].texture != null) { //Create new texture part Texture2D newTexturePart = (Texture2D)MA_Texture.MA_TextureUtils.ConvertToReadableTexture(q.textureGroups[i].texture); //Scale it newTexturePart = newTexturePart.MA_Scale32D((int)q.guiRect.width, (int)q.guiRect.height); //Add it newTexture = newTexture.MA_Combine2D(newTexturePart, (int)q.guiRect.x, (int)q.guiRect.y); } } //Save it newTexture.MA_Save2D("MA_" + newTexture.name, savePath); TextureImporter textureImporter = (TextureImporter)AssetImporter.GetAtPath(savePath + "MA_" + newTexture.name + ".png"); textureImporter.textureType = TextureImporterType.Default; textureImporter.SaveAndReimport(); } //Refresh AssetDatabase.Refresh(); } }
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()); }
private static string[] ExportAtlasPNG(MA_TextureAtlasserProAtlas atlas, TextureExportSettings textureExportSettings, string savePath = EXPORT_ASSET_PATH, string tempPath = TEMP_ASSET_PATH) { if (atlas == null || atlas.textureQuads == null || atlas.textureGroupRegistration == null) { return(null); } string[] assetPaths = new string[atlas.textureGroupRegistration.Count]; //Create temp folder CreateFolder(tempPath); //Foreach texture group for (int i = 0; i < atlas.textureGroupRegistration.Count; i++) { //Create new Texture Atlas Texture2D newTexture = new Texture2D((int)atlas.textureAtlasSize.x, (int)atlas.textureAtlasSize.y) { name = atlas.name + "_" + atlas.textureGroupRegistration[i].name }; foreach (MA_TextureAtlasserProQuad q in atlas.textureQuads) { if (q.textureGroups != null && q.textureGroups[i].texture != null) { //Make temp copy string orginalTexturePath = AssetDatabase.GetAssetPath(q.textureGroups[i].texture); string orginalTextureExtension = System.IO.Path.GetExtension(orginalTexturePath); string tempTexturePath = tempPath + q.textureGroups[i].texture.name + orginalTextureExtension; AssetDatabase.CopyAsset(orginalTexturePath, tempTexturePath); //Set temp copy to default settings TextureImporter tempTextureImporter = (TextureImporter)AssetImporter.GetAtPath(tempTexturePath); tempTextureImporter.textureType = TextureImporterType.Default; tempTextureImporter.sRGBTexture = false; tempTextureImporter.alphaIsTransparency = false; tempTextureImporter.maxTextureSize = (int)Mathf.Max(atlas.textureAtlasSize.x, atlas.textureAtlasSize.y); tempTextureImporter.textureCompression = TextureImporterCompression.Uncompressed; tempTextureImporter.SaveAndReimport(); //Load temp copy Texture tempCopy = AssetDatabase.LoadAssetAtPath <Texture>(tempTextureImporter.assetPath); //Create new texture part Texture2D newTexturePart = (Texture2D)MA_TextureUtils.ConvertToReadableTexture(tempCopy); //Scale it newTexturePart = newTexturePart.MA_Scale2D((int)q.guiRect.width, (int)q.guiRect.height, textureExportSettings.textureScaleMode); //Add it newTexture = newTexture.MA_Combine2D(newTexturePart, (int)q.guiRect.x, (int)q.guiRect.y); //Delete temp copy AssetDatabase.DeleteAsset(tempTextureImporter.assetPath); } } //Save it newTexture.MA_Save2D(newTexture.name, savePath); assetPaths[i] = (savePath + newTexture.name + '.' + textureExportSettings.textureFormat.ToString()); //Set settings. switch (textureExportSettings.textureType) { case TextureType.Default: { TextureImporter textureImporter = (TextureImporter)AssetImporter.GetAtPath(savePath + newTexture.name + '.' + textureExportSettings.textureFormat.ToString()); textureImporter.textureType = TextureImporterType.Default; textureImporter.SaveAndReimport(); } break; case TextureType.Sprite: SetAtlasSpriteSettings(atlas, textureExportSettings, savePath); break; case TextureType.SpriteSliced: SetAtlasSpriteSettings(atlas, textureExportSettings, savePath); break; default: break; } } //Delete temp folder DeleteFolder(tempPath); //Refresh AssetDatabase.Refresh(); return(assetPaths); }
private void OnGUI() { if (thisWindow == null) { GetCurrentWindow(); return; } //Get current event Event e = ProcessEvents(); if (isLoaded) { GUILayout.BeginArea(new Rect(MA_TextureAtlasserProUtils.VIEW_OFFSET, MA_TextureAtlasserProUtils.VIEW_OFFSET, position.width - (MA_TextureAtlasserProUtils.VIEW_OFFSET * 2), position.height - (MA_TextureAtlasserProUtils.VIEW_OFFSET * 2))); GUILayout.BeginVertical(); //Input options textureAtlasName = EditorGUILayout.TextField("Atlas name", textureAtlasName, GUILayout.ExpandWidth(true)); if (textureAtlasName == "Atlas name" || string.IsNullOrEmpty(textureAtlasName)) { nameError = true; GUI.backgroundColor = Color.red; GUILayout.Box("Error: Enter a valid atlas name!", EditorStyles.helpBox); GUI.backgroundColor = Color.white; } else { nameError = false; } textureAtlasSize.x = EditorGUILayout.IntField("Atlas width", (int)textureAtlasSize.x, GUILayout.ExpandWidth(true)); if (linkedAtlasSize) { linkedAtlasSize = EditorGUILayout.Toggle("link height", linkedAtlasSize, GUILayout.ExpandWidth(true)); textureAtlasSize.y = textureAtlasSize.x; } else { textureAtlasSize.y = EditorGUILayout.IntField("Atlas height", (int)textureAtlasSize.y, GUILayout.ExpandWidth(true)); } if (!Mathf.IsPowerOfTwo((int)textureAtlasSize.x) || !Mathf.IsPowerOfTwo((int)textureAtlasSize.y)) { GUI.backgroundColor = Color.yellow; GUILayout.Box("Warning: Atlas size value isn't a power of two!", EditorStyles.helpBox); GUI.backgroundColor = Color.white; } if (textureAtlasSize.x < 64 || textureAtlasSize.y < 64) { sizeError = true; GUI.backgroundColor = Color.red; GUILayout.Box("Error: The minimum atlas size is 64!", EditorStyles.helpBox); GUI.backgroundColor = Color.white; } else { sizeError = false; } //Create if (!nameError && !sizeError) { if (GUILayout.Button("Create Atlas", GUILayout.ExpandWidth(true), GUILayout.Height(37))) { textureAtlas = MA_TextureAtlasserProUtils.CreateTextureAtlas(textureAtlasName, textureAtlasSize); if (curWindow != null) { curWindow.textureAtlas = textureAtlas; } else { Debug.Log("No editor window found"); } CloseWindow(); } } GUILayout.EndVertical(); GUILayout.EndArea(); } if (e.type == EventType.Repaint) { isLoaded = true; } }