Skip to content

Feature: Custom texture size #23

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 54 additions & 10 deletions Editor/TexturePackerWindow.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Collections.Generic;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using UnityEditor;
Expand Down Expand Up @@ -26,6 +26,9 @@ public class TexturePackerWindow : EditorWindow
private List<TextureItem> _items = new List<TextureItem>();
private TexturePreview _preview;

private bool _useCustomTexSize = false;
private bool _overrideDefaultTexSize = true;

[MenuItem("Window/Channel Packer")]
static void Open()
{
Expand All @@ -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());
Expand All @@ -53,14 +56,19 @@ 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);
_items.Remove(item);
}
}

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);
Expand Down Expand Up @@ -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);
Expand All @@ -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());
Expand Down
13 changes: 1 addition & 12 deletions Editor/TexturePackerWindow.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 12 additions & 2 deletions Runtime/TexturePacker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,17 @@ public List<TextureInput> 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()
{
Expand Down Expand Up @@ -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;
}
Expand Down