I tested your code and it works with a few changes...
- Make sure that the texture you are modifying was originally imported with Read/Write enabled (under advanced settings in the Inspector).
- Be sure to call texture.Apply().
Here's an example showing the modifications I made...
//----------------------------------------------------------- using UnityEngine;
public class DrawLine : MonoBehaviour { public Texture2D MyTexture;
// Use this for initialization
void Start ()
{
Draw(0, 0, 100, 100);
renderer.material.mainTexture = MyTexture;
}
// Update is called once per frame
void Update ()
{
}
void Draw(float x1, float y1, float x2, float y2)
{
float x,y;
float dy = y2-y1;
float dx = x2-x1;
float m = dy/dx;
float dy_inc = -1;
if( dy < 0 )
dy = 1;
float dx_inc = 1;
if( dx < 0 )
dx = -1;
if( Mathf.Abs(dy) > Mathf.Abs(dx) )
{
for( y = y2; y < y1; y += dy_inc )
{
x = x1 + ( y - y1 ) * m;
Debug.Log("Setting Pixel at: x=" + x + ", y=" + y);
MyTexture.SetPixel((int)(x), (int)(y), Color.black);
}
}
else
{
for( x = x1; x < x2; x += dx_inc )
{
y = y1 + ( x - x1 ) * m;
Debug.Log("Setting Pixel at: x=" + x + ", y=" + y);
MyTexture.SetPixel((int)(x), (int)(y), Color.black);
}
}
MyTexture.Apply();
}
}
answered
Feb 20 at 09:14 PM
JamesT
2
●
3
●
4
●
5