Several buttons with the same onClick event?

For example, calculator buttons (each of them appends a specific character to the string by the same way).

What is the common approach to implement it?

For example, in usual programming languages I create an enum which has different values for different buttons and just assign the same method to all the buttons.

As I understand It is hard to implement in unity:

  1. A lot of extra duplicating info: Each OnClick event of each button requires a script and sender to be specified manually? Even if OnClick method is inside the script inside this button

  2. If implemented as 1) then it requires much more time and it becomes hard to support such project

You can simply expose a public void AppendDigit (int digit) method to your main public class Calculator : MonoBehaviour.

Then, you create buttons and assign that method to their onClick events, and as the onClick is of type void, it’ll make a static (not dynamic) access to the method, and you’ll set the digit for each event.

Here’s a demo video of a UI extension in which I’ve made a calculator type input. The extension is also available on the Asset Store.