-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTextureConverter.cs
More file actions
235 lines (209 loc) · 7.91 KB
/
TextureConverter.cs
File metadata and controls
235 lines (209 loc) · 7.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
using System;
using System.IO;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
#endif
public class TextureConverter : EditorWindow
{
public string texturesFolder = "Assets/Textures";
private static EditorWindow _editorWindow;
private Vector2 _scrollPos;
#if UNITY_EDITOR
[MenuItem("Tools/Texture Converter/Texture Converter")]
public static void ShowFolderPanel()
{
OpenFolderPanel();
}
[MenuItem("Tools/Texture Converter/Texture Converter (Window)")]
public static void ShowWindow()
{
_editorWindow = GetWindow(typeof(TextureConverter), false);
}
[MenuItem("Assets/Texture Converter/Texture Converter")]
public static void ShowFolderPanelRightClick()
{
OpenFolderPanel();
}
[MenuItem("Assets/Texture Converter/Texture Converter (Window)")]
public static void ShowWindowRightClick()
{
_editorWindow = GetWindow(typeof(TextureConverter), false);
}
private static void OpenFolderPanel()
{
string path = UnityEditor.EditorUtility.OpenFolderPanel("Select Folder", "", "");
if (path.Length != 0)
{
bool confirmedDialog = UnityEditor.EditorUtility.DisplayDialog(
title: "Convert Textures to JPG and PNG",
message: "This will created duplicated JPG and PNG textures in the same folder. Are you sure you want to continue?",
ok: "Apply",
cancel: "Cancel"
);
if (confirmedDialog)
{
ConvertTextures(RemoveBefore(path, "Assets"));
}
}
}
private static string RemoveBefore(string value, string character)
{
int index = value.IndexOf(character);
if (index > 0)
{
value = value.Substring(index);
}
return value;
}
private void OnInspectorUpdate()
{
if (!_editorWindow)
_editorWindow = GetWindow(typeof(TextureConverter), false);
}
private void OnGUI()
{
if (_editorWindow)
{
GUILayout.BeginArea(new Rect(0, 0, _editorWindow.position.size.x, _editorWindow.position.size.y));
GUILayout.BeginVertical();
_scrollPos = GUILayout.BeginScrollView(_scrollPos, false, true, GUILayout.ExpandHeight(true));
}
// Textures Folder
GUILayout.BeginVertical(EditorStyles.helpBox);
GUILayout.Space(10f);
texturesFolder = EditorGUILayout.TextField("Textures Folder", texturesFolder);
GUILayout.Space(10f);
GUILayout.EndVertical();
// Button
GUILayout.BeginVertical(EditorStyles.helpBox);
GUILayout.Space(10f);
if (GUILayout.Button("Generate PBR Textures"))
{
ConvertTextures(texturesFolder);
}
GUILayout.Space(10f);
GUILayout.EndVertical();
GUILayout.Space(100);
if (_editorWindow)
{
GUILayout.EndScrollView();
GUILayout.EndVertical();
GUILayout.EndArea();
}
}
private static void ConvertTextures(string texturesFolder)
{
EditorUtility.DisplayProgressBar("Packing Textures, please wait...", "", 1f);
try
{
// "keyword l: <label> t: <type>"
// https://docs.unity3d.com/ScriptReference/AssetDatabase.FindAssets.html
string[] guids = AssetDatabase.FindAssets("_ t: texture2D", new[] { texturesFolder });
foreach (string guid in guids)
{
string path = AssetDatabase.GUIDToAssetPath(guid);
Texture2D tex = (Texture2D)AssetDatabase.LoadAssetAtPath(path, typeof(Texture2D));
if (tex == null)
{
continue;
}
string textureName = tex.name;
string folder = Path.GetDirectoryName(path) + "/";
TextureImporter textureImporter = (TextureImporter)TextureImporter.GetAtPath(path);
bool hasAlpha = textureImporter.DoesSourceTextureHaveAlpha();
// Set Texture Importer before copying it. (To have the same result)
// TextureType (Default)
TextureImporterType textureType = textureImporter.textureType;
textureImporter.textureType = TextureImporterType.Default;
// sRGB (true)
bool sRGB = textureImporter.sRGBTexture;
textureImporter.sRGBTexture = true;
// MaxTextureSize (4096)
string platform = EditorUserBuildSettings.activeBuildTarget.ToString();
int maxTextureSize = 4096;
bool hasPlatformSettings = textureImporter.GetPlatformTextureSettings(platform, out maxTextureSize, out TextureImporterFormat textureFormat, out int compressionQuality, out bool etc1AlphaSplitEnabled);
if (hasPlatformSettings)
{
textureImporter.SetPlatformTextureSettings(
new TextureImporterPlatformSettings() {
name = platform,
overridden = true,
maxTextureSize = 4096,
format = TextureImporterFormat.RGBA32,
compressionQuality = 100
});
}
else
{
maxTextureSize = textureImporter.maxTextureSize;
textureImporter.maxTextureSize = 4096;
}
EditorUtility.SetDirty(textureImporter);
textureImporter.SaveAndReimport();
// Duplicate texture
Texture2D texture = TexturePackerUtilities.DuplicateTexture(tex);
SaveTexture(texture, folder, textureName, hasAlpha, textureImporter);
// Set back to original settings
textureImporter.textureType = textureType;
textureImporter.sRGBTexture = sRGB;
if (hasPlatformSettings)
{
textureImporter.SetPlatformTextureSettings(
new TextureImporterPlatformSettings() {
name = platform,
overridden = true,
maxTextureSize = maxTextureSize,
format = textureFormat,
compressionQuality = 100
});
}
else
{
textureImporter.maxTextureSize = maxTextureSize;
}
EditorUtility.SetDirty(textureImporter);
textureImporter.SaveAndReimport();
}
}
catch (Exception e)
{
Debug.Log($"TextureConverter Exception: {e}");
}
EditorUtility.ClearProgressBar();
}
private static void SaveTexture(Texture2D texture, string folder, string textureName, bool hasAlpha = false, TextureImporter originalTextureImporter = null)
{
byte[] bytes;
string extension;
if (hasAlpha)
{
bytes = ImageConversion.EncodeToPNG(texture);
extension = "png";
}
else
{
bytes = ImageConversion.EncodeToJPG(texture);
extension = "jpg";
}
string path = $"{folder}/{textureName}.{extension}";
if (bytes != null)
{
if (!Directory.Exists(folder))
{
Directory.CreateDirectory(folder);
}
File.WriteAllBytes(path, bytes);
}
if(originalTextureImporter != null)
{
// TextureImporter textureImporter = (TextureImporter)TextureImporter.GetAtPath(path);
// textureImporter.SaveAndReimport();
}
AssetDatabase.Refresh();
}
#endif
}