Skip to content

Instantly share code, notes, and snippets.

@jonbro
Created December 11, 2020 22:27
Show Gist options
  • Save jonbro/640df784bab92e6671b75830786a8ddc to your computer and use it in GitHub Desktop.
Save jonbro/640df784bab92e6671b75830786a8ddc to your computer and use it in GitHub Desktop.
Editor Script that generates a single color texture
using System.IO;
using UnityEngine;
using UnityEditor;
public class GenerateColorTexture : EditorWindow
{
private Color textureColor;
private string textureName;
private Vector2Int dimensions;
[MenuItem("Tools/Generate Color Texture")]
static void Init()
{
// Get existing open window or if none, make a new one:
GenerateColorTexture window = (GenerateColorTexture)EditorWindow.GetWindow(typeof(GenerateColorTexture));
window.Show();
}
void OnGUI()
{
textureColor = EditorGUILayout.ColorField("Color", textureColor);
textureName = EditorGUILayout.TextField("Filename", textureName);
dimensions = EditorGUILayout.Vector2IntField("Dimensions", dimensions);
if (GUILayout.Button("Generate Texure"))
{
var texture = new Texture2D(dimensions.x, dimensions.y);
var dim = dimensions.x * dimensions.y;
var colors = new Color32[dim];
for (int i = 0; i < dim; i++)
{
colors[i] = textureColor;
}
texture.SetPixels32(colors);
texture.Apply();
byte[] bytes = texture.EncodeToPNG();
var dirPath = Application.dataPath + "/";
var filename = dirPath + textureName + ".png";
File.WriteAllBytes(filename, bytes);
AssetDatabase.ImportAsset("Assets/" + textureName + ".png");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment