Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 当鼠标在UI按钮上时,是否更改其源图像?(团结)_C#_User Interface_Button_Unity3d_Unity3d 2dtools - Fatal编程技术网

C# 当鼠标在UI按钮上时,是否更改其源图像?(团结)

C# 当鼠标在UI按钮上时,是否更改其源图像?(团结),c#,user-interface,button,unity3d,unity3d-2dtools,C#,User Interface,Button,Unity3d,Unity3d 2dtools,您好,我是Unity的新手,我正在尝试让一个UI按钮在鼠标位于其上方时更改其源图像,当鼠标不再位于按钮上方时,按钮的源图像将恢复正常。我知道精灵必须是按钮的源图像,所以我创建了两个图像精灵文件(一个是普通图像,另一个是鼠标悬停在按钮上时亮起的图像) 下面是播放按钮的屏幕截图,它有正常的源图像 当鼠标移到按钮上时,按钮将点亮以更改其源图像 如何在C#中完成此任务?当鼠标悬停在按钮上方时,如何更改按钮的源图像 这是我看了一些参考资料后得到的。我是Unity的新手,很抱歉我缺乏知识 using S

您好,我是Unity的新手,我正在尝试让一个UI按钮在鼠标位于其上方时更改其源图像,当鼠标不再位于按钮上方时,按钮的源图像将恢复正常。我知道精灵必须是按钮的源图像,所以我创建了两个图像精灵文件(一个是普通图像,另一个是鼠标悬停在按钮上时亮起的图像)

下面是播放按钮的屏幕截图,它有正常的源图像

当鼠标移到按钮上时,按钮将点亮以更改其源图像

如何在C#中完成此任务?当鼠标悬停在按钮上方时,如何更改按钮的源图像

这是我看了一些参考资料后得到的。我是Unity的新手,很抱歉我缺乏知识

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

public class PlayButton : MonoBehaviour {

    private PlayButton pb;
    private Sprite newSprite;

    // Use this for initialization
    void Start () {
        pb = GetComponentInChildren<PlayButton> ();
    }

    // Update is called once per frame
    void Update () {

    }

    public void onClick(){

    }

    public void onPointerHover(PointerEventData eventData){
        //When mouse hovers over the button, the button changes
        pb.image.overrideSprite = newSprite;
    }
 }
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
使用UnityEngine.UI;
公共类播放按钮:MonoBehavior{
私人播放按钮pb;
私人雪碧;
//用于初始化
无效开始(){
pb=getComponentChildren();
}
//每帧调用一次更新
无效更新(){
}
公共void onClick(){
}
PointerHover上的公共无效(PointerEventData事件数据){
//当鼠标悬停在按钮上时,按钮会发生变化
pb.image.overrideSprite=newSprite;
}
}

没有什么是
onPointerHover
。要检测鼠标悬停,请使用
OnPointerEnter
。要从鼠标上方检测鼠标何时存在,请使用
OnPointerExit
。为了使用这些函数,您必须实现
IPInterexitHandler
IPInterenterHandler

你读了更多的例子

至于更改按钮的源图像,可以使用
Button.Image.sprite
Button.Image.overrideSprite
来执行此操作

只需将此按钮附加到Button对象:

using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;

public class PlayButton : MonoBehaviour, IPointerExitHandler, IPointerEnterHandler
{
    private Button pb;
    public Sprite newSprite;

    void Start()
    {
        pb = GetComponent<Button>();
    }

    public void OnPointerEnter(PointerEventData eventData)
    {
        pb.image.sprite = newSprite; ;
        Debug.Log("Mouse Enter");
    }

    public void OnPointerExit(PointerEventData eventData)
    {
        Debug.Log("Mouse Exit");
        //Change Image back to default?
    }
}
使用UnityEngine;
使用UnityEngine.UI;
使用UnityEngine.EventSystems;
公共类播放按钮:MonoBehavior、IPInterexitHandler、IPInterenterHandler
{
私人按钮pb;
公共雪碧;
void Start()
{
pb=GetComponent();
}
公共void OnPointerEnter(pointereventdataeventdata)
{
pb.image.sprite=newSprite;
Log(“鼠标输入”);
}
公共void OnPointerExit(pointereventdataeventdata)
{
Log(“鼠标退出”);
//是否将图像更改回默认值?
}
}
编辑

请注意,您不必自己做这件事。团结是实现这一目标的必由之路

1。将按钮的转换选项从“颜色色调”更改为“精灵交换”

2。将“突出显示的精灵”插槽更改为您想要的精灵


没有
onPointerHover
这样的东西。要检测鼠标悬停,请使用
OnPointerEnter
。要从鼠标上方检测鼠标何时存在,请使用
OnPointerExit
。为了使用这些函数,您必须实现
IPInterexitHandler
IPInterenterHandler

你读了更多的例子

至于更改按钮的源图像,可以使用
Button.Image.sprite
Button.Image.overrideSprite
来执行此操作

只需将此按钮附加到Button对象:

using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;

public class PlayButton : MonoBehaviour, IPointerExitHandler, IPointerEnterHandler
{
    private Button pb;
    public Sprite newSprite;

    void Start()
    {
        pb = GetComponent<Button>();
    }

    public void OnPointerEnter(PointerEventData eventData)
    {
        pb.image.sprite = newSprite; ;
        Debug.Log("Mouse Enter");
    }

    public void OnPointerExit(PointerEventData eventData)
    {
        Debug.Log("Mouse Exit");
        //Change Image back to default?
    }
}
使用UnityEngine;
使用UnityEngine.UI;
使用UnityEngine.EventSystems;
公共类播放按钮:MonoBehavior、IPInterexitHandler、IPInterenterHandler
{
私人按钮pb;
公共雪碧;
void Start()
{
pb=GetComponent();
}
公共void OnPointerEnter(pointereventdataeventdata)
{
pb.image.sprite=newSprite;
Log(“鼠标输入”);
}
公共void OnPointerExit(pointereventdataeventdata)
{
Log(“鼠标退出”);
//是否将图像更改回默认值?
}
}
编辑

请注意,您不必自己做这件事。团结是实现这一目标的必由之路

1。将按钮的转换选项从“颜色色调”更改为“精灵交换”

2。将“突出显示的精灵”插槽更改为您想要的精灵


突出显示时,按钮默认行为已点亮图像。突出显示时,按钮默认行为已点亮图像。