Skip to content

Instantly share code, notes, and snippets.

@Naphier
Created November 21, 2016 21:20
Show Gist options
  • Save Naphier/b5359faa5865ac7e1d02df9806b7f1a0 to your computer and use it in GitHub Desktop.
Save Naphier/b5359faa5865ac7e1d02df9806b7f1a0 to your computer and use it in GitHub Desktop.
Simple editor script that will create a 4x4 texture of the specified color. Useful for prototyping with sprites.
using UnityEngine;
using UnityEditor;
public class MakeSpriteTexture : EditorWindow
{
[MenuItem("Assets/Create Sprite")]
static void Init()
{
MakeSpriteTexture window = GetWindow<MakeSpriteTexture>("Create Sprite");
window.Show();
Rect winPos = window.position;
winPos.width = 210;
winPos.height = 100;
winPos.position = new Vector2(Screen.width * 0.5f, Screen.height * 0.5f);
window.position = winPos;
}
private Color color = Color.white;
private string relPath = "_Sprites/Colors";
void OnGUI()
{
GUILayout.Label("Select color the press create.", EditorStyles.boldLabel);
relPath = GUILayout.TextField(relPath);
color = EditorGUILayout.ColorField(color);
if (GUILayout.Button("Create"))
{
EnsurePath(relPath);
CreateTexture();
}
//GUILayout.Label(string.Format("({0}, {1})", position.width, position.height));
}
void EnsurePath(string path)
{
string fullPath = path;
if (fullPath[0] != '\\' || fullPath[0] != '/')
fullPath = "/" + fullPath;
path = path.Replace("\\", "/");
fullPath = Application.dataPath + fullPath;
string[] folders = path.Split('/');
if (!AssetDatabase.IsValidFolder("Assets/" + path))
{
Debug.LogFormat("Folder '{0}' is not valid. Creating path: {1}", fullPath, path);
string parent = "Assets";
for (int i = 0; i < folders.Length; i++)
{
if (i > 0)
parent += "/" + folders[i - 1];
string guid = AssetDatabase.CreateFolder(parent, folders[i]);
string newFolderPath = AssetDatabase.GUIDToAssetPath(guid);
AssetDatabase.Refresh();
Debug.LogFormat("parent: {0} folder: {1} newFolderPath: {2}", parent, folders[i], newFolderPath);
}
}
else
{
//Debug.LogFormat("Path: '{0}' is valid", path);
}
}
void CreateTexture()
{
Texture2D tex = new Texture2D(4, 4);
for (int i = 0; i < tex.width; i++)
{
for (int j = 0; j < tex.height; j++)
{
tex.SetPixel(i, j, color);
}
}
tex.Apply();
byte[] bytes = tex.EncodeToPNG();
string fileName = color.ToString().Replace("RGBA", "") + ".png";
string dbPath = "Assets/" + relPath + "/" + fileName;
string systemPath = Application.dataPath + "/" + relPath + "/";
System.IO.File.WriteAllBytes(systemPath + fileName, bytes);
Object.DestroyImmediate(tex);
AssetDatabase.Refresh();
TextureImporter tImport = (TextureImporter)AssetImporter.GetAtPath(dbPath);
if (tImport == null)
Debug.LogError("tImport sucks");
else
{
tImport.textureType = TextureImporterType.Sprite;
tImport.mipmapEnabled = false;
tImport.maxTextureSize = 32;
tImport.filterMode = FilterMode.Point;
AssetDatabase.ImportAsset(tImport.assetPath, ImportAssetOptions.ForceUpdate);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment