Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Did the user click a menu object?

Discussion in '2D' started by scott_reece, Nov 2, 2018.

  1. scott_reece

    scott_reece

    Joined:
    May 21, 2016
    Posts:
    3
    Hello, I have a 2D game I'm building in Unity 2018.2.14f1. I have some panels and buttons in a canvas, and when the user clicks anywhere on screen I want to take the mouse position and find out if they clicked on a child of the canvas or if they clicked on anything else. How can I detect this? Here's my code in my component that is attached to the main camera game object.

    Code (CSharp):
    1.  
    2.     void Update()
    3.     {
    4.         if (Input.GetMouseButtonDown(0))
    5.         {
    6.             bool clickedInMenuSystem = //WHAT GOES HERE?
    7.             if (clickedInMenuSystem)
    8.                 return;
    9.  
    10.             // Eventually do something here
    11.         }
    12.     }
    My apologies if I'm posting this in the wrong place or if this has been answered before. I'm new here, and all the answers I found were for the older GUI system.
     
  2. MisterSkitz

    MisterSkitz

    Joined:
    Sep 2, 2015
    Posts:
    833
    You want to set up a raycast from the camera to the mouse curser. I think it's something like ScreenPointToRay().
    That should be enough information to get your research started. Sorry, I don't know more about it.
     
  3. MisterSkitz

    MisterSkitz

    Joined:
    Sep 2, 2015
    Posts:
    833
    Code (CSharp):
    1.  
    2.     void Update()
    3.     {
    4.         if (Input.GetMouseButtonDown(0))
    5.         {
    6.             bool clickedInMenuSystem =  true / false // GOES HERE!
    7.            
    8.         }
    9.     }
    A bool only reads true or false. So is what you're doing, seemingly, is determining if something was clicked on a true or false basis. However, you're only clicking the mouse and firing the left click here. You're not actually testing any conditions. To do this, you'd need an IF statement. There are ways to test if the mouse curser has collided with an object; however, I think this will lead back to casting a ray and testing the hit. That's a pretty standard way of gathering information.
     
  4. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,153
    you can try with events, the script can be added to canvas (or object).
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.EventSystems;
    3.  
    4. public class NoIdeaHowToDo : MonoBehaviour, IPointerClickHandler
    5. {
    6.     public void OnPointerClick (PointerEventData something)
    7.     {
    8.         if (something.button == PointerEventData.InputButton.Left)
    9.         {
    10.             Debug.Log("click on " + something.pointerEnter.name);
    11.             Debug.Log("position: " + something.position);
    12.         }
    13.     }
    14. }
    or maybe
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.EventSystems;
    3.  
    4. public class OtherEvent : MonoBehaviour, IPointerDownHandler
    5. {
    6.     public void OnPointerDown (PointerEventData something)
    7.     {
    8.         if (Input.GetMouseButtonDown (0))
    9.         {
    10.             Debug.Log("click on " + something.pointerEnter.name);
    11.             Debug.Log("position: " + something.position);
    12.         }
    13.     }
    14. }
    https://docs.unity3d.com/ScriptReference/EventSystems.IPointerClickHandler.html
     
    Last edited: Nov 4, 2018
    MisterSkitz likes this.
  5. MisterSkitz

    MisterSkitz

    Joined:
    Sep 2, 2015
    Posts:
    833
    Code (CSharp):
    1. public class NoIdeaHowToDo
    /Dead lol
     
  6. scott_reece

    scott_reece

    Joined:
    May 21, 2016
    Posts:
    3
    Thank you all for your suggestions, but what I'm trying to do is some kind of ray trace that includes the children of the canvas. Is there such a thing? All the ray tracing code I've tried ignores any children of the canvas.
     
  7. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,153
  8. scott_reece

    scott_reece

    Joined:
    May 21, 2016
    Posts:
    3
    Thanks Vakabaka! That is exactly what I was looking for. Is there a way for me to mark that as the answer?