Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Create a Texture2D from scratch, and modify it slightly each frame.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class dynamicTexture : MonoBehaviour
{
public int width=32;
public int height=32;
void Start()
{
Texture2D texture = new Texture2D(width,height);
var pixels = texture.GetPixels32(0);
for (int j=0; j < height; j++)
{
for (int i=0; i < width; i++)
{
pixels[i+j*width] = new Color(j/(float)height,i/(float)width,0,0) + Random.ColorHSV()/10f;
}
}
texture.SetPixels32(pixels);
GetComponent<Renderer>().material.mainTexture = texture;
texture.Apply();
}
void Update()
{
Texture2D texture = (Texture2D) GetComponent<Renderer>().material.mainTexture;
texture.SetPixel(Random.Range(0,width), Random.Range(0,height), Color.black);
texture.Apply();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.