optimization of game code

I’ve recently read on another thread that some (or maybe all, I don’t know) of the overrideable methods of MonoBehaviour are accessed by Unity by issuing a SendMessage() call to each of them, generating overhead that is a tad greater than what is generated when starting a coroutine and letting it continuously loop and yield to simulate a frame-dependent behavior. I once thought that the overrideable methods were always called regardless of whether they were included in a MonoBehaviour-derived script or not, since you were overriding them after all. Of course, now that I think about it, I’ve never once used the override keyword when creating the base method for an overrideable method, so I guess things always appear clearer in retrospect. Anyway, what I would like to know is if it really is better for optimization of code to call a method that simulates Update() rather than Update() itself. Basically, assuming that you were writing code for a standard level for a game with the scope and magnitude of a multiplayer game (since that is one of my long-term objectives), would the game run faster and smoother if my scripts employed the Update(), FixedUpdate() and LateUpdate() methods or just coroutines that simulated those methods? And if the latter way is indeed more efficient, then there follow these questions: 1) which is more efficient between InvokeRepeating() and StartCoroutine; 2) how would one go about achieving the inheritance characteristics (i.e. being able to use base.Update() on a child class) of a method for a coroutine; and 3) how would one gain the benefits of using the overrideable methods (such as LateUpdate() being called each frame before the cameras are rendered, and being able to use the GetKeyUp/KeyDown/etc. methods accurately)?

Also following the purpose my inquiry, I pose another question: is using GUIElements really better for the game’s frame rate than calling OnGUI()?

TLDR: Is calling a coroutine or a repeating method from Start() more efficient performance-wise than calling the overrideable methods of MonoBehaviour (Update(), FixedUpdate(), LateUpdate())? And assuming that it is, then which is more efficient performance-wise between InvokeRepeating() and StartCoroutine(), and how would you retain the added benefits of the overrideable methods (such as retention of accurate state of GetKeyUp/GetKeyDown/etc., inheritability, being called before the camera is rendered, etc.)?

Don’t use any gui stuff at all. There is a reason unity is replacing all of it in a future patch. This really only applies to mobile though, as computers have more than enough grunt to chew through it.

I don’t really want to spend 5 mins trying to decipher your wall of text which is causing errors in my brain but I will say that the docs have several optimization bits so search for optimization there.

Are you experiencing performance problems currently with your project?

I think there are bigger gains to be had from optimizing and avoiding certain methods, rather than spending quite a bit of time rewriting and adding back the functionality of those MonoBehaviour methods. I never use SendMessage, Find methods outside Start(), or the component shortcuts, I reference as much as possible beforehand, never destroy/instantiate anything that can be reused to stop the GC kicking in and keep Update() as lean as possible.

As for the GUI, personally I’d wait to see what UT bring out for it replacement (in 3.5?) If you can’t wait I’d suggest you implement your own GUI or use a 3rd party solution like EZGUI.

Eric5h5 or Dreamora will have a better idea if you really want to do it all as co-routines :slight_smile:

Well, the last project that I worked on was a small game prototype for the Android. Despite being a fairly small project, the frame rate dipped well below tolerable levels. I used OnGUI() to generate a few labels and whatnot, so that contributed to some of the dip, but not enough to make it unplayable. The most significant factor contributing to the dip was the enemy AI which was implemented in a mere 4 enemy objects. The enemy AI basically simulated a conic field of vision using raycasting based on arc length for detection in addition to a very rudimentary pathfinding system. Anyway, I had to rewrite the AI to minimize calls to detection, but never had the opportunity to test if that alleviated some of the the burden on the Android device since my team decided to port the prototype to PC in the end. Anyway, I now have some spare time and can start working on a larger-scale project, which will be continually expanded over time to implement multiplayer playability as well. I also wish to employ a relatively complex AI detection and decision-making algorithm for at least 5 enemy objects present in a level at any given time. (Think 5 distinct bosses acting as one unit against the player.) Now, I’m a long ways from actually finishing this project, and will surely be making a few more small games alongside it, along the way. But I wish to at least have a general idea of which method provides greater optimization efficiency-wise before I start typing down thousands of lines of code which I would later find out would generate extremely huge overhead and couldn’t be converted to coroutines as easily. I guess you could say it’s just a bit of planned pre-optimization to avoid having to do a double-take.

Tl;dr could you maybe break your word walls into nicely formed paragraphs?

:slight_smile:

Gimme a couple of minutes. =P