jeudi 20 août 2015

Unity Design Pattern - Singleton Example (Part 2)

Ok so now we are going to add another option to our menu: "Quit" . We would like a confirmation by the user wether he actually wants to quit or just cancel his request.

We have now a "setup" function to initialize our popup properly, I implemented a general one and a basic one.

In our example we have a "validate" and "cancel" button but it could work for any two choices scenario.

So here is the setup function for our popup :






























It is not a completely safe script as we don't check if there actually IS a text component for the button when we change the label (that would mean the user has modified the original popup prefab). Of course if one want to add buttons, input fields... in the popup one should redesign the all "setup" function(s).

and this is how the GameHandler looks like:







































Note that in unity when you deactivate an object he gets kind of lost (you can find them by script using Find() ) unless you have a reference on a script. With the singleton you always have access to the instance of your object (just by knowing the name of the class) even if it is deactivated which is kind of helpful.

Aucun commentaire:

Enregistrer un commentaire