首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在鼠标悬停时突出显示相邻的六边形

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

Stack Overflow用户
提问于 2018-05-14 04:14:24
回答 1查看 260关注 0票数 0

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

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

下面是我用来突出显示邻居的代码:

代码语言:javascript
运行
复制
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];             
        }
    }
}

下面是我检查是否应该突出显示或取消突出显示的方法:

代码语言:javascript
运行
复制
    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;
        }
    }
EN

回答 1

Stack Overflow用户

发布于 2018-05-14 04:58:26

通过在更改.hovering属性/变量的代码中突出显示和取消突出显示修复了该问题

代码语言:javascript
运行
复制
    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;
        }
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50320252

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档