Recommended reading

  • CSDN home page
  • GitHub open source address
  • Unity3D plug-in sharing
  • Jane’s address book
  • My personal blog
  • QQ group: 1040082875

One, foreword

Today share UGUI Button binding event of several methods, as well as the advantages and disadvantages of what do not understand the small friends can also contact my QQ, my QQ is hidden in the blog link, see can find it

Second, the body

Let’s write a response script for Button, buttontest.cs

using UnityEngine;
using UnityEngine.UI;

public class ButtonTest : MonoBehaviour
{
    public Text m_Text;
    public void ButtonOnClickEvent()
    {
        m_Text.text = "Mouse click"; }}Copy the code

I. Visual creation and event binding

Click the + sign OnClick on the Button component

Then assign the script bound object to the Button component

Bind events with direct binding scripts

Use the onclick.addListener method that comes with the Button component

Code:

using UnityEngine;
using UnityEngine.UI;

public class ButtonTest : MonoBehaviour
{
    public Button m_Button;
    public Text m_Text;
    void Start()
    {
        m_Button.onClick.AddListener(ButtonOnClickEvent);
    }
    public void ButtonOnClickEvent()
    {
        m_Text.text = "Mouse click"; }}Copy the code

Bind the event by listening to the object clicked by the ray

Code:

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

public class ButtonTest : MonoBehaviour
{
    public Text m_Text;

    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            if(OnePointColliderObject() ! =null)
            {
                if (OnePointColliderObject().name == "Button" || OnePointColliderObject().name == "Text") { ButtonOnClickEvent(); }}}}// Click the object to get the name of the object
    public GameObject OnePointColliderObject()
    {
        // The object that holds mouse or touch data
        PointerEventData eventDataCurrentPosition = new PointerEventData(EventSystem.current);
        // The current pointer position
        eventDataCurrentPosition.position = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
        // The feedback data after the ray hit
        List<RaycastResult> results = new List<RaycastResult>();
        // Cast a ray and return all collisions
        EventSystem.current.RaycastAll(eventDataCurrentPosition, results);
        // Return to the clicked object
        if (results.Count > 0)
            return results[0].gameObject;
        else
            return null;
    }

    public void ButtonOnClickEvent()
    {
        m_Text.text = "Mouse click"; }}Copy the code

4. Realize button click event through EventTrigger

Code:

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

[RequireComponent(typeof(EventTrigger))]
public class ButtonTest : MonoBehaviour
{
    public Text m_Text;

    void Start()
    {
        Button btn = transform.GetComponent<Button>();
        EventTrigger trigger = btn.gameObject.GetComponent<EventTrigger>();
        EventTrigger.Entry entry = new EventTrigger.Entry
        {
            // Mouse click event
            eventID = EventTriggerType.PointerClick,
            / / mouse into the event entry. The eventID = EventTriggerType. PointerEnter;
            / / mouse sliding out event entry. The eventID = EventTriggerType. PointerExit;
            callback = new EventTrigger.TriggerEvent()
        };
        entry.callback.AddListener(ButtonOnClickEvent);
        // entry.callback.AddListener (OnMouseEnter);
        trigger.triggers.Add(entry);
    }

    public void ButtonOnClickEvent(BaseEventData pointData)
    {
        m_Text.text = "Mouse click"; }}Copy the code

Button response events are handled by the generic class UIEventListener

Code:

using UnityEngine;
using UnityEngine.EventSystems;

public class UIEventListener : MonoBehaviour.IPointerClickHandler
{
    // Define the event proxy
    public delegate void UIEventProxy();
    // Mouse click event
    public event UIEventProxy OnClick;

    public void OnPointerClick(PointerEventData eventData)
    {
        if(OnClick ! =null) OnClick(); }}Copy the code

ButtonTest.cs

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

[RequireComponent(typeof(EventTrigger))]
public class ButtonTest : MonoBehaviour
{
    public Text m_Text;

    void Start()
    {
        Button btn = this.GetComponent<Button>();
        UIEventListener btnListener = btn.gameObject.AddComponent<UIEventListener>();

        btnListener.OnClick += delegate () {
            ButtonOnClickEvent();
        };
    }

    public void ButtonOnClickEvent()
    {
        m_Text.text = "Mouse click"; }}Copy the code