- Home /
Implementation of a calculator with a given object(calculator)
Hi All, I am about to develop a calculator. My artist given me a calculator object, in that all the buttons are separate objects and calculator screen also separate. How do I write the code for it for accessing all the buttons with over the screen.can I use GUI text for accessing calculator.? If so let me know the procedure behind it.
Doing a calculator is can be a complex problem depending on how far you want to take it and how closely you want to mimic a "real" calculator. Here is a question that talks a bit about a calculator:
http://answers.unity3d.com/questions/407297/simple-calculator-for-kid-math-app.html
Note @fafase code might give you a starting point. He is using GUI.Button() with strings. You would be using GUI.Button() with your textures.
Given the scope of this problem (many separate technical issues that can be solved in a number of ways), the Unity Forum is likely a better place for this question.
I have already gone through this ..This is entirely different from my question.I have an obj. I have to access the screen obj in the calculator obj. for every hit w.r.t to button the corresponding value has to be displayed. once I get it done I'll write the code for proper working of the calculator.
Answer by fafase · May 18, 2013 at 08:41 AM
Ok I guess your question is not about making calculation but about how to get the pressed button.
Two ways that I know.
First use GUI.Button. You would have to create a GUIStyle for each button or a GUISkin for the whole thing , and pass the corresponding style/skin index.
GUIStyle button1 = new GUIStyle();
GUIStyle button2 = new GUIStyle();
public Texture2D texture1;
public Texture2D texture2;
void Start(){
button1.normal.background = texture1;
button2.normal.background = texture2;
}
void OnGUI(){
if(GUI.Button(positionRect1,"",style1)){}
if(GUI.Button(positionRect2,"",style2)){}
}
The other way is to use planes to which you pass the texture and then you use a basic raycast and check. You would manually place them in the environment.
RaycastHit hit;
public Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit, 100)){
if(hit.collider.tag == "Button1"){}
if(hit.collider.tag == "Button2"){}
}
Thank u for giving me a direction. But please find this attachment.In this I put my calculator object image. For this buttons are objects and calculator screen also another object. I added box colliders to all the buttons. Now I tagged all buttons.though the above code is not working.
2)after I get access to the buttons, how can I use GUI text to make the text to be displayed on the calculator screen.
You just place a Text$$anonymous$$esh over your diplay.
Seriously, if you ask such a question you either started using Unity 23.4 $$anonymous$$utes ago and / or you're just very lazy to actually look through the things Unity offers. For any task there are multiple ways to get the job done. First you should be clear about what you need. The second step is to look out what functions / components / features in Unity can be used to implement what you want.
If you're new to Unity, just take a look at this page. There's a complete list of all classes that comes with Unity. You should invest some time and look through the list. Almost all classnames give a good hint what the class is good for.
Your answer
Follow this Question
Related Questions
how to access a screen(separate obj in my app). 1 Answer
Android Button Screen 1 Answer
How can I get a UI Panel's size in relation to the screen size? 1 Answer
Developing a Dynamic GUI 1 Answer
GUI adapting to screen resolution? 3 Answers