From b5da4a9af2d1ef9d017767b64b643a182b92a3f9 Mon Sep 17 00:00:00 2001 From: EliyaFishman <45822259+EliyaFishman@users.noreply.github.com> Date: Wed, 30 Nov 2022 17:57:34 +0200 Subject: [PATCH] add option to not rotate texture + small ui enhancments --- Assets/Editor/bread2unity/Bread2Unity.cs | 28 ++++++++++++++++------ Assets/Editor/bread2unity/SpriteCreator.cs | 18 +++++++------- 2 files changed, 30 insertions(+), 16 deletions(-) diff --git a/Assets/Editor/bread2unity/Bread2Unity.cs b/Assets/Editor/bread2unity/Bread2Unity.cs index 2ec7e1024..aa9338b29 100644 --- a/Assets/Editor/bread2unity/Bread2Unity.cs +++ b/Assets/Editor/bread2unity/Bread2Unity.cs @@ -4,7 +4,6 @@ using System.IO; using System.Linq; using UnityEngine; using UnityEditor; - using Bread2Unity; namespace Bread2Unity @@ -13,9 +12,10 @@ namespace Bread2Unity { public const string EditorFolderName = "bread2unity"; private GameObject _prefab; - private DataModel animation; + private DataModel _animation; private List _prefabDataList = new List(); private List _animationsIndexes; + private bool shouldRotate = false; private Vector2 _scrollPosition; @@ -32,6 +32,7 @@ namespace Bread2Unity public void OnGUI() { + // Logo Texture logo = (Texture)AssetDatabase.LoadAssetAtPath($"Assets/Editor/{EditorFolderName}/logo.png", typeof(Texture)); GUILayout.Box(logo, GUILayout.ExpandWidth(true), GUILayout.Height(60)); @@ -40,7 +41,7 @@ namespace Bread2Unity GUIStyle desc = EditorStyles.label; desc.wordWrap = true; desc.fontStyle = FontStyle.BoldAndItalic; - + // Description GUILayout.Box( "bread2unity is a tool built with the purpose of converting RH Megamix animations to unity. And to generally speed up development by a lot.", desc); @@ -48,6 +49,7 @@ namespace Bread2Unity GUILayout.Space(60); EditorGUIUtility.labelWidth = 100; + // Prefab Selector GUILayout.BeginHorizontal(); EditorGUILayout.PrefixLabel("Prefab"); _prefab = (GameObject)EditorGUILayout.ObjectField(_prefab, typeof(GameObject), false); @@ -55,6 +57,7 @@ namespace Bread2Unity EditorGUILayout.Separator(); + // Prefab data input _scrollPosition = EditorGUILayout.BeginScrollView(_scrollPosition, GUILayout.MaxHeight(60)); var plusGuiStyle = new GUIStyle(GUI.skin.button) { @@ -88,15 +91,22 @@ namespace Bread2Unity EditorGUILayout.Separator(); + // Add line button if (GUILayout.Button("+", plusGuiStyle, GUILayout.Height(40), GUILayout.Width(40))) { _prefabDataList.Add(new PrefabData("", 0)); _animationsIndexes.Add(""); } - EditorGUILayout.Separator(); + GUILayout.Space(12f); - if (GUILayout.Button("Create Prefabs (WIP)") && _prefab != null) + // Rotate check box + shouldRotate = GUILayout.Toggle(shouldRotate, "Rotate Spritesheet"); + + GUILayout.Space(12f); + + // Create button + if (GUILayout.Button("Generate Assets") && _prefab != null) { //Choose png and bccad files var bccadFilePath = EditorUtility.OpenFilePanel("Open BCCAD File", null, "bccad"); @@ -105,7 +115,7 @@ namespace Bread2Unity if (bccadFilePath != null && pngFilePath != null) { var bccad = BCCAD.Read(File.ReadAllBytes(bccadFilePath)); - var spriteTexture = SpriteCreator.ComputeSprites(bccad, pngFilePath, _prefab.name); + var spriteTexture = SpriteCreator.ComputeSprites(bccad, pngFilePath, _prefab.name, shouldRotate); //Create prefab from prefab data for (int i = 0; i < _prefabDataList.Count; i++) { @@ -123,11 +133,15 @@ namespace Bread2Unity } } + GUILayout.Space(12f); + if (GUILayout.Button("bccad test")) { BccadTest.TestBccad(); } - + + GUILayout.Space(12f); + GUILayout.BeginHorizontal(); if (GUILayout.Button("Bread Download", GUILayout.Height(40))) { diff --git a/Assets/Editor/bread2unity/SpriteCreator.cs b/Assets/Editor/bread2unity/SpriteCreator.cs index 31fdd872a..b08216600 100644 --- a/Assets/Editor/bread2unity/SpriteCreator.cs +++ b/Assets/Editor/bread2unity/SpriteCreator.cs @@ -11,7 +11,7 @@ namespace Bread2Unity public class SpriteCreator : MonoBehaviour { public const int PixelsPerUnit = 100; - public static Texture2D ComputeSprites(BCCAD bccad, string texturePath, string prefabName) + public static Texture2D ComputeSprites(BCCAD bccad, string texturePath, string prefabName, bool shouldRotate = false) { var textureName = Path.GetFileName(texturePath); var spritesFolder = @@ -26,9 +26,9 @@ namespace Bread2Unity $"{textureName}"; var newTexture = new Texture2D(bccad.sheetW, bccad.sheetH); newTexture.LoadImage(File.ReadAllBytes(texturePath)); - var rotatedTexture = RotateTexture(newTexture); - rotatedTexture.name = textureName.Substring(0, textureName.Length - ".png".Length); - File.WriteAllBytes(destTexturePath, rotatedTexture.EncodeToPNG()); + var finalTexture = shouldRotate ? RotateTexture(newTexture) : newTexture; + finalTexture.name = textureName.Substring(0, textureName.Length - ".png".Length); + File.WriteAllBytes(destTexturePath, finalTexture.EncodeToPNG()); AssetDatabase.ImportAsset(destTexturePath); var ti = AssetImporter.GetAtPath(destTexturePath) as TextureImporter; @@ -43,17 +43,17 @@ namespace Bread2Unity ti.textureCompression = TextureImporterCompression.Uncompressed; var newData = new List(); var rectCtr = 0; - var heightRatio = (float)rotatedTexture.height / bccad.sheetH; - var widthRatio = (float)rotatedTexture.width / bccad.sheetW; + var heightRatio = (float)finalTexture.height / bccad.sheetH; + var widthRatio = (float)finalTexture.width / bccad.sheetW; foreach (var r in bccad.regions) { var smd = new SpriteMetaData { pivot = new Vector2(0.5f, 0.5f), alignment = 0, - name = rotatedTexture.name + "_" + rectCtr, + name = finalTexture.name + "_" + rectCtr, rect = new Rect(r.regionX * widthRatio, - rotatedTexture.height - (r.regionH + r.regionY) * heightRatio, r.regionW * widthRatio, + finalTexture.height - (r.regionH + r.regionY) * heightRatio, r.regionW * widthRatio, r.regionH * heightRatio) }; @@ -65,7 +65,7 @@ namespace Bread2Unity } AssetDatabase.ImportAsset(destTexturePath, ImportAssetOptions.ForceUpdate); - return rotatedTexture; + return finalTexture; } public static Texture2D RotateTexture(Texture2D image)