In Java, I'm able to do the following:
Java
public class Button {
private Callback callback;
public Button(Callback callback) {
this.callback = callback;
}
public void update() {
// Check if clicked..
callback.onClick(this);
}
public interface Callback {
public void onClick(Button Button);
}
}
And use it like:
Button b = new Button(new Callback() {
@Override
public void onClick(Button b) {
System.out.println("Clicked");
}
});
And I'm wondering, if the same is possible in (Unity) C#.
Currently I have the same setup, with the correct syntax
C#:
public class Button {
private Callback callback;
public Button(Callback callback) {
this.callback = callback;
}
public void Update() {
// Check if clicked..
callback.OnClick(this);
}
public interface Callback {
void OnClick(Button button);
}
}
However, when I try to do the same when creating a button,
Button b = new Button(new Button.Callback() {
void OnClick(Button button) {
Debug.Log("Clicked");
}
});
I get errors like:
Unexpected symbol 'void'
error CS1526: A new expression requires () or [] after type
error CS8025: Parsing error
I've tried making the method 'virtual' or 'abstract' in the interface, or adding the 'new' keyword before void OnClick(.. when I create the button, but no luck so far.
It seems that the unexpected symbol 'void' error dissapears if I remove the '()' from 'new Button.Callback'
I would appreciate some help here.