C# 鼠标悬停时高亮显示相邻六边形
在过去的一天里,我一直在尝试将我的love2D游戏移植到unity中,现在我遇到了一个主要问题:我无法复制邻居的高亮显示 问题是: 如果我从一个六边形悬停到另一个六边形上,如果我去的六边形共享前一个六边形的邻居,这些邻居将不会高亮显示 以下是我突出显示邻居的代码:C# 鼠标悬停时高亮显示相邻六边形,c#,unity3d,hexagonal-tiles,C#,Unity3d,Hexagonal Tiles,在过去的一天里,我一直在尝试将我的love2D游戏移植到unity中,现在我遇到了一个主要问题:我无法复制邻居的高亮显示 问题是: 如果我从一个六边形悬停到另一个六边形上,如果我去的六边形共享前一个六边形的邻居,这些邻居将不会高亮显示 以下是我突出显示邻居的代码: foreach(Tile tile in hex.neighbours){ tile.GetComponent<Renderer>().material.color=tile.data.Elements[tile.
foreach(Tile tile in hex.neighbours){
tile.GetComponent<Renderer>().material.color=tile.data.Elements[tile.data.Element];
if (on){ // are we highlighting?
print("Highlighting..");
if(tile.GetComponent<Renderer>().material.color==tile.data.Elements[tile.data.Element] ){
//if the color of the neighbors is the same as it's element's color(meaning it hasn't been highlighted yet), highlight it.
print("changing color!");
tile.GetComponent<Renderer>().material.color=tile.GetComponent<Renderer>().material.color+new Color32(20,20,20,0);
}
}
else{
//unhightlighting
if(tile.GetComponent<Renderer>().material.color!=tile.data.Elements[tile.data.Element]){
//if the color of the neighbors isn't the same as it's element's color(meaning it's highlighted), unhighlight it.
tile.GetComponent<Renderer>().material.color=tile.data.Elements[tile.data.Element];
}
}
}
foreach(十六进制的瓷砖){
tile.GetComponent().material.color=tile.data.Elements[tile.data.Element];
如果(打开){//我们正在突出显示吗?
打印(“突出显示…”);
if(tile.GetComponent().material.color==tile.data.Elements[tile.data.Element]){
//如果相邻元素的颜色与其元素的颜色相同(表示尚未高亮显示),则高亮显示该元素。
打印(“改变颜色!”);
tile.GetComponent().material.color=tile.GetComponent().material.color+新颜色32(20,20,20,0);
}
}
否则{
//暗照明
if(tile.GetComponent().material.color!=tile.data.Elements[tile.data.Element]){
//如果相邻元素的颜色与其元素的颜色不同(表示高亮显示),请取消高亮显示。
tile.GetComponent().material.color=tile.data.Elements[tile.data.Element];
}
}
}
以下是我检查是否应高亮显示或取消高亮显示的方法:
foreach(KeyValuePair<GameObject,Hex> h in HexData){
if (hovering==null && h.Value.hovering){
h.Value.hovering=false;
//if we're not hovering over anything and a hexagon still has it's neighbors highlighted, unhighlight it's neighbors
if (h.Value.neighborsHighlighted==true){
highlightNeighbors(false,h.Value);
h.Value.neighborsHighlighted=false;
}
}
if(!h.Value.hovering && h.Value.neighborsHighlighted){
{
//if a hexagon isn't being hovered over, but it's neighbors are still hightlighed, unhighlight them.
highlightNeighbors(false,h.Value);
h.Value.neighborsHighlighted=false;
};
}
if (h.Value.hovering && h.Value.neighborsHighlighted==false){
//if we're hovering over a hexagon and it's neighbors aren't highlighted, highlight them
highlightNeighbors(true,h.Value);
h.Value.neighborsHighlighted=true;
}
}
foreach(HexData中的KeyValuePair h){
if(悬停==null&&h.Value.hover){
h、 值。悬停=false;
//如果我们没有悬停在任何东西上,六边形仍然高亮显示其邻居,则取消高亮显示其邻居
if(h.Value.NeightarsHighlighted==true){
HighlightNeights(假,h值);
h、 Value.nextershighlighted=false;
}
}
如果(!h.Value.hovering&&h.Value.neighbors高亮显示){
{
//如果一个六边形没有悬停,但它的邻居们仍然很高,请取消它们的高光。
HighlightNeights(假,h值);
h、 Value.nextershighlighted=false;
};
}
if(h.Value.hovering&&h.Value.neightarsphighted==false){
//如果我们在一个六边形上悬停,而它的邻居没有高亮显示,请高亮显示它们
HighlightNeights(真,h值);
h、 Value.nextershighlighted=true;
}
}
通过在我更改.hovering属性/变量的代码位中高亮显示和取消高亮显示,修复了此问题
if(Physics.Raycast(ray, out hit))
{
if (hit.collider.name.Contains("Hex"))
{
hovering=hit.collider.gameObject;
foreach(KeyValuePair<GameObject,Hex> h in HexData){
if (h.Key==hovering){
highlightNeighbors(true,h.Value);
h.Value.hovering=true;
}
else if(h.Key!=hovering && h.Value.hovering==true){
highlightNeighbors(false,h.Value);
h.Value.hovering=false;
}
}
}
}
else{
if (hovering){
hovering=null;
}
}
if(Physics.Raycast(光线,出击))
{
if(hit.collider.name.Contains(“十六进制”))
{
悬停=hit.collider.gameObject;
foreach(HexData中的KeyValuePair h){
if(h.Key==悬停){
HighlightNeights(真,h值);
h、 Value.hovering=true;
}
else if(h.Key!=悬停&&h.Value.hovering==true){
HighlightNeights(假,h值);
h、 值。悬停=false;
}
}
}
}
否则{
如果(悬停){
悬停=空;
}
}