2
0
Fork 0
You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

42 lines
1.3 KiB
C#

using System.IO;
using UnityEditor;
using UnityEngine;
public class ExampleEditorScript : MonoBehaviour
{
[MenuItem("Tools/PalletteTexture")]
static void CreateTexture3D()
{
int size = 256;
int sizeSub = size - 1;
TextureFormat format = TextureFormat.RGBA32;
TextureWrapMode wrapMode = TextureWrapMode.Clamp;
Texture2D texture = new Texture2D(size, size, format, false);
texture.wrapMode = wrapMode;
var size2D = size * size;
Color[] colors = new Color[size2D];
for (int i = 0; i < size2D; i++)
{
colors[i] = Color.clear;
}
var image = Resources.Load<Texture2D>("Pallette/palette");
var pixels = image.GetPixels(); //must be size
for (int i = 0; i < size; i++)
{
var color = pixels[i];
var x = color.r * sizeSub;
var y = color.b * sizeSub;
var cood = (int)(x * size + y);
var value = i * 1f / size;
colors[cood] = new Color(value, value, value, 1);
}
texture.SetPixels(colors);
texture.Apply();
byte[] bytes = texture.EncodeToPNG();
var dirPath = Application.dataPath + "/Resources/Pallette/";
File.WriteAllBytes(dirPath + "PalletteTex.png", bytes);
}
}