【发布时间】:2017-12-18 19:17:48
【问题描述】:
您好,我是 Unity 新手,我正在尝试让 UI 按钮在鼠标悬停时更改其源图像,当鼠标不再悬停在按钮上时,按钮的源图像恢复正常.我知道需要一个精灵作为按钮的源图像,所以我创建了两个图像精灵文件(一个是普通图像,另一个是鼠标悬停在按钮上时亮起的图像)
现在这里是播放按钮的屏幕截图,具有正常的源图像
当鼠标移过按钮时,按钮会通过点亮来改变它的源图像
如何在 C# 中执行此任务?当鼠标悬停在 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;
}
}
【问题讨论】:
-
按钮默认行为在突出显示时已经点亮了图像。
标签: c# user-interface button unity3d unity3d-2dtools