Hello, I’m Jojo! and in this post of our Unity tutorials series, we’ll see how simple and highly reusable scripts can make your life easier in Unity3D. And how, if you find some time, making them look better, will also save you time and bugs!
Thinking before coding
When creating scripts, sometimes my main thought is, if I take this script and just drop it in another, and entirely different project, will it work?
If the answer is NO, then you should really take at least some minutes to think if it’s easier to do it in some other way that lets you do that!
When I created this script, I was creating a GUI for a game. It had several menus as usual (main menu, credits, options, store, etc). After doing some scripts that let me show some panel and hide another, I realized, what If I create a simple script, that lets me do just that? I could just use it in every project and with only two game object drags, I would have a full flow in a matter of seconds. And that is how I created the “SmartButton“.
Then, I stumble onto something I did a dozen times. I had to do a script that opens an URL. So I said to myself, “Jojo, why don’t you use the same script and just add a checkbox to it that let’s you choose what behavior will it have?!
Well, you get the idea. I added several stuff I wanted to use (still wanting to add more), later changed the checkbox for an enum, and that’s it! a simple and reusable script, that I’d like to share with you!
Alright, how does it work?
The script we will be talking about today as I said before, is SmartButton. It’s a simple script, that requires a collider (2D or 3D) to be attached to a game object, and will let you use that as a button that can open a URL, load a scene, switch GUI panels (or basically hide and show objects), start a coroutine, exit the game, send events, and even use it for IAP purchases, all in one, super clean and easy to use script.
Really Jojo? All that, clean and is simple to use?
Here is the code
So what do we have here
Usage : Is an enum, with all the possible uses of the button. Simple enough
UseAs : Is a variable of Usage type, so this will tell us basically, how are we going to use this script
Value : Is a string that will be used in different ways according to the current use of the button
ComponentToUse : When used as a coroutine trigger, a coroutine with the specified name will be started in this script.
GoToPanel : This object will be set to active.
OurParentPanel : Since most of the times, buttons are childs of a menu or something alike, here we will have a reference to the panel that has to be hidden once we tap on this button.
And what usages does this script have
Default: Will trigger an event OnSmartButtonClickEvent, you can register to this event by doing “SmartButton.OnSmartButtonClickEvent += myHandlerMethod”. Note that is a static event, you can probably replace it and just make it public if you want to listen to a specific button event as well.
URL: Opens a URL
Scene Loader: Will open the scene with the specified Name (Remember to add that scene in the build settings)
Panel Switcher: Will hide “OurPanel” using SetActive(false), and will show “GoToPanel” using SetActive(true)
IAP Purchase: Depends on your IAP management, but this could trigger a purchase event with the product Id
Exit: Will exit the application
Coroutine Trigger: Will start a coroutine with the specified name, in the specified ComponentToUse
So at this point, I had been using SmartButton just choosing the enum I wanted, but when I shared it with other people, they often got errors like : “Hey, I’m clicking and this is not working!” or “Hey, I just added an object to hide but I get this weird null reference!!”.
So yeah, all this was my fault, and I realized I needed to present it in a cleaner and easier to use way. Some way that won’t make you look at the code to use it, unless you want to tweak it in some way. So I changed how it looked in the inspector, I even added some comments right there when needed, and now is clean, easy to use, and rehusable, in every project.
This is the editor script
What I want you to get out of this post : When creating scripts, make yourself the question, can I drag this to another project, and will this just work? If the answer is NO. Refactor it. Think about it, and do it right. Your future self will thank you many many times.
If you have some ideas (I do have them) to make SmartButton better, then please tell us about them, and we can include them and share them to everyone!
And of course, feel more than free to use this, and to comment on how would you make this in a different way. Or if you created some similar and easy to use script that you ended up using in several projects, tell us about it!
I hope you enjoyed the post, and that you didn’t suffer much from my very bad writing skills!
Oh and remember, simple Scripts, go a long way!