So I wanted to save time and create icons with code instead of using an image editing program.
My first solution was simple, and effective, but left some images slightly damaged. The idea was to take the asset preview and make an icon out of it, but it has a background my default, so I removed the background as best as possible by doing this.
[MenuItem("Cheats/Gen Icon")]
public static void GenerateIcon()
{
Texture2D tex = AssetPreview.GetAssetPreview(Selection.activeGameObject);
Color[] colors = tex.GetPixels();
int i = 0;
Color alpha = colors[i];
Debug.Log(alpha);
for (; i < colors.Length; i++)
{
if (colors[i] == alpha)
{
colors[i].a = 0;
}
}
tex.SetPixels(colors);
byte[] bytes = tex.EncodeToPNG();
// For testing purposes, also write to a file in the project folder
System.IO.File.WriteAllBytes("Assets/" + Selection.activeGameObject.name + ".png", bytes);
AssetDatabase.ImportAsset("Assets/" + Selection.activeGameObject.name + ".png");
}
Again this works fine, but if any part of the object I'm creating an icon from contains the same color as background color, it gets removed.
So I'm trying to figure out how to access or remove the background color of AssetPreview. If I can turn it blue or a color not shared by my objects then that would solve it. So my second attempt was to capture an editor window, but I may be doing it wrong... Here is my code.
public class GameObjectEditorWindow : EditorWindow
{
GameObject gameObject;
Editor gameObjectEditor;
[MenuItem("Window/GameObject Editor")]
static void ShowWindow()
{
GetWindow("GameObject Editor");
}
Color fillColor;
void OnGUI()
{
gameObject = (GameObject)EditorGUILayout.ObjectField(gameObject, typeof(GameObject), true);
fillColor = EditorGUILayout.ColorField(fillColor);
if (gameObject != null)
{
if (gameObjectEditor == null)
gameObjectEditor = Editor.CreateEditor(gameObject);
GUIStyle style = new GUIStyle();
Texture2D texture2D = Texture2D.whiteTexture;
Color[] fillColorArray = texture2D.GetPixels();
for (var i = 0; i < fillColorArray.Length; ++i)
{
fillColorArray[i] = fillColor;
}
texture2D.SetPixels(fillColorArray);
texture2D.Apply();
style.normal.background = texture2D;
gameObjectEditor.OnPreviewGUI(GUILayoutUtility.GetRect(512, 512), style);
if (GUILayout.Button("Generate Texture"))
{
gameObjectEditor.ReloadPreviewInstances();
Object[] objs = new Object[1];
objs[0] = gameObject;
Texture2D tex = new Texture2D(512, 512, TextureFormat.RGB24, false);
tex.ReadPixels(GUILayoutUtility.GetRect(512, 512), 256, 256);
Color[] colors = tex.GetPixels();
int i = 0;
Color alpha = colors[i];
Debug.Log(alpha);
for (; i < colors.Length; i++)
{
Color c = new Color(colors[i].r, colors[i].g, colors[i].b, colors[i].a);
if (colors[i] == alpha)
{
c.a = 0;
}
colors[i] = c;
}
tex.SetPixels(colors);
byte[] bytes = tex.EncodeToPNG();
// For testing purposes, also write to a file in the project folder
System.IO.File.WriteAllBytes("Assets/" + gameObject.name + ".png", bytes);
AssetDatabase.ImportAsset("Assets/" + gameObject.name + ".png");
}
}
}
}
↧