Ah, that’s certainly true. Thanks a lot for bringing up data oriented design, by the way! It’s a topic that isn’t given the attention it deserves, and it’s refreshing to have people like yourself be skeptical and make the rest of us think twice every once in a while.
As I’ve understood it, a data-oriented design seeks to minimize memory reads and cache misses by focusing on keeping things sequential in memory, right? There’s a post in a thread on Stack Overload, (http://stackoverflow.com/questions/1641580/what-is-data-oriented-design) that explains this very well using an example. It’s possible an approach such as this might be beneficial in a lot of aspects, but once again, I’m not sure I agree with you about how it applies to this discussion.
You see, the Unity GUI is already object-oriented. It’s just OO in a really poor way. Take this example from the Unity documentation on GUI.Button:
if (GUI.Button(new Rect(10, 10, 50, 50), btnTexture))
Debug.Log("Clicked the button with an image");
That’s the standard way of doing a button right now. This goes in OnGui, which gets called every time there’s a Gui-event, which means possibly several times per frame, but at least once per frame, right? (Since redrawing the Gui elements is a Gui event). The button location and size are declared with a Rect object which is instantiated with the new operator on every single OnGui. This means you’re basically asking the runtime system to allocate memory for a new (identical, in this case) Rect-object at least once every single frame, and since .Net is a managed framework, you’re also asking the garbage collector to come destroy your identical Rect-objects when they go out of scope at the end of every single call to OnGui. From a memory standpoint, this is very, very silly. In addition to this, we must also consider the boolean value GUI.Button returns. It indicates whether the button was just clicked or not, so evidently, there’s some hit-detection against the mouse pointer and left button going on inside the GUI.Button method. Since you have to check this bool on every call to OnGui in order to react to the click, you’re basically asking, “Was the button clicked now? Was the button clicked now? No? How about now, then? Now?” continually, over and over again. This is similar to the criticism the Sony-article you linked to directed against the polling of a dirty-flag. Why would a time-critical application waste time continually polling a boolean when it doesn’t have to?
Well… Delegates and events solve precisely the problem described above. They may cost more CPU cycles when they occur than the polling of a boolean, but at least they occur only once - when the button is clicked, which is when you need it. One could also argue that the constant instantiation of Rect objects could be solved by creating a global Rect outside the scope of OnGui, and then pass that to GUI.Button instead. That would also be true, but then you’re pretty much doing what I was originally suggesting - If one creates an object to represent the size and position of a button, one might as well create an object to represent the button…