XaiJu
Megan Fox
Megan Fox

patreon


Advanced Throwup analysis

I figure I'll walk you through the Throwup code, to give you a leg up on extending it. Most of the meat you'll want to tweak is here:

This is where I create the UI objects. It's done on-demand, the first time something invokes the singleton, pretty simple self-instantiating singleton pattern. The only fanciness here is I'm creating a GameObject and components from whole cloth. Which is easy, really, just not a thing Unity tutorials explain. They'd probably suggest a prefab for this, but then this whole this has dependencies and - nawwww.

When you inevitably want to tweak the font, colors, text box size, whatever, I recommend using Throwup to throw up some placeholder text. Then go into the scene runtime, open the Inspector on the Throwup's UI elements, and move it all around yourself. Once you've got a good layout and whatever else you're happy with, work backward from there to update this code to implement this. Trying to go the other way, code->see how it looks->tweak->etc, can take forever.

Be Careful During Cleanup

When you inevitably add something in here (an instantiation or whatever) that you have to manually clean up when the Singleton goes away, you're going to care about this:

It's really easy to screw up with self-instantiating singletons by calling something that invokes Singleton during your OnExit code. Which, of course, then spawns a whole new GameObject while the engine is in the middle of shutting down. It'll cause some hardcore errors and leaks and etc, bad idea.

So that's why Exists is here! This is how you ask "do I need to clean this up?" without accidentally creating it in the process.

Where To Extend

You'll probably want to add to these quickly:

First thing I'd add is a Log(string, bool) that, instead of outputting "True" and "False" at the end of a string, instead appends a single character to indicate the bool is true or false. You'll see why the second you try making a Throwup of a bool. The whole line shifts left/right every time the state changes, because True and False aren't the same length character-wise, and boy howdy is it distracting.

You're probably also going to want some text colorization in there, assuming Unity UI supports it. If it doesn't, changing this to use TextMeshPro would be trivial, and that DOES support inline font coloration.

But yeah, this really isn't a complicated system. All it really does is give you some of the handy on-screen debugging capability that UE4 offers. I got sooooo tired of staring at a rapidly scrolling log window, staring at three thousands strings as they scrolled by, trying to figure out if a Bool value when true or false when I did a thing. Hopefully this saves you from that fate.


More Creators