Hello all.
I’m very new to sharers in general so I’m wondering if I could get a bit of help in writing a shader that’ll turn a color array into a view able texture for me to use with Graphics.DrawTexture in the Editor.
I’ve done a bit of searching but haven’t come up with anything yet. I’m assuming this method will be quicker than creating a new texture every time I update the array.
The method I’m using currently to get an x/y position in the array is “y * width + x” if that helps.
I’m guessing you’d also need to pass the texture width into the shader.
Best of luck, I’m hoping that this is an easy one to anybody with a knowledge of shaders.
Hey there,
I’m not quite sure if using shaders is the right usecase for that. If you want to create textures, I guess using a script will be a more viable solution. I made a simple example on how such a script could look like (Using scriptable objects to keep the settings for different textures nicely coupled):
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="TextureCreator.cs" company="Supyrb">
// Copyright (c) 2017 Supyrb. All rights reserved.
// </copyright>
// <author>
// Johannes Deml
// send@johannesdeml.com
// </author>
// --------------------------------------------------------------------------------------------------------------------
using System.IO;
using UnityEngine;
using UnityEditor;
namespace Supyrb
{
public class TextureCreator : ScriptableObject
{
public string FilePath = "GeneratedTexture.png";
public int Width = 8;
public int Height = 1;
public Color[] Colors = new Color[8];
[ContextMenu("Create Texture")]
public void CreateTexture()
{
var texture = new Texture2D(Width, Height, TextureFormat.ARGB32, false);
for (int y = 0; y < texture.height; y++)
{
for (int x = 0; x < texture.width; x++)
{
texture.SetPixel(x, y, GetColorValue(x, y));
}
}
texture.Apply();
// Encode texture into PNG
byte[] bytes = texture.EncodeToPNG();
File.WriteAllBytes(FilePath, bytes);
AssetDatabase.Refresh();
}
public Color GetColorValue(int x, int y)
{
var index = Width*y + x;
if (index >= Colors.Length)
{
return Color.black;
}
return Colors[index];
}
void Reset()
{
FilePath = Application.dataPath + "/GeneratedTexture.png";
}
}
}
This is the resulting scriptable object. When you click create texture a png will be saved to the defined file path.

This is rather simple, but I guess it gives you an idea on where to go with creating textures.
Nice script, but I actually do need a shader so that I can render the colors on screen without any of the overhead of creating textures. Thanks for the help though.