在鼠标悬停时突出显示相邻的六边形

时间:2018-05-13 20:14:24

标签: c# unity3d hexagonal-tiles

我过去一直试图将我的love2D游戏移植到一起,我现在遇到了一个主要问题:我无法复制邻居突出显示。

问题是: 如果我从一个六边形悬停到另一个六边形,如果六边形我去过前六边形的邻居,那些邻居就不会被突出显示。

这是我突出邻居的代码:

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(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;
        }
    }

1 个答案:

答案 0 :(得分:0)

通过在我更改.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;
        }
    }