diff --git a/Editor/TexturePackerWindow.cs b/Editor/TexturePackerWindow.cs index ea77800..fa80153 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; @@ -26,6 +26,9 @@ public class TexturePackerWindow : EditorWindow private List _items = new List(); private TexturePreview _preview; + private bool _useCustomTexSize = false; + private bool _overrideDefaultTexSize = true; + [MenuItem("Window/Channel Packer")] static void Open() { @@ -38,7 +41,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()); @@ -53,7 +56,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); @@ -61,6 +64,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); @@ -93,26 +101,62 @@ 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) + { + if (_overrideDefaultTexSize && _texturePacker.texInputs.Count == 1) + { + SetTexSizeFromInput(_texturePacker.texInputs[0]); + } + _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)); + + 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 + { + _texturePacker.texSize = Vector2Int.one * EditorGUILayout.IntPopup("> Resolution:", _texturePacker.resolution, _textureResolutionsNames.ToArray(), _textureResolutions.ToArray()); + _overrideDefaultTexSize = true; + } + GUILayout.EndVertical(); 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); @@ -123,9 +167,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 d9de1c7..c2c18b4 100644 --- a/Runtime/TexturePacker.cs +++ b/Runtime/TexturePacker.cs @@ -13,7 +13,17 @@ 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 +118,7 @@ public Texture2D Create() ++idx; } - var texture = TextureUtility.GenerateTexture(resolution, resolution, _material); + var texture = TextureUtility.GenerateTexture(texSize.x, texSize.y, _material); return texture; }