繁体   English   中英

Unity 引用场景中的所有图像而不是一个

[英]Unity referencing all images in scene instead of just one

当我将鼠标悬停在图像上时,我试图更改图像的颜色。 它运行PointerEnter当我在它悬停,并PointerExit我的鼠标消失的时候。

它不仅改变了我悬停的图像的颜色,还改变了场景中每个图像的颜色。 有人可以帮忙吗?

这是我的代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class click : MonoBehaviour {

    void Start(){
        
    }

    void Update(){
        
    }
    
    public void PointerEnter(){
        gameObject.GetComponent<Image>().material.color=new Color(1,1,0);
    }
    
    public void PointerExit(){
        gameObject.GetComponent<Image>().material.color=new Color(1,1,1);
    }

}

共享材料以进行优化,它与所有使用它的图像所用的材料相同。

您需要更改图像的颜色,只需删除线条上的材料即可。

gameObject.GetComponent<Image>().color=new Color(1,1,0);

Button 组件具有过渡设置,您可以在其中设置状态颜色,也许您不需要额外的代码。

Unity 有 2 个预编程函数,称为OnMouseOver()OnMouseExit() 您可以使用它来查看鼠标是否进入了您的Image

您将需要此代码段添加到每个Image你想要的Color上悬停改变,并确保你得到的.Color组件替代的.material.color组件。

例子:

public class OnMouseOverExample : MonoBehaviour
{
    void OnMouseOver() {
        // Executes when you Hover over this GameObject with the Mouse
        gameObject.GetComponent<Image>().color = new Color(1f, 1f, 0f);
    }

    void OnMouseExit() {
        // Executes when you no longer Hover over this GameObject with the Mouse
        gameObject.GetComponent<Image>().color = new Color(1f, 1f, 1f);
    }
}

如果要更改 UI 组件Color ,也可以使用Button组件。 它已经内置了功能,您可以用来为图像的Color着色。

暂无
暂无

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

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