- Home /
Generate a gradient texture from editor script
Hi does somebody know how to generate a gradient PNG from a editor script? Whats the formula behind it?
I've looked around but nothing worked :/
This generates a texture using UnityEngine; using System.Collections; using UnityEditor; using System.IO;
public class GenGradient : EditorWindow {
public Color fromm;
public Color too;
public float tt;
[MenuItem ("Window/Gen Texture")]
// Use this for initialization
static void Init(){
GenGradient window = (GenGradient)EditorWindow.GetWindow(typeof(GenGradient));
window.maxSize = new Vector2(300,200);
}
void OnGUI(){
if(GUILayout.Button("Gen")){
GenTexture();
}
fromm = EditorGUILayout.ColorField(fromm);
too = EditorGUILayout.ColorField(too);
tt = EditorGUILayout.Slider(tt,0,100);
}
void GenTexture(){
Texture2D texture = new Texture2D(64,64,TextureFormat.RGB24,false);
Color[] colors = new Color[texture.width];
for(int i = 0; i < texture.width; i++){
//for(int y = 0; y <texture.height; y++){
colors[i].r = (i/225)*1000;//Linear((i/64)*100);
Debug.Log("Value"+(i/225f)*1000f);//Linear((i/64)*100));
texture.SetPixel(i,1,colors[i]);
//}
}
texture.Apply();
var bytes = texture.EncodeToPNG();
File.WriteAllBytes("Assets/gui/swag.png",bytes);
//AssetDatabase.CreateAsset(bytes,"Assets/hi.png");
//AssetDatabase.CreateAsset(bytes,"Assets/grad.png");
AssetDatabase.Refresh();
}
public float Linear (float per) {
return fromm.r * per + too.r * (1-per);
}
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220613102803im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Are textured/z-clipped gizmos possible? 0 Answers
Overall quality became worse after changing to Windows from Mac 1 Answer
Gradient Changes in Editor Reset In Play Mode. Why? 1 Answer
How to check if a Gradient in Editor has been changed. 1 Answer
How can you tell if a texture is NPOT or not, and what its NPOT import settings are 1 Answer