A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://docs.unity3d.com/Manual/sprite/../../ScriptReference/../Manual/UnityEvents.html below:

Unity - Manual: Inspector-configurable custom events

Optimizing per-frame updates with an update manager

Inspector-configurable custom events

Unity provides the UnityEvent API as a Unity-specific alternative to standard C# events and delegates. The main advantage of Unity events over standard C# events is that Unity events are serializable, meaning you can configure them in the Inspector window.

A UnityEvent can be added to any MonoBehaviour and is executed at runtime like a standard C# delegate. When a UnityEvent is declared in a MonoBehaviour it appears in the InspectorA Unity window that displays information about the currently selected GameObject, asset or project settings, allowing you to inspect and edit the values. More info
See in Glossary
window where you can define callbacks that persist between Edit time and runtime.

Unity events have similar limitations to standard C# delegates:

Configure Unity events Prerequisites Configure callbacks in the Inspector window:
  1. Select the GameObjectThe fundamental object in Unity scenes, which can represent characters, props, scenery, cameras, waypoints, and more. A GameObject’s functionality is defined by the Components attached to it. More info
    See in Glossary
    with the script component that contains your declared UnityEvent field(s).

  2. Click the + button under the name of an event to add a slot for a callback.

  3. Select the UnityEngine.Object you want to receive the callback. You can use the object selector or drag and drop an object into the field.

  4. Select the function you want to be called when the event happens. The dropdown selector is populated with filtered lists of appropriate methods available on the GameObject and its components.

  5. Repeat steps 1–4 as required to add additional callbacks for the same event.

Configuring callbacks for events called Trigger Entered and Trigger Exited in the Inspector window Static and dynamic calls

When configuring a UnityEvent in the Inspector window there are two types of function calls that are supported:

Choosing between static or dynamic functions whose signatures match the event type in the Inspector window Generic support in UnityEvent

By default a UnityEvent in a Monobehaviour binds dynamically to a void function. But you can create a UnityEvent with up to four generic type parameters as shown in the following example:

using UnityEngine;
using UnityEngine.Events;

public class GenericTest : MonoBehaviour
{
    public UnityEvent<int, int, bool, string> myEvent;
    
    // Start is called once before the first execution of Update after the MonoBehaviour is created
    void Start()
    {
        if (myEvent == null)
        {
            myEvent = new UnityEvent<int, int, bool, string>();
        }
        myEvent.AddListener(Ping);
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.anyKeyDown && myEvent != null)
        {
            myEvent.Invoke(5, 6, true, "Hello");
        }
    }

    void Ping(int i, int j, bool print, string text)
    {
        if (print)
        {
            Debug.Log("Ping: " + text + i + j);
        }
    }
}
Additional resources

Optimizing per-frame updates with an update manager


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4