Created
December 11, 2020 22:27
-
-
Save jonbro/640df784bab92e6671b75830786a8ddc to your computer and use it in GitHub Desktop.
Editor Script that generates a single color texture
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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