Created
February 26, 2020 12:54
-
-
Save chrisyarbrough/495a8a1b52f1120ac859c79b48cb08ec to your computer and use it in GitHub Desktop.
ScriptableWizard to create random noise images in Unity
This file contains hidden or 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
#if UNITY_EDITOR | |
using System.IO; | |
using UnityEditor; | |
using UnityEngine; | |
public class ImageGenerator : ScriptableWizard | |
{ | |
[MenuItem("Nementic/Utility/Image Generator...")] | |
public static void CreateWizard() | |
{ | |
ScriptableWizard.DisplayWizard<ImageGenerator>("Image Generator"); | |
} | |
public int count = 10; | |
public int width = 512; | |
public int height = 512; | |
public string fileNameInProject = "Resources/Texture_"; | |
private void OnValidate() | |
{ | |
if (count < 1) | |
count = 1; | |
if (width < 1) | |
width = 1; | |
if (height < 1) | |
height = 1; | |
} | |
private void OnWizardCreate() | |
{ | |
Generate(); | |
} | |
public void Generate() | |
{ | |
try | |
{ | |
AssetDatabase.StartAssetEditing(); | |
var texture = new Texture2D(width, height); | |
var pixels = new Color32[width * height]; | |
string basePath = Path.Combine(Application.dataPath, fileNameInProject); | |
for (int i = 0; i < count; i++) | |
{ | |
if (EditorUtility.DisplayCancelableProgressBar("Image Generator", "Image " + i, (i + 1) / (float)count)) | |
break; | |
for (int j = 0; j < pixels.Length; j++) | |
pixels[j] = RandomColor(); | |
texture.SetPixels32(pixels); | |
texture.Apply(); | |
var bytes = texture.EncodeToPNG(); | |
string path = basePath + i + ".png"; | |
File.WriteAllBytes(path, bytes); | |
} | |
} | |
finally | |
{ | |
EditorUtility.ClearProgressBar(); | |
AssetDatabase.StopAssetEditing(); | |
} | |
} | |
private static Color32 RandomColor() | |
{ | |
return new Color32( | |
(byte)(Random.value * 255), | |
(byte)(Random.value * 255), | |
(byte)(Random.value * 255), | |
255); | |
} | |
} | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment