From 3e212932c028ce36510810cc7492cf918ee597df Mon Sep 17 00:00:00 2001 From: Pituivan Date: Sat, 26 Apr 2025 23:04:53 +0200 Subject: [PATCH 1/6] Added support for using a custom texture size --- Editor/TexturePackerWindow.cs | 21 ++++++++++++++++++--- Runtime/TexturePacker.cs | 13 +++++++++++-- 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/Editor/TexturePackerWindow.cs b/Editor/TexturePackerWindow.cs index ea77800..9371a50 100644 --- a/Editor/TexturePackerWindow.cs +++ b/Editor/TexturePackerWindow.cs @@ -26,6 +26,8 @@ public class TexturePackerWindow : EditorWindow private List _items = new List(); private TexturePreview _preview; + private bool _useCustomTexSize = false; + [MenuItem("Window/Channel Packer")] static void Open() { @@ -93,17 +95,30 @@ private void OnGUI() GUILayout.FlexibleSpace(); EditorGUILayout.EndHorizontal(); - int prevRes = _texturePacker.resolution; + Vector2Int prevTexSize = _texturePacker.texSize; _texturePacker.resolution = 128; _preview.Draw(_texturePacker); - _texturePacker.resolution = prevRes; + _texturePacker.texSize = prevTexSize; GUILayout.Label("Options", TexturePackerStyles.Heading); GUILayout.BeginVertical(TexturePackerStyles.Section); _textureFormat = (TextureFormat)EditorGUILayout.EnumPopup("> Format:", _textureFormat); - _texturePacker.resolution = EditorGUILayout.IntPopup("> Resolution:", _texturePacker.resolution, _textureResolutionsNames.ToArray(), _textureResolutions.ToArray()); + + _useCustomTexSize = EditorGUILayout.Toggle("> Custom texture size:", _useCustomTexSize); + + if (_useCustomTexSize) + { + int width = EditorGUILayout.IntField("> Texture width:", _texturePacker.texSize.x); + int height = EditorGUILayout.IntField("> Texture height:", _texturePacker.texSize.y); + _texturePacker.texSize = new Vector2Int(width, height); + } + else + { + _texturePacker.texSize = Vector2Int.one * EditorGUILayout.IntPopup("> Resolution:", _texturePacker.resolution, _textureResolutionsNames.ToArray(), _textureResolutions.ToArray()); + } + GUILayout.EndVertical(); if (GUILayout.Button("Generate Texture", TexturePackerStyles.Button)) diff --git a/Runtime/TexturePacker.cs b/Runtime/TexturePacker.cs index d9de1c7..a1a8d8d 100644 --- a/Runtime/TexturePacker.cs +++ b/Runtime/TexturePacker.cs @@ -13,7 +13,16 @@ public List texInputs { get { return _texInputs; } } - public int resolution = 2048; + public Vector2Int texSize = new Vector2Int(2048, 2048); + + private int _resolution; + public int resolution { + get { return _resolution; } + set { + _resolution = value; + texSize = Vector2Int.one * value; + } + } public void Initialize() { @@ -108,7 +117,7 @@ public Texture2D Create() ++idx; } - var texture = TextureUtility.GenerateTexture(resolution, resolution, _material); + var texture = TextureUtility.GenerateTexture(texSize.x, texSize.y, _material); return texture; } From 901312d323b8d70c401c70176d4c138c68aacfbf Mon Sep 17 00:00:00 2001 From: Pituivan Date: Sun, 27 Apr 2025 00:03:25 +0200 Subject: [PATCH 2/6] Added a button to automatically set texture size to the size of the texture of a given input. Also prevented the bugs that would be caused if setting negative width or height or setting texture size as the size of a null texture --- Editor/TexturePackerWindow.cs | 40 +++++++++++++++++++++++------- Editor/TexturePackerWindow.cs.meta | 13 +--------- Runtime/TexturePacker.cs | 3 ++- 3 files changed, 34 insertions(+), 22 deletions(-) diff --git a/Editor/TexturePackerWindow.cs b/Editor/TexturePackerWindow.cs index 9371a50..28f43cc 100644 --- a/Editor/TexturePackerWindow.cs +++ b/Editor/TexturePackerWindow.cs @@ -1,4 +1,4 @@ -using System.Collections.Generic; +using System.Collections.Generic; using System.IO; using System.Linq; using UnityEditor; @@ -40,7 +40,7 @@ public void Initialize() minSize = _windowSize; titleContent = new GUIContent(_windowTitle); - for(int i = _textureSupportedResolutionMin; i <= _textureSupportedResolutionMax; i *= 2) + for (int i = _textureSupportedResolutionMin; i <= _textureSupportedResolutionMax; i *= 2) { _textureResolutions.Add(i); _textureResolutionsNames.Add(i.ToString()); @@ -55,7 +55,7 @@ private void RefreshItems() if (_items.Count == 0) return; - var toDeleteItems = _items.Where(x => x.toDelete==true).ToList(); + var toDeleteItems = _items.Where(x => x.toDelete == true).ToList(); foreach (var item in toDeleteItems) { _texturePacker.Remove(item.input); @@ -63,6 +63,11 @@ private void RefreshItems() } } + private void SetTexSizeFromInput(TextureInput input) + { + _texturePacker.texSize = input.texture == null ? Vector2Int.zero : new Vector2Int(input.texture.width, input.texture.height); + } + private void OnGUI() { _windowScrollPos = EditorGUILayout.BeginScrollView(_windowScrollPos, false, false); @@ -110,9 +115,26 @@ private void OnGUI() if (_useCustomTexSize) { - int width = EditorGUILayout.IntField("> Texture width:", _texturePacker.texSize.x); - int height = EditorGUILayout.IntField("> Texture height:", _texturePacker.texSize.y); + int width = Mathf.Abs(EditorGUILayout.IntField("> Texture width:", _texturePacker.texSize.x)); + int height = Mathf.Abs(EditorGUILayout.IntField("> Texture height:", _texturePacker.texSize.y)); _texturePacker.texSize = new Vector2Int(width, height); + + if (_texturePacker.texInputs.Count > 0) + { + EditorGUILayout.Separator(); + if (GUILayout.Button("Use size from input")) + { + var menu = new GenericMenu(); + for (int i = 0; i < _texturePacker.texInputs.Count; i++) + { + TextureInput input = _texturePacker.texInputs[i]; + menu.AddItem(new GUIContent($"Input {i + 1}"), on: false, () => SetTexSizeFromInput(input)); + } + + var dropdownPos = new Rect(Event.current.mousePosition, size: Vector2.zero); + menu.DropDown(dropdownPos); + } + } } else { @@ -124,10 +146,10 @@ private void OnGUI() if (GUILayout.Button("Generate Texture", TexturePackerStyles.Button)) { string defaultPath = Application.dataPath; - if(_texturePacker.texInputs.Count > 0 && _texturePacker.texInputs[0].texture != null) + if (_texturePacker.texInputs.Count > 0 && _texturePacker.texInputs[0].texture != null) { string path = AssetDatabase.GetAssetPath(_texturePacker.texInputs[0].texture); - if(path != null && !string.IsNullOrEmpty(path)) + if (path != null && !string.IsNullOrEmpty(path)) { path = Path.Combine(Application.dataPath, "..", path); defaultPath = Path.GetDirectoryName(path); @@ -138,9 +160,9 @@ private void OnGUI() { Texture2D output = _texturePacker.Create(); - if(_textureFormat == TextureFormat.JPG) + if (_textureFormat == TextureFormat.JPG) File.WriteAllBytes(savePath, output.EncodeToJPG()); - else if(_textureFormat == TextureFormat.PNG) + else if (_textureFormat == TextureFormat.PNG) File.WriteAllBytes(savePath, output.EncodeToPNG()); else File.WriteAllBytes(savePath, output.EncodeToEXR()); diff --git a/Editor/TexturePackerWindow.cs.meta b/Editor/TexturePackerWindow.cs.meta index ddd2b67..88a5a06 100644 --- a/Editor/TexturePackerWindow.cs.meta +++ b/Editor/TexturePackerWindow.cs.meta @@ -1,13 +1,2 @@ fileFormatVersion: 2 -guid: ccb4b81226a897345898c0c1e08e4f14 -timeCreated: 1518232730 -licenseType: Free -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: +guid: ccb4b81226a897345898c0c1e08e4f14 \ No newline at end of file diff --git a/Runtime/TexturePacker.cs b/Runtime/TexturePacker.cs index a1a8d8d..c2c18b4 100644 --- a/Runtime/TexturePacker.cs +++ b/Runtime/TexturePacker.cs @@ -18,7 +18,8 @@ public List texInputs { private int _resolution; public int resolution { get { return _resolution; } - set { + set + { _resolution = value; texSize = Vector2Int.one * value; } From 6a73358bf6eed2acbfce3bd80fb9a05222662d1a Mon Sep 17 00:00:00 2001 From: Pituivan Date: Sun, 27 Apr 2025 00:19:19 +0200 Subject: [PATCH 3/6] Made it so the default custom texture size is the size of the first input texture, as requested in #22 --- Editor/TexturePackerWindow.cs | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/Editor/TexturePackerWindow.cs b/Editor/TexturePackerWindow.cs index 28f43cc..acb1a9d 100644 --- a/Editor/TexturePackerWindow.cs +++ b/Editor/TexturePackerWindow.cs @@ -115,9 +115,15 @@ private void OnGUI() if (_useCustomTexSize) { - int width = Mathf.Abs(EditorGUILayout.IntField("> Texture width:", _texturePacker.texSize.x)); - int height = Mathf.Abs(EditorGUILayout.IntField("> Texture height:", _texturePacker.texSize.y)); - _texturePacker.texSize = new Vector2Int(width, height); + if (_texturePacker.texInputs.Count > 0) + { + Texture firstInputTex = _texturePacker.texInputs[0].texture; + _texturePacker.texSize.x = firstInputTex.width; + _texturePacker.texSize.y = firstInputTex.height; + } + + _texturePacker.texSize.x = Mathf.Abs(EditorGUILayout.IntField("> Texture width:", _texturePacker.texSize.x)); + _texturePacker.texSize.y = Mathf.Abs(EditorGUILayout.IntField("> Texture height:", _texturePacker.texSize.y)); if (_texturePacker.texInputs.Count > 0) { From aa37830ed262341a578cebcb13b260b7350bbbd7 Mon Sep 17 00:00:00 2001 From: Pituivan Date: Sun, 27 Apr 2025 00:27:59 +0200 Subject: [PATCH 4/6] Solved a NullReferenceException thrown when the texture of the first input was null --- Editor/TexturePackerWindow.cs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/Editor/TexturePackerWindow.cs b/Editor/TexturePackerWindow.cs index acb1a9d..9601507 100644 --- a/Editor/TexturePackerWindow.cs +++ b/Editor/TexturePackerWindow.cs @@ -117,9 +117,7 @@ private void OnGUI() { if (_texturePacker.texInputs.Count > 0) { - Texture firstInputTex = _texturePacker.texInputs[0].texture; - _texturePacker.texSize.x = firstInputTex.width; - _texturePacker.texSize.y = firstInputTex.height; + SetTexSizeFromInput(_texturePacker.texInputs[0]); } _texturePacker.texSize.x = Mathf.Abs(EditorGUILayout.IntField("> Texture width:", _texturePacker.texSize.x)); From c170e7ba44bfc3a870a8eccb07621afe1a5b1d50 Mon Sep 17 00:00:00 2001 From: Pituivan Date: Sun, 27 Apr 2025 00:49:26 +0200 Subject: [PATCH 5/6] Fixed last known bug, where the size of the first input texture would override the custom texture size --- Editor/TexturePackerWindow.cs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Editor/TexturePackerWindow.cs b/Editor/TexturePackerWindow.cs index 9601507..8071480 100644 --- a/Editor/TexturePackerWindow.cs +++ b/Editor/TexturePackerWindow.cs @@ -27,6 +27,7 @@ public class TexturePackerWindow : EditorWindow private TexturePreview _preview; private bool _useCustomTexSize = false; + private bool _overrideDefaultTextSize = true; [MenuItem("Window/Channel Packer")] static void Open() @@ -115,10 +116,11 @@ private void OnGUI() if (_useCustomTexSize) { - if (_texturePacker.texInputs.Count > 0) + if (_overrideDefaultTextSize && _texturePacker.texInputs.Count == 1) { SetTexSizeFromInput(_texturePacker.texInputs[0]); } + _overrideDefaultTextSize = false; _texturePacker.texSize.x = Mathf.Abs(EditorGUILayout.IntField("> Texture width:", _texturePacker.texSize.x)); _texturePacker.texSize.y = Mathf.Abs(EditorGUILayout.IntField("> Texture height:", _texturePacker.texSize.y)); @@ -143,6 +145,7 @@ private void OnGUI() else { _texturePacker.texSize = Vector2Int.one * EditorGUILayout.IntPopup("> Resolution:", _texturePacker.resolution, _textureResolutionsNames.ToArray(), _textureResolutions.ToArray()); + _overrideDefaultTextSize = true; } GUILayout.EndVertical(); From 91f415c39a9a04fc869d7c4963038695908182fd Mon Sep 17 00:00:00 2001 From: Pituivan Date: Sun, 27 Apr 2025 01:10:32 +0200 Subject: [PATCH 6/6] Just fixed a typo --- Editor/TexturePackerWindow.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Editor/TexturePackerWindow.cs b/Editor/TexturePackerWindow.cs index 8071480..fa80153 100644 --- a/Editor/TexturePackerWindow.cs +++ b/Editor/TexturePackerWindow.cs @@ -27,7 +27,7 @@ public class TexturePackerWindow : EditorWindow private TexturePreview _preview; private bool _useCustomTexSize = false; - private bool _overrideDefaultTextSize = true; + private bool _overrideDefaultTexSize = true; [MenuItem("Window/Channel Packer")] static void Open() @@ -116,11 +116,11 @@ private void OnGUI() if (_useCustomTexSize) { - if (_overrideDefaultTextSize && _texturePacker.texInputs.Count == 1) + if (_overrideDefaultTexSize && _texturePacker.texInputs.Count == 1) { SetTexSizeFromInput(_texturePacker.texInputs[0]); } - _overrideDefaultTextSize = false; + _overrideDefaultTexSize = false; _texturePacker.texSize.x = Mathf.Abs(EditorGUILayout.IntField("> Texture width:", _texturePacker.texSize.x)); _texturePacker.texSize.y = Mathf.Abs(EditorGUILayout.IntField("> Texture height:", _texturePacker.texSize.y)); @@ -145,7 +145,7 @@ private void OnGUI() else { _texturePacker.texSize = Vector2Int.one * EditorGUILayout.IntPopup("> Resolution:", _texturePacker.resolution, _textureResolutionsNames.ToArray(), _textureResolutions.ToArray()); - _overrideDefaultTextSize = true; + _overrideDefaultTexSize = true; } GUILayout.EndVertical();