Unity通過代碼修改按鈕點擊效果
更新時間:2021年04月21日 10:24:30 作者:柚子味的小檸檬
這篇文章主要為大家詳細介紹了Unity通過代碼修改按鈕點擊效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了Unity通過代碼修改按鈕點擊效果的具體代碼,供大家參考,具體內(nèi)容如下
效果:

創(chuàng)建一個腳本,掛載到按鈕上,主要是實現(xiàn)鼠標移入、移出、按下、抬起等事件的接口
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;
}
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
相關(guān)文章
C# 解決datagridview控件顯示大量數(shù)據(jù)拖拉卡頓問題
這篇文章主要介紹了C# 解決datagridview控件顯示大量數(shù)據(jù)拖拉卡頓問題,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01
C#設(shè)置WinForm中DataGrid列的方法(列寬/列標題等)
這篇文章主要介紹了C#設(shè)置WinForm中DataGrid列的方法,包括列寬、列標題等部分,并分析了其中相關(guān)的操作技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-07-07
C#設(shè)計模式之Builder生成器模式解決帶老婆配置電腦問題實例
這篇文章主要介紹了C#設(shè)計模式之Builder生成器模式解決帶老婆配置電腦問題,簡單介紹了生成器模式的概念、功能并結(jié)合具體實例形式分析了C#生成器模式解決配電腦問題的步驟與相關(guān)操作技巧,需要的朋友可以參考下2017-09-09
C#判斷頁面中的多個文本框輸入值是否有重復(fù)的實現(xiàn)方法
這篇文章主要介紹了C#判斷頁面中的多個文本框輸入值是否有重復(fù)的實現(xiàn)方法,是一個非常簡單實用的技巧,需要的朋友可以參考下2014-10-10

