Hello. I’m just starting a project that requires dynamically writing to textures. The animations currently come out incorrectly, as if they’re zoomed way in. I’ve simplified the issue into this script. It is intended to dynamically add a black texture to an object then draw a white diagonal line. I’m simply setting all pixels black except for where x = y, which is white. I’m stumped why this doesn’t generate a simple diagonal line on a quad. Does anyone see the issues? Thanks!
using UnityEngine;
using System.Collections;
public class DrawDiagonal : MonoBehaviour {
Texture2D texture2D;
int size = 256;
int x, y;
// Use this for initialization
void Start () {
x = 0;
y = 0;
texture2D = new Texture2D(size, size);
//for some reason I have to use Texture2D.blackTexture as well as set pixels to black individually
texture2D = Texture2D.blackTexture;
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
texture2D.SetPixel(i, j, Color.black);
}
}
texture2D.Apply ();
renderer.material.mainTexture = texture2D;
}
void Update () {
if (Time.frameCount % 30 == 0 && x < size) {
x++;
y++;
texture2D.SetPixel(x, y, Color.white);
texture2D.Apply();
}
}
}