Author Topic: Assigning Delegates via Inspector vs Code  (Read 1714 times)

schellenberghq

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 31
  • Game Programmer
    • View Profile
    • Blog
Assigning Delegates via Inspector vs Code
« on: October 21, 2014, 12:01:57 PM »
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.


  1. // 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.
  2. createButton.onClick.Add (new EventDelegate (this, "OnCreate"));
  3.  
  4. void OnCreate(){
  5.  // create something
  6. }

This feels more maintainable to me than having to manually assign events via inspector.
  1. // However assigning events like this removes the misspelled string, as now you have to strongly use the actual method
  2. // Setup button events
  3. createButton.onClick.Add(new EventDelegate(OnCreate));
  4. editButton.onClick.Add (new EventDelegate (OnEdit));
  5. exportViewButton.onClick.Add (new EventDelegate (OnExport));
  6. helpButton.onClick.Add (new EventDelegate (OnHelp));
  7. doneButton.onClick.Add (new EventDelegate (OnDone));
  8.  

And even further, adding events with parameters.

  1. // Sample Event Delegate with parameter: EventDelegate.Set(btn.onClick, delegate() { MyClick(123); });
  2. EventDelegate.Set (newCanvasViewController.canvasOverviewButton.onClick, delegate() {
  3.         OnClickedCanvasOverview (newCanvasViewController);
  4. });
  5.  
  6. EventDelegate.Set (newCanvasViewController.deleteButton.onClick, delegate() {
  7.         OnDelete (newCanvasViewController);
  8. });
  9.  
  10. EventDelegate.Set (newCanvasViewController.createButton.onClick, delegate() {
  11.         OnFinishCreate (newCanvasViewController);
  12. });
  13.  
  14. EventDelegate.Set (newCanvasViewController.titleOfCanvasInput.onSubmit, delegate() {
  15.         OnDone();
  16. });
  17.  
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.
« Last Edit: October 21, 2014, 12:24:38 PM by schellenberghq »
---------------------------
Jacob S.
Game Programmer
---------------------------