Action Callback in Unity3D

Hello Programmers, In this article, we will discuss “Action Callback in Unity”.

Before we get started we need to know a few concepts. Let’s discuss them one by one.

Action in Unity

Action is an intrinsic, native delegate type declared in the System namespace.

To define an Action, we need to add “using System;” to the System namespace on the top.

using System;

Syntax for Actions in unity :-

public void Project(Action<string> obj)

Lambda Expressions

We use the Lambda method to create an anonymous method. This method is very much similar to delegates and really useful when you are working with delegates.

Lambda expressions can separate the input parameter on both the left and right sides of the lambda body.

Lambda Operator is denoted as “=>

Syntax :-

(insert parameters) => expression

The below-given program uses Action Callback in Unity.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;

public class ActionCallback : MonoBehaviour
{
  // Start is called before the first frame update
  void Start()
  {
    Project((String result) => Debug.Log(result));
  }

  public void Project(Action<string> callback)
  {
    bool ProjectStatus;

    //Project Status here
    ProjectStatus = true;

    if (ProjectStatus)
      callback("Yes Project is completed successfully");
    else
      callback("Sorry Project is incomplete");

  }
}

Leave a Reply

Your email address will not be published.