简体   繁体   English

当鼠标悬停在统一的游戏对象上时,鼠标悬停不会改变材质

[英]Mouse over is not changing the material when hovering over game object in unity

I want to be able to hover over certain game objects and the object changes material.我希望能够将鼠标悬停在某些游戏对象上,并且该对象会更改材质。 I cannot figure out why my game object won't change material when I hover over it.当我将鼠标悬停在它上面时,我无法弄清楚为什么我的游戏对象不会改变材质。 I have made sure and added the materials in the inspector.我已经确定并在检查员中添加了材料。 I have tried multiple ways to get it to work but still no luck.我尝试了多种方法来让它工作,但仍然没有运气。 I am using unity 2018.4.16.我正在使用统一 2018.4.16。 I have tried using different examples and looked at the documentation, but alas I am stuck in the water.我尝试使用不同的示例并查看文档,但可惜我被困在水中。 Any help would be very appreciated.任何帮助将不胜感激。

        public Material startColor;
        public Material mouseOverColor;
        Renderer rend;

        void OnMouseOver()
        {
            rend.sharedMaterial = mouseOverColor;
        }

        void OnMouseExit()
        {
            rend.sharedMaterial = startColor;
        }

        // Start is called before the first frame update
        void Start()
        {
            rend = GetComponent<Renderer>();
            rend.enabled = true;
            rend.sharedMaterial = startColor;
        }

I ended up finding a solution.我最终找到了解决方案。 I needed to add a "Rigidbody" component and then checking "Is Kinematic" in the inspector as well as adding a "mesh collider" component, and now it works for anyone who is having a similar issue.我需要添加一个“刚体”组件,然后在检查器中检查“Is Kinematic”以及添加一个“mesh collider”组件,现在它适用于遇到类似问题的任何人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM