UGUI获取Button的按下,抬起,离开等事件

本文介绍如何在Unity中使用两种不同方法绑定按钮点击事件:一是直接通过脚本的方法,二是利用EventTrigger组件来响应多种鼠标事件。

1. 通过直接绑定脚本来绑定事件

1 : 建一个ButtonTest脚本,挂在一个游戏对象上(里面的方法public或private都可以).

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ButtonTest : MonoBehaviour {
    
    private GameObject Obj;
    private void Start()
    {
        Obj = GameObject.Find("Button");
        Obj.GetComponent<Button>().onClick.AddListener(Func1);
        Obj.GetComponent<Button>().onClick.AddListener(Func2);
    }
    void Func1()
    {
        print("执行了Func1方法!");
    }
    public void Func2()
    {
        print("执行了Func2方法!");
    }
}

2 : 运行,点击按钮执行两个方法.

Obj.GetComponent<Button>().onClick.AddListener(Func1);可以换成
Obj.GetComponent<Button>().onClick.AddListener
     (
          delegate () {Func1(); }
     );

3. 通过 EventTrigger 实现按钮点击事件(一) 

1 : 创建一个ButtonClickTest 脚本, 利用 UnityEngine.EventSystems.EventTrigger 添加监听事件.

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


// 此请求为自动添加 EventTrigger 脚本
[RequireComponent(typeof(UnityEngine.EventSystems.EventTrigger))]
public class ButtonTest : MonoBehaviour
{

    // Use this for initialization
    void Start()
    {
        Button  _button = this.GetComponent<Button>();
        EventTrigger  _trigger = _button.gameObject.GetComponent<EventTrigger>();
        EventTrigger.Entry en1 = new EventTrigger.Entry();
        EventTrigger.Entry en2 = new EventTrigger.Entry();
        EventTrigger.Entry en3 = new EventTrigger.Entry();

        EventTrigger.Entry en4 = new EventTrigger.Entry();

        EventTrigger.Entry en5 = new EventTrigger.Entry();



        // 鼠标点击事件
        en1.eventID = EventTriggerType.PointerClick;
        // 鼠标进入事件 
        en2.eventID = EventTriggerType.PointerEnter;
        // 鼠标滑出事件 
        en3.eventID = EventTriggerType.PointerExit;

        en4.eventID = EventTriggerType.PointerDown;

        en5.eventID = EventTriggerType.PointerUp;



        en1.callback = new EventTrigger.TriggerEvent();
        en1.callback.AddListener(Func1);
        _trigger.triggers.Add(en1);


        en2.callback = new EventTrigger.TriggerEvent();
        en2.callback.AddListener(Func2);
        _trigger.triggers.Add(en2);


        en3.callback = new EventTrigger.TriggerEvent();
        en3.callback.AddListener(Func3);
        _trigger.triggers.Add(en3);

        

         en4.callback = new EventTrigger.TriggerEvent();
        en4.callback.AddListener(Func4);
        _trigger.triggers.Add(en4);



         en5.callback = new EventTrigger.TriggerEvent();
        en5.callback.AddListener(Func5);
        _trigger.triggers.Add(en5);




    }
    private void Func1(BaseEventData pointData)
    {
        Debug.Log("鼠标点击了!");
    }
    private void Func2(BaseEventData pointData)
    {
        Debug.Log("鼠标进入了!");
    }
    private void Func3(BaseEventData pointData)
    {
        Debug.Log("鼠标滑出了!");
    }

    private void Func4(BaseEventData pointData)
    {
        Debug.Log("按下");
    }

    private void Func5(BaseEventData pointData)
    {
        Debug.Log("抬起");
    }


}


2 :把脚本挂在Button对象上,然后Unity会给Button游戏对象再挂一个EventTrigger脚本.

 3 :运行.


4. 通过 EventTrigger 实现按钮点击事件(二)

 1 :写一个脚本挂在一个游戏对象,例如相机上(和按钮关联的事件方法要声明为public).

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


public class ButtonTest : MonoBehaviour
{
    // Use this for initialization
    void Start()
    {
    }
    public void Func1()
    {
        Debug.Log("鼠标点击了!");
    }
    public void Func2()
    {
        Debug.Log("鼠标进入了!");
    }
    public void Func3()
    {
        Debug.Log("鼠标滑出了!");
    }
    public void Func4()
    {
        Debug.Log("鼠标按下了!");
    }
    public void Func5()
    {
        Debug.Log("鼠标抬起了!");
    }
}

2 :给Button游戏对象添加EventTrigger组件.

 3 :在组件里添加对应的事件,选择对应的方法.


4 :运行,鼠标在按钮上运动,点击


<think>我们正在讨论Unity UGUI Button组件,特别是如何修改按钮按下状态的颜色。根据用户的问题,他想要修改按钮在按下状态(Pressed)的默认颜色。 在Unity UGUI中,Button组件有一个“Transition”属性,用于控制按钮在不同状态(Normal, Highlighted, Pressed, Disabled)下的外观变化。其中一种过渡类型是“Color Tint”,即通过颜色变化来反映状态变化。 修改按下状态颜色的步骤如下: 1. 在Unity编辑器中,选择需要修改的Button对象。 2. 在Inspector窗口中找到Button组件,其中有一个“Transition”选项,默认为“Color Tint”。 3. 在“Color Tint”设置中,可以看到“Pressed Color”属性,这就是按钮按下时显示的颜色。 4. 点击“Pressed Color”旁边的颜色块,选择你希望的颜色。 此外,我们还可以通过代码来修改。但是用户的问题更偏向于编辑器操作,所以重点放在编辑器操作上。 但是,根据引用[2]和[3],我们也可以看到Button组件的事件监听是通过代码添加的。如果用户需要动态修改颜色,也可以通过代码实现,例如: button.colors = 一个ColorBlock结构体 不过,用户的问题明确是修改“默认按下交互颜色”,所以编辑器操作是最直接的。 另外,引用[1]是一篇关于UGUI Button组件详解的文章,其中应该包含颜色修改的部分,我们可以参考。 因此,回答如下:</think>### Unity UGUI Button 按下状态颜色修改方法 在 Unity UGUI 中修改 Button 按下状态的颜色,可以通过以下两种方式实现: #### 🎨 方法一:编辑器可视化修改(推荐) 1. **选择 Button 对象** 在 Hierarchy 窗口中选中需要修改的 Button 对象。 2. **修改颜色参数** 在 Inspector 窗口中定位到 Button 组件: - 找到 `Transition` 属性,确保设置为 **Color Tint** - 展开 `Colors` 折叠菜单 - 点击 `Pressed Color` 右侧的颜色块(默认为深蓝色) - 在拾色器中选择新颜色(例如红色) ```markdown [Button 组件参数示例] Transition: Color Tint Normal Color: [当前颜色] Highlighted Color: [当前颜色] Pressed Color: [修改为目标颜色] 👈 关键参数 Disabled Color: [当前颜色] ``` 3. **即时测试** 点击 Unity 编辑器顶部的 ▶️ 运行按钮,点击 Button 即可看到按下时的新颜色效果。 #### 💻 方法二:通过代码动态修改 若需运行时动态修改颜色,可在脚本中使用以下代码: ```csharp using UnityEngine; using UnityEngine.UI; public class ButtonColorChanger : MonoBehaviour { public Color pressedColor = Color.red; // 定义目标颜色 void Start() { Button button = GetComponent<Button>(); ColorBlock colors = button.colors; // 获取当前颜色配置 colors.pressedColor = pressedColor; // 修改按下状态颜色 button.colors = colors; // 应用新配置 } } ``` 将此脚本挂载到 Button 对象上,在 Inspector 中可调整 `pressedColor` 字段值。 #### ⚠️ 注意事项 1. 若修改后无效果: - 检查 Button 的 `Transition` 模式需为 **Color Tint** - 确认未覆盖默认颜色(如 Image 组件单独设置了颜色) 2. 高级效果定制: - 如需纹理变化,可将 `Transition` 改为 **Sprite Swap** - 使用动画系统可创建更复杂的按压反馈[^1] --- ### 📚 相关问题 1. 如何为UGUI Button添加按下时的音效反馈? 2. 怎样通过代码批量修改场景中所有Button的按下颜色? 3. UGUI Button的四种交互状态(Normal/Hightlighted/Pressed/Disabled)分别对应哪些用户操作? 4. 如何实现Button按下时的缩放动画效果? [^1]: UnityUGUI Button按钮组件详解
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值