Skip to content

Instantly share code, notes, and snippets.

@James-Frowen
Last active July 14, 2020 21:35
Show Gist options
  • Save James-Frowen/4b739cc6ec5358142d6e37566049a5ee to your computer and use it in GitHub Desktop.
Save James-Frowen/4b739cc6ec5358142d6e37566049a5ee to your computer and use it in GitHub Desktop.
using System.IO;
using UnityEditor;
using UnityEngine;
namespace JamesFrowen.EditorScripts
{
public static class SpriteMaker
{
[MenuItem("Tools/Sprite Maker/Make Circle 256x256")]
public static void MakeCircle()
{
string path = EditorUtility.SaveFilePanel("Save Circle 256x256", Application.dataPath, "Circle", "png");
if (string.IsNullOrEmpty(path))
{
return;
}
int radius = 128;
float sqRadius = radius * radius;
Texture2D texture = new Texture2D(radius * 2, radius * 2);
for (int x = 0; x < texture.width; x++)
{
for (int y = 0; y < texture.height; y++)
{
float dx = x - radius;
float dy = y - radius;
bool inCircle = (dx * dx + dy * dy) < sqRadius;
Color color = inCircle ? Color.white : Color.clear;
texture.SetPixel(x, y, color);
}
}
byte[] bytes = texture.EncodeToPNG();
File.WriteAllBytes(path, bytes);
AssetDatabase.Refresh();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment