Question:
Do you prefer assigning event delegates using the Inspector or via Code.
I ask this because when I first started my project, I assigned all the events via inspector.
As the project progressed, I found I needed to assign some events via code because they came dynamically.
And now, I find myself wanting to go back and assign via code all the events I am currently doing via Inspector.
I came to this conclusion because I decided it was much easier to maintain and fix/alter these events via code than it was via inspector.
For example, if I replace my Create Button, I now have to reassign the event in it's OnClick, and if it has multiple events than it's spending a few minutes rewiring all those events back up.
However, if the events were assigned via code, I would simple have to reassign the new Create Button to my script via the Inspector.
I guess it's about consistency. I'm just realizing that I consistently assign UI elements via inspector, as opposed to trying to use FindGameObject like I used to along time ago.
Perhaps it is also about how much control you want or need.
Anyways, just some thoughts.
// Example of assigning event via code. In this scenario I would say it's the same as assigning in the inspector, and may actually cause unwanted behavior if you change the method name.
createButton
.onClick.Add (new EventDelegate
(this,
"OnCreate"));
void OnCreate(){
// create something
}
This feels more maintainable to me than having to manually assign events via inspector.
// However assigning events like this removes the misspelled string, as now you have to strongly use the actual method
// Setup button events
createButton
.onClick.Add(new EventDelegate
(OnCreate
));editButton
.onClick.Add (new EventDelegate
(OnEdit
));exportViewButton
.onClick.Add (new EventDelegate
(OnExport
));helpButton
.onClick.Add (new EventDelegate
(OnHelp
));doneButton
.onClick.Add (new EventDelegate
(OnDone
));
And even further, adding events with parameters.
// Sample Event Delegate with parameter: EventDelegate.Set(btn.onClick, delegate() { MyClick(123); });
EventDelegate.Set (newCanvasViewController.canvasOverviewButton.onClick, delegate() {
OnClickedCanvasOverview (newCanvasViewController);
});
EventDelegate.Set (newCanvasViewController.deleteButton.onClick, delegate() {
OnDelete (newCanvasViewController);
});
EventDelegate.Set (newCanvasViewController.createButton.onClick, delegate() {
OnFinishCreate (newCanvasViewController);
});
EventDelegate.Set (newCanvasViewController.titleOfCanvasInput.onSubmit, delegate() {
OnDone();
});
I am in no way diminishing the importance and awesome-ness, and ease of assigning events via Inspector. I'm simply speculating as a programmer.