- Home /
Programming 2D buttons
Hello, I have a game which has some 2D buttons in the main. Those buttons were made in another program and I added them in a prefabs folder. The problem I have is those are several buttons, so to program them the only way I know is making a script with the function OnMouseDown() for each button. Is a better solution like making a general script in the camera or in an empty GameObject for everything?? Thank you very much
Each button may have a different behaviour if you click on one or another, right ?
Then, you will have to implement On$$anonymous$$ouseDown for every button.
Please, be more specific if you want help.
Answer by amanpu · Sep 21, 2015 at 02:00 PM
There is no need to make separate script for each button. You can make only one script with only OnMouseDown() method. And add this script to all the buttons. Then you can differentiate between buttons using switch statement as following:
void OnMouseDown()
{
switch(gameObject.name)
{
case "Button1":
// Do something related to Button1 here
break;
case "Button2":
// Do something related to Button1 here
break;
}
}
But make sure you are not doing anything else in this script. Because this script might be attached to many GameObjects and all the methods like Start(), Update() are invoked individually for each GameObject. This will eventually lead to problems e.g.:
void Start()
{
GameObject player = GameObject.Find ("Player");
player.SetActive(false);
}
Above method will cause a problem, if this script is attached to more than one GameObject then first time it will find the "Player" and disable it. When this method is invoked again second statement will throw Runtime Exception because it will not be able to find disabled GameObject.
Even if this solution works, it is a very dirty way to do things.
Supposing you have hundreds of buttons Change a button name ? Add a new button ? Delete one ? You have to find your button in the mess you have created inside the On$$anonymous$$ouseDown function.
As you said, if a button has a Start button that must not be executed by the others, you have to make conditionnal statement. The same as above, change / add / delete a button, and you have to change your whole script
This code is absolutely not maintainable ...
I know, some arguments here can apply if you have a thousand scripts, one for each button. But there are some ways to use abstract classes so as to make the job less painful.
Hello! Yes, each button have their own function. I have used in another part (maybe I'll consider what you wrote later) something that the name is Physics.Raycast into the script and then I added it to the camera. But in that case were only three objects that I had to detect; not all the buttons I mentioned. It detects when an object has a collider and then it applies what you want:
void Update(){
if (Input.Get$$anonymous$$ouseButtonDown(0)){
Ray ray = camera.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit)){
Destroy(hit.transform.gameObject);
}
}
}
Thank you very much!
@Hellium ,So what is best solution then?
$$anonymous$$y argument is there should be one script just for gettings clicks. Now for example you need to play sound on button click, Is it justify to write same line that is audioSource.Play in all scripts?
But yes working of all buttons should not be defined here, It should be defined in some other script say Game$$anonymous$$anager using delegate callbacks. But I think click should be in one script rather than in thousand scripts.
And here is the utility of abstract classes ! ;) Just call the audio play function in the Start method of the abstract class, and make the scripts derive from this class and you have to write only one line.
click should be in one script rather than in thousand scripts
What do you mean ? If the logic behind every button is different, they should have their own script, this is the way I see things.
Answer by DiegoSLTS · Sep 21, 2015 at 02:58 PM
In Unity you add components to add behaviour to game objects. If you have buttons, add components to the buttons. If more than one button does "the same" (same code with probably diffrerent values) add the same component to them. If another button does something different, add another component.
You can add a script to the camera to handle everything, but it goes against the Component Based design. You're not forced to use that approach though, but it makes things easier if you do, since the engine was created around that concept.
Look at how uGUI works, each button has it's "OnClick" event, so each button "knows" what it should do when clicked. Note that uGUI uses an EventSystem game object that actually detects what is clicked, but that object does only that, it just tells the object "hey, you were clicked, do what you have to do".
Answer by franbat · Sep 21, 2015 at 07:49 PM
@Hellium, what @amanpu suggested is something that is a good idea in the case that you write "This button makes this". I used the Physics.Raycast beacuse in that case I had to make some things in that code. But the thing I'm agree with @Hellium is that I don't like the idea of adding one script for all the buttons.... It depends on how do you like to program