Skip to content

Instantly share code, notes, and snippets.

@Hotrian
Last active February 2, 2016 22:51
Show Gist options
  • Save Hotrian/3f2023678dd4b4c08f7e to your computer and use it in GitHub Desktop.
Save Hotrian/3f2023678dd4b4c08f7e to your computer and use it in GitHub Desktop.
Create a quad, prepare it for drawing, and edit some pixels. Remember use the Sprite Material to get the desired effect. This is just a barebones example on how to display and edit an arbitrary Texture2D.
using UnityEngine;
[RequireComponent(typeof(MeshRenderer)), RequireComponent(typeof(MeshFilter))]
public class RandomSprite : MonoBehaviour
{
private static float quadScale = 1f; // Size of our Quad in World Units
private static Vector3[] quadVertices = new Vector3[] { new Vector3(0f, 0f, 0f),
new Vector3(quadScale, 0f, 0f),
new Vector3(0f, quadScale, 0f),
new Vector3(quadScale, quadScale, 0f)};
private static int[] quadIndices = new int[] { 0, 3, 1, 0, 2, 3 };
private static Vector2[] quadUVs = new Vector2[] { new Vector2(0f, 0f),
new Vector2(1f, 0f),
new Vector2(0f, 1f),
new Vector2(1f, 1f)};
private int width = 16; // The dimensions of our Texture in Pixels
private int height = 16;
MeshRenderer myMeshRenderer;
MeshFilter myMeshFilter;
Texture2D myTexture;
void Start()
{
// Get out references to our components
myMeshRenderer = GetComponent<MeshRenderer>();
myMeshFilter = GetComponent<MeshFilter>();
// Create a texture for this GameObject
myTexture = new Texture2D(width, height);
myTexture.filterMode = FilterMode.Point; // Don't blur our texture
myMeshRenderer.material.mainTexture = myTexture; // Apply our texture to our renderer
// Create a new mesh (so we have a shape to render!)
Mesh tMesh = new Mesh();
tMesh.vertices = quadVertices;
tMesh.triangles = quadIndices;
tMesh.uv = quadUVs;
myMeshFilter.mesh = tMesh; // Apply our mesh to our filter
// Clear our texture
for (int i = 0; i < width; i ++)
{
for (int j = 0; j < height; j++)
{
SetPixNoApply(i, j, new Color(0f, 0f, 0f, 0f));
}
}
ApplyTexture(); // Apply our changes
// Make some other changes to our texture
SetPixNoApply(1, 1, Color.red);
SetPixNoApply(2, 1, new Color(0f, 0f, 1f));
SetPixNoApply(3, 1, new Color(0f, 1f, 0f));
SetPixNoApply(4, 1, Color.red);
ApplyTexture(); // Apply those changes too
}
// This method is faster but you must call ApplyTexture() after calling this
void SetPixNoApply(int x, int y, Color c)
{
if (myTexture != null)
{
myTexture.SetPixel(x, y, c);
}
}
// This method applies changes to our texture
void ApplyTexture()
{
if (myTexture != null)
{
myTexture.Apply();
}
}
// This method is slower because it calls Apply() after every pixel change
// Only use this when changing just a few pixels at a time
// If you are changing more than one pixel at a time, you should use
// SetPixNoApply for all pixels, then call ApplyTexture() instead
void SetPix(int x, int y, Color c)
{
SetPixNoApply(x, y, c);
ApplyTexture();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment