- Home /
Returning Coordinates when clicking on a QUAD
First, Thanks for taking the time to read my post and provide a clear answer... or any tips you can provide.
Setup
have a Main Camera and a Quad ( its a bit stretched ). And a texture on the QUAD of a simple picture i made in photoshop that I drew a start button and an exit button. Simple, right? I have a MouseClick C# script on the quad that checks for OnMouseDown ()
I want to
- OnMouseDown ()... I want to find out if they clicked the start button box, or the exit button box
Should I be trying to find the x,y coordinate of the JPG when clicked? I am just not sure the best way to go about finding out if they clicked the simple buttons of the texture on the quad?
Is Raycast, the solution I seek? How would that translate to whether or not they are clicking the buttons instead of simply just clicking the quad anywhere?
Because, I did draw this in photoshop... I do know the exact coordinates on the JPG where the button boxes are, but Unity has it's own coordinate system.
I did find this bit of code in the UnityAPI:
using UnityEngine;
using System.Collections;
public class ExampleClass : MonoBehaviour {
void Update() {
if (!Input.GetMouseButton(0))
return;
RaycastHit hit;
if (!Physics.Raycast(camera.ScreenPointToRay(Input.mousePosition), out hit))
return;
Renderer renderer = hit.collider.renderer;
MeshCollider meshCollider = hit.collider as MeshCollider;
if (renderer == null || renderer.sharedMaterial == null || renderer.sharedMaterial.mainTexture == null || meshCollider == null)
return;
Texture2D tex = renderer.material.mainTexture;
Vector2 pixelUV = hit.textureCoord;
pixelUV.x *= tex.width;
pixelUV.y *= tex.height;
tex.SetPixel(pixelUV.x, pixelUV.y, Color.black);
tex.Apply();
}
}
But, I do not understand Renderer renderer = hit.collider.renderer and down... I think this is what I need. If someone could possibly give me a brief introduction in English on what the code lines are doing...
ANSWER
ow, so I simply just took the sample code from the UNITY API and threw it on the Quad, and it basically worked...almost! If anyone else is curious how to do this the Image I used is this:
and this is the script I threw on it. The print statements easily let me know where my boxes are and then I can use a check to see if they clicked within those boxes
// Use this for initialization
void Start () {
}
// Update is called once per frame
void On$$anonymous$$ouseDown ()
{
RaycastHit hit;
if (!Physics.Raycast(GameObject.Find ("$$anonymous$$ain Camera").camera.ScreenPointToRay(Input.mousePosition), out hit))
return;
Renderer renderer = hit.collider.renderer;
$$anonymous$$eshCollider meshCollider = hit.collider as $$anonymous$$eshCollider;
if (renderer == null || renderer.shared$$anonymous$$aterial == null || renderer.shared$$anonymous$$aterial.mainTexture == null || meshCollider == null)
return;
Texture tex = renderer.material.mainTexture;
print (hit.textureCoord);
Vector2 pixelUV = hit.textureCoord;
pixelUV.x *= tex.width;
pixelUV.y *= tex.height;
print ("X:"+pixelUV.x +" ,Y:"+pixelUV.y);
//tex.SetPixel(pixelUV.x, pixelUV.y, Color.black);
//tex.Apply();
}
Answer by AcE_fLoOdEr · Jan 07, 2015 at 02:32 AM
Use unity 4.6 UI. All what you're doing is just a waste of time to be honest. I used to do that in the old unity.
1). update your unity to 4.6 if you haven't already done so
2). right click in the hierarchy and go to UI > Canvas
3). right click on the canvas and go again to UI > Image and add the "Main Menu" image or just add text.
4). right click on the canvas and go again to UI > Button. Then mess around and see what happens.
Your answer
Follow this Question
Related Questions
What does a quad look like? 1 Answer
How to make a menu that looks like a smartphone/tablet? 2 Answers
How to make this custom GUI/Menu? 2 Answers
Creating Menu in 2D Game 0 Answers
2D Game: main menu button dont work 0 Answers