Usage of Unity Action

Table of contents

Namespaces

definition

use

call method


UnityAction is a generic delegate class in Unity3D, which is used to encapsulate methods with no parameters and no return value, and is mainly used to handle events and delegates in Unity.

Namespaces

using UnityEngine.Events;

definition

public UnityAction myAction;//private,protect都可,看用法

use

Method 1: Directly use the method body to give the delegate content

public class Example : MonoBehaviour
{
    private UnityAction myAction;//各访问类型都可,具体看使用场景

    void Start()
    {
        myAction = MyMethod;
        Invoke("CallMyAction", 2f);//延迟两秒唤起CallMyAction方法
    }

    void CallMyAction()
    {
        myAction.Invoke();//唤起myAction
    }

    void MyMethod()
    {
        Debug.Log("MyMethod called!");
    }
}

Method 2: Increment and decrement operators

public class Example : MonoBehaviour
{
    private UnityAction myAction1;
    private UnityAction myAction2;

    void Start()
    {
        myAction1 = MyMethod1;
        myAction2 = MyMethod2;

        // 向 myAction1 增加 MyMethod2 方法
        myAction1 += myAction2;//也可以是myAction1 += MyMethod2;
        myAction1.Invoke(); // 调用 myAction1 委托

        // 从 myAction1 移除 MyMethod2 方法
        myAction1 -= myAction2;//也可以是myAction1 -= MyMethod2;
        myAction1.Invoke(); // 再次调用 myAction1 委托,这次只调用 MyMethod1 方法
    }

    void MyMethod1()
    {
        Debug.Log("MyMethod1 called!");
    }

    void MyMethod2()
    {
        Debug.Log("MyMethod2 called!");
    }
}

If you remove a method from the UnityAction delegate, but the method has not been added to the delegate, you can use the -= operator to remove it, which will not throw an exception, and the -= operator will do nothing.

Method 3: Anonymous method

public class Example : MonoBehaviour
{
    private UnityAction myAction;

    void Start()
    {
        myAction = delegate { Debug.Log("MyMethod1 called!"); };
        myAction += delegate { Debug.Log("MyMethod2 called!"); };
        myAction.Invoke(); // 调用 myAction 委托,依次调用 MyMethod1 和 MyMethod2 方法
        
        myAction -= delegate { Debug.Log("MyMethod2 called!"); };
        myAction.Invoke(); // 再次调用 myAction 委托,只调用 MyMethod1 方法
    }
}

It should be noted that when using an anonymous method to assign a value to a UnityAction delegate, the parameter list and return type of the delegate type must match the method signature, otherwise a compilation error will occur.

call method

myAction.Invoke();

or

myAction();

Update (2023.05.15)

Looking at the background, I found that many students entered the article by searching for UnityAction?.Invoke(), so I wanted to update it.

It is easy to think of the conditional operator when you see the question mark? : 

Exp1 ? Exp2 : Exp3;

equivalent to

if(Exp1)
{
    Exp2;
}
else
{
    Exp3;
}

In UnityAction?.Invoke(), ? exists as a null coalescing operator.

Indicates that if UnityAction is null, the Invoke() method will not be called and NullReferenceException will not occur. If UnityAction is not null, the Invoke() method is called.


Personal study notes, if there are mistakes, you are welcome to criticize and correct.

Guess you like

Origin blog.csdn.net/weixin_46041788/article/details/130403424