C# 把红线内的东西都画成红色
我在unity中有一个堆栈溢出错误。 这将使红线内的所有东西都变成红色。有办法解决这个问题吗?还是有更好的办法让红线内的一切都变成红色C# 把红线内的东西都画成红色,c#,unity3d,pixels,C#,Unity3d,Pixels,我在unity中有一个堆栈溢出错误。 这将使红线内的所有东西都变成红色。有办法解决这个问题吗?还是有更好的办法让红线内的一切都变成红色 void Checkpoint(Texture2D tex, int x, int y){ if (tex.GetPixel(x,tex.height-y).r == 1) return; tex.SetPixel(x,tex.height-y,Color.red); Checkpoint(tex,x+1,y); Check
void Checkpoint(Texture2D tex, int x, int y){
if (tex.GetPixel(x,tex.height-y).r == 1) return;
tex.SetPixel(x,tex.height-y,Color.red);
Checkpoint(tex,x+1,y);
Checkpoint(tex,x,y+1);
Checkpoint(tex,x-1,y);
Checkpoint(tex,x,y-1);
return;
}
我想把红线内的一切都变成红色
可能是以下三种情况中的一种或多种: 1-与int
1
的浮点比较总是返回false。使用:
2-您不测试纹理的限制,因此x
和y
增长到+无穷大和-无穷大,因为GetPixel
超出限制不会返回任何红色:)
3-堆栈内存是一个有限的资源,如果你有一个大的纹理堆栈将不足以堆栈这么多的方法调用。改用System.Collections.Generic.Stack
我可以问你为什么要在Unity中实现洪水填充算法吗?我已经找到了方法。
这是我的coe
void Stack(int x, int y, Texture2D tex){
//make a list and add the first pixel
List<PT> toDo = new List<PT>();
toDo.Add(new PT(x,y,state));
while(toDo.Count != 0){
PT thisPoint = toDo[toDo.Count-1];
switch(thisPoint.State){
case 0:
//remove this pixel from the list when he is red
if (tex.GetPixel(thisPoint.X,tex.height-thisPoint.Y).r == 1f) {
toDo.RemoveAt(toDo.Count-1);
}else{
//change state and add the right pixel
tex.SetPixel(thisPoint.X,tex.height-thisPoint.Y,new Color(1,0,0));
toDo[toDo.Count-1].State = 1;
toDo.Add(new PT(thisPoint.X+1,thisPoint.Y,0));
}
break;
//add the pixel below this one
case 1:
toDo[toDo.Count-1].State = 2;
toDo.Add(new PT(thisPoint.X,thisPoint.Y+1,0));
break;
//add left pixel
case 2:
toDo[toDo.Count-1].State = 3;
toDo.Add(new PT(thisPoint.X-1,thisPoint.Y,0));
break;
//add the last pixel and remove himself
case 3:
toDo.RemoveAt(toDo.Count-1);
toDo.Add(new PT(thisPoint.X,thisPoint.Y-1,0));
break;
}
}
}
void堆栈(int x,int y,Texture2D tex){
//列出并添加第一个像素
List toDo=新列表();
toDo.Add(新PT(x,y,状态));
while(toDo.Count!=0){
PT thisPoint=toDo[toDo.Count-1];
开关(thisPoint.State){
案例0:
//当该像素为红色时,将其从列表中删除
if(tex.GetPixel(thisPoint.X,tex.height-thisPoint.Y).r==1f){
toDo.REMOVET(toDo.Count-1);
}否则{
//更改状态并添加正确的像素
tex.SetPixel(thisPoint.X,tex.height-thisPoint.Y,新颜色(1,0,0));
toDo[toDo.Count-1]。状态=1;
toDo.Add(新PT(thisPoint.X+1,thisPoint.Y,0));
}
打破
//将像素添加到这个下面
案例1:
toDo[toDo.Count-1]。状态=2;
toDo.Add(新PT(thisPoint.X,thisPoint.Y+1,0));
打破
//添加左像素
案例2:
toDo[toDo.Count-1]。状态=3;
toDo.Add(新PT(该点X-1,该点Y,0));
打破
//添加最后一个像素并删除它自己
案例3:
toDo.REMOVET(toDo.Count-1);
toDo.Add(新PT(thisPoint.X,thisPoint.Y-1,0));
打破
}
}
}
谢谢你的帮助,但我找到了办法。
void Stack(int x, int y, Texture2D tex){
//make a list and add the first pixel
List<PT> toDo = new List<PT>();
toDo.Add(new PT(x,y,state));
while(toDo.Count != 0){
PT thisPoint = toDo[toDo.Count-1];
switch(thisPoint.State){
case 0:
//remove this pixel from the list when he is red
if (tex.GetPixel(thisPoint.X,tex.height-thisPoint.Y).r == 1f) {
toDo.RemoveAt(toDo.Count-1);
}else{
//change state and add the right pixel
tex.SetPixel(thisPoint.X,tex.height-thisPoint.Y,new Color(1,0,0));
toDo[toDo.Count-1].State = 1;
toDo.Add(new PT(thisPoint.X+1,thisPoint.Y,0));
}
break;
//add the pixel below this one
case 1:
toDo[toDo.Count-1].State = 2;
toDo.Add(new PT(thisPoint.X,thisPoint.Y+1,0));
break;
//add left pixel
case 2:
toDo[toDo.Count-1].State = 3;
toDo.Add(new PT(thisPoint.X-1,thisPoint.Y,0));
break;
//add the last pixel and remove himself
case 3:
toDo.RemoveAt(toDo.Count-1);
toDo.Add(new PT(thisPoint.X,thisPoint.Y-1,0));
break;
}
}
}