【问题标题】:Highlighting neighboring hexagons on mouseover在鼠标悬停时突出显示相邻的六边形
【发布时间】:2018-10-23 12:28:13
【问题描述】:

过去一天,我一直在尝试将我的 love2D 游戏移植到 unity 中,但目前我遇到了一个主要问题:我无法复制邻居突出显示。

问题是: 如果我从一个六边形悬停到另一个六边形,如果我去过的六边形与前一个六边形共享邻居,则这些邻居将不会突出显示。

这是我突出显示邻居的代码:

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

【问题讨论】:

    标签: c# unity3d hexagonal-tiles


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-03-17
      • 1970-01-01
      • 1970-01-01
      • 2023-03-23
      • 2011-11-04
      • 1970-01-01
      相关资源
      最近更新 更多