System.Action OnTransformChanged

book_4_sparkGenerated
code_blocksInput

Description

The OnTransformChanged field is an event of type System.Action in the GameTransform class. This event is triggered whenever the transform of a GameObject changes. It allows developers to attach custom logic that should execute in response to changes in the transform properties such as position, rotation, or scale.

Usage

To use the OnTransformChanged event, you can subscribe to it by adding a method that matches the System.Action delegate signature. This method will be called whenever the transform changes.

Example

// Example of subscribing to the OnTransformChanged event

public class MyComponent : Component
{
    private GameTransform myTransform;

    public override void Spawn()
    {
        base.Spawn();

        myTransform = GameObject.Transform;
        myTransform.OnTransformChanged += OnTransformChangedHandler;
    }

    private void OnTransformChangedHandler()
    {
        // Logic to execute when the transform changes
        Log.Info("Transform has changed!");
    }

    public override void OnDestroy()
    {
        // Unsubscribe from the event to prevent memory leaks
        if (myTransform != null)
        {
            myTransform.OnTransformChanged -= OnTransformChangedHandler;
        }

        base.OnDestroy();
    }
}