- Home /
Question by
AyakoKisuke · Feb 02, 2017 at 03:23 PM ·
guitexturerenderer
How To draw Line by DrawTexture method?
I used this Script to draw lines but I can see nothing on the screen and there are no issues appeared:
function Line (tex : Texture2D, x0 : int, y0 : int, x1 : int, y1 : int, col : Color) {
var dy = y1-y0;
var dx = x1-x0;
if (dy < 0) {dy = -dy; var stepy = -1;}
else {stepy = 1;}
if (dx < 0) {dx = -dx; var stepx = -1;}
else {stepx = 1;}
dy <<= 1;
dx <<= 1;
tex.SetPixel(x0, y0, col);
if (dx > dy) {
var fraction = dy - (dx >> 1);
while (x0 != x1) {
if (fraction >= 0) {
y0 += stepy;
fraction -= dx;
}
x0 += stepx;
fraction += dy;
tex.SetPixel(x0, y0, col);
}
}
else {
fraction = dx - (dy >> 1);
while (y0 != y1) {
if (fraction >= 0) {
x0 += stepx;
fraction -= dy;
}
y0 += stepy;
fraction += dx;
tex.SetPixel(x0, y0, col);
}
}
}
and this is the function call
function Start () {
var tex = new Texture2D (128, 128, TextureFormat.Alpha8, false);
GetComponent.<Renderer>().material.mainTexture = tex;
for (var i = 0; i < lines; i++)
{
Line(tex, Random.Range(0, texSize),
Random.Range(0, texSize),
Random.Range(0, texSize),
Random.Range(0, texSize),
Color(Random.Range(0.25, 1.0),
Random.Range(0.25, 1.0),
Random.Range(0.25, 1.0)) );
}
tex.Apply();
GetComponent.<Renderer>().material.mainTexture = tex;
}
Comment
Why don't you use LineRenderer? In Unity 5.5 LineRenderer'd been improved. Now its lines looks nice)
Yes Use LineRenderer Insted its a better way to draw line
Your answer
Follow this Question
Related Questions
Help with cameras and enabling/disabling GUItextures? 1 Answer
Gui Box - Texture - Spritesheet 0 Answers
GUI Texture error. 1 Answer