Skip to content

Instantly share code, notes, and snippets.

@davepape
Created November 19, 2019 18:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davepape/58540e68adab1cc5d1cd81a57f2e66b2 to your computer and use it in GitHub Desktop.
Save davepape/58540e68adab1cc5d1cd81a57f2e66b2 to your computer and use it in GitHub Desktop.
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