Unity uses delegates to complete click event registration

1. Need to create 2 scripts

1.ClickListener .cs
2.UIManager.cs

Second, the specific steps

1.ClickListener

First determine what needs to be done in ClickListener!

1.1

We need to define a delegate, this delegate ClickCallBack has two parameters, a Transform type, an object type, and then define two private global variables ClickCallBack mCallBack, object mParm

// An highlighted block
	public delegate void ClickCallBack(Transform t, object parm);
    private ClickCallBack mCallBack;
    private object mParm;

1.2

Define three methods:

  • AddCallBack(Transform trans,ClickCallBack callBack,object parm) //For external callback field addition
  • SetCallBack(ClickCallBack callBack, object parm)//Set internally
  • OnPointerClick(PointerEventData eventData)//Click to trigger event
    Note: The third OnPointerClick is implemented from the interface IPointerClickHandle
using UnityEngine;
using UnityEngine.EventSystems;
public class ClickListener : MonoBehaviour,IPointerClickHandler
{
    
    
	public delegate void ClickCallBack(Transform t, object parm);
    private ClickCallBack mCallBack;
    private object mPram;
	public static void AddCallBack(Transform trans,ClickCallBack callBack,object pram)
	{
    
    
		//TODO 引用SetCallBack进行方法的绑定
		ClickListener cl= trans.GetComponent<ClickCallBack>();
		if(cl==null)
		{
    
    
			Debug.Log("----------GetComponentFailed----------");
		}
		SetCallBack(callBack,pram);
	}
	public void SetCallBack(ClickCallCack callBack,object pram)
	{
    
    
		//通过UIManager中调用AddCallBack来进行委托的绑定
		mCallBack=callBack;
		mPram=pram;
	}
	public void OnPointerClick(PointerEventData eventData)
    {
    
    
    	//触发事件
    	Debug.Log("------ClickTrigger------");
    	mCallBack(transform,pram);
    }
}

2.UIManager

2.1 Declare and bind methods to objects that need to register for monitoring

Declare a variable Transform btn1 that needs to be monitored and bound; there is also a method for processing logic (triggering)

using UnityEngine;

public class UIManager : MonoBehaviour
{
    
    
    public Transform btn1;
    private void Awake()
    {
    
    
        ClickListener.AddCallBack(btn1, OnBtn1Click, null);
    }
    // 这里的参数必须和定义的委托保持一致
    public void OnBtn1Click(Transform trans,object pram)
    {
    
    
        Debug.Log("---------btn1Click----------");
    }
}

to sum up

This click method is applicable to all UI elements. You only need to add ClickListener components to the UI elements, and add monitoring and registration methods in the manager!

Supplement: If you need 3D objects to respond to button clicks, you can directly add the function OnMouseClick() in ClickListener.cs; because OnPointerClick() only responds to the UI, OnMouseClick() only responds to 3D objects; therefore, both are placed in ClickListener. No conflict in cs

    private void OnMouseClick()
    {
    
    
        Debug.Log("------OnMouseClick--------");
        mCallBack(transform, mPram);
    }

Guess you like

Origin blog.csdn.net/weixin_44870508/article/details/113980175