Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by karma0413 · Jan 07, 2015 at 01:48 AM · menucoordinatesquadclicking

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

  1. OnMouseDown ()... I want to find out if they clicked the start button box, or the exit button box
    1. 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?

    2. 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...

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image karma0413 · Jan 07, 2015 at 01:27 AM 0
Share

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:

alt text

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();
     }

1 Reply

· Add your reply
  • Sort: 
avatar image
-1

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges