forked from UnityCommunity/UnityLibrary
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSpriteBackgroundRemover.cs
161 lines (126 loc) · 5.48 KB
/
SpriteBackgroundRemover.cs
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
// original script : http://answers.unity3d.com/answers/252528/view.html
// Usage: Place this script in Editor/ folder
// Start the tool from menu, Window/Tools/Alpha-fy Images
using UnityEngine;
using UnityEditor;
using System.IO;
namespace UnityLibrary
{
public class SpriteBackgroundRemover : EditorWindow
{
Texture2D img;
Texture2D newImg;
Color colorToRemove = Color.magenta;
public static SpriteBackgroundRemover win;
[MenuItem("Window/Tools/Alpha-fy Images")]
static void Init()
{
win = ScriptableObject.CreateInstance(typeof(SpriteBackgroundRemover)) as SpriteBackgroundRemover;
win.minSize = new Vector2(300, 350);
win.ShowUtility();
}
void OnGUI()
{
GUILayout.BeginHorizontal();
/** Toolbar **/
GUILayout.BeginVertical();
img = (Texture2D)EditorGUILayout.ObjectField(img, typeof(Texture2D), false, GUILayout.MinWidth(128), GUILayout.MinHeight(128), GUILayout.MaxWidth(128), GUILayout.MaxHeight(128));
colorToRemove = EditorGUILayout.ColorField(colorToRemove, GUILayout.MaxWidth(128));
if (GUILayout.Button("Preview", GUILayout.MinWidth(128), GUILayout.MinHeight(32), GUILayout.MaxWidth(128), GUILayout.MaxHeight(128)))
newImg = RemoveColor(colorToRemove, img);
if (GUILayout.Button("Alpha-fy All", GUILayout.MinWidth(128), GUILayout.MinHeight(32), GUILayout.MaxWidth(128), GUILayout.MaxHeight(128)))
RemoveColor(colorToRemove, (UnityEngine.Object[])Selection.GetFiltered(typeof(Texture2D), SelectionMode.Assets));
GUILayout.EndVertical();
GUILayout.BeginVertical();
GUILayout.Label("Selected Files", EditorStyles.boldLabel);
foreach (Texture2D selected in Selection.GetFiltered(typeof(Texture2D), SelectionMode.Assets))
{
GUILayout.Label(selected.name);
}
GUILayout.EndVertical();
/** Image Display **/
GUILayout.BeginVertical();
GUILayout.Label("Preview", EditorStyles.boldLabel);
if (newImg)
{
GUILayout.Label(newImg);
}
GUILayout.EndVertical();
GUILayout.EndHorizontal();
}
// for multiple images
void RemoveColor(Color c, UnityEngine.Object[] imgs)
{
if (!Directory.Exists("Assets/AlphaImages/"))
{
Directory.CreateDirectory("Assets/AlphaImages/");
}
float inc = 0f;
foreach (Texture2D i in imgs)
{
inc++;
if (inc % 512 == 0 && EditorUtility.DisplayCancelableProgressBar("Playin' With Pixels", "Seaching for Color Matches", ((float)inc / (float)imgs.Length)))
{
Debug.LogError("Cancelled..");
break;
}
CheckTextureSettings(i);
Color[] pixels = i.GetPixels(0, 0, i.width, i.height, 0);
var clear = new Color(0, 0, 0, 0);
for (int p = 0; p < pixels.Length; p++)
{
if (pixels[p] == c)
{
pixels[p] = clear;
}
}
Texture2D n = new Texture2D(i.width, i.height);
n.SetPixels(0, 0, i.width, i.height, pixels, 0);
n.Apply();
byte[] bytes = n.EncodeToPNG();
File.WriteAllBytes("Assets/AlphaImages/" + i.name + "_alpha.png", bytes);
}
EditorUtility.ClearProgressBar();
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
}
// for single image
Texture2D RemoveColor(Color c, Texture2D i)
{
CheckTextureSettings(i);
Color[] pixels = i.GetPixels(0, 0, i.width, i.height, 0);
var clear = new Color(0, 0, 0, 0);
for (int p = 0; p < pixels.Length; p++)
{
if (p % 512 == 0 && EditorUtility.DisplayCancelableProgressBar("Playin' With Pixels", "Seaching for Color Matches", ((float)p / pixels.Length)))
{
Debug.LogError("Cancelled..");
break;
}
if (pixels[p] == c)
{
pixels[p] = clear;
}
}
Texture2D n = new Texture2D(i.width, i.height);
n.SetPixels(0, 0, i.width, i.height, pixels, 0);
n.Apply();
EditorUtility.ClearProgressBar();
return (n);
}
public void CheckTextureSettings(Texture2D texture)
{
if (texture == null) { Debug.LogError("CheckTextureSettings Failed - Texture is null"); return; }
string path = AssetDatabase.GetAssetPath(texture);
if (string.IsNullOrEmpty(path)) { Debug.LogError("CheckTextureSettings Failed - Texture path is null"); return; }
TextureImporter textureImporter = AssetImporter.GetAtPath(path) as TextureImporter;
if (!textureImporter.isReadable)
{
Debug.Log("Enabling read/write for image " + path);
// textureImporter.mipmapEnabled = false;
textureImporter.isReadable = true;
AssetDatabase.ImportAsset(path, ImportAssetOptions.ForceUpdate);
}
}
}
}