Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use C# Actions Instead Of UnityEvents #783

Open
tinxx opened this issue Sep 24, 2024 · 1 comment
Open

Use C# Actions Instead Of UnityEvents #783

tinxx opened this issue Sep 24, 2024 · 1 comment
Labels
Improvement Improvement of existing features

Comments

@tinxx
Copy link
Collaborator

tinxx commented Sep 24, 2024

Where it is possible, we should consider using C# delegates instead of UnityEvents for their performance benefit.

Refer to this extensive write-up by Jackson Dunstan.

While the analysis is not quite recent one can probably assume that the C# language feature delegate is better integrated and can also benefit from compiler optimizations.

@tinxx tinxx added the Improvement Improvement of existing features label Sep 24, 2024
@tinxx tinxx changed the title Use Delegates Instead Of UnityEvents Use C# Actions Instead Of UnityEvents Sep 25, 2024
@tinxx
Copy link
Collaborator Author

tinxx commented Sep 25, 2024

Declaring custom Delegates seems to be the classic approach.

The more modern approach is very similar to UnityEvent but with the classic += and -= to assign callbacks. Example:

public event Action<Vector3, Vector3> OnSizeChanged;
// Then in some method:
OnSizeChanged?.Invoke(transform.localScale, transform.position);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Improvement Improvement of existing features
Projects
None yet
Development

No branches or pull requests

1 participant