本文实例为大家分享了Unity通过代码修改按钮点击效果的具体代码,供大家参考,具体内容如下
效果:
创建一个脚本,挂载到按钮上,主要是实现鼠标移入、移出、按下、抬起等事件的接口
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using UnityEngine.EventSystems; public class ButtonChangeColor : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler, IPointerDownHandler,IPointerUpHandler { private Text m_Text; private Image image; public Sprite[] sprites; void Awake() { m_Text = transform.Find( "Text" ).GetComponent<Text>(); image = GetComponent<Image>(); } /// <summary> /// 鼠标移入事件 /// </summary> /// <param name="eventData"></param> public void OnPointerEnter(PointerEventData eventData) { image.sprite = sprites[1]; m_Text.color = Color.red; } /// <summary> /// 鼠标移出事件 /// </summary> /// <param name="eventData"></param> public void OnPointerExit(PointerEventData eventData) { image.sprite = sprites[0]; m_Text.color = Color.black; } /// <summary> /// 鼠标按下事件 /// </summary> /// <param name="eventData"></param> public void OnPointerDown(PointerEventData eventData) { image.sprite = sprites[2]; m_Text.color = Color.white; } /// <summary> /// 鼠标抬起事件 /// </summary> /// <param name="eventData"></param> public void OnPointerUp(PointerEventData eventData) { image.sprite = sprites[0]; m_Text.color = Color.black; } } |
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/qq_43511290/article/details/115878261