Search Unity

Resolved HoldDown mouse button to get any UI element?

Discussion in 'Scripting' started by Quast, Oct 8, 2020.

  1. Quast

    Quast

    Joined:
    Jul 5, 2015
    Posts:
    560
    I'm working on 2D game. I'm trying to find a way with rayCast to get the name of any element in my Menu "Canves" by holding down mouse button only. Not by keep clicking on every aspect in my menu. How to do that ?
     
  2. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    If you already have some code that can do that when you click, you can probably take the same code and put it in an Update function (so it runs every frame) and add an "if (Input.GetMouseButton(0))" check to see if the mouse is being held down.

    If you only want this for debugging purposes, then instead of scripting, consider selecting your Event System object and keeping an eye on the bottom of the inspector as you move the mouse around the game view (while in play mode).
     
  3. Quast

    Quast

    Joined:
    Jul 5, 2015
    Posts:
    560
    I have a script but it doesn't do want i need. Maybe I missing something. All UI have 2D collider.
    Code (CSharp):
    1.         if (Input.GetMouseButton(0))
    2.         {
    3.             RaycastHit hit;
    4.             Ray ray = mainCamera.ScreenPointToRay(Input.mousePosition);
    5.             if (Physics.Raycast(ray, out hit))
    6.             {
    7.                 if (hit.collider.gameObject)
    8.                 {
    9.                     Debug.Log("UI : " + hit.transform.name);
    10.                 }
    11.             }
    12.         }
    I need to do this by script.

     
    Last edited: Oct 9, 2020
  4. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    What does it do? (And how does that differ from the desired result?)
     
  5. Quast

    Quast

    Joined:
    Jul 5, 2015
    Posts:
    560
    Antistone, You see something that I don't see. Do i need to use RaycastHit2D ? If you know the answer just write it please.
     
  6. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    Nope. I never use raycasts and couldn't possibly write this code without looking up an example. No idea what's wrong with your code.

    I'm just applying standard debugging rules: If you want help figuring out why something doesn't work, always post the code, always explain what you want it to do, always explain what your current code actually does instead. Many common problems are easy to figure out if you do all of that but very hard to figure out if you don't.

    If you don't know what your current code is doing, start adding Debug.Log calls and/or step through in a debugger.

    The way programming generally works is that there's a million tiny details, and nearly all of them are very easy, but you need to get every single one of them right. One itsy bitsy mistake anywhere along the line and your program doesn't work. And when there's a million things you could have done wrong, it often takes a lot of information to narrow down which one of them you actually did do wrong. Most of debugging is just narrowing down the list of possibilities.
     
  7. Quast

    Quast

    Joined:
    Jul 5, 2015
    Posts:
    560
    Thank you for your words. Actually, I got close to solve this problem.
    But I got again another issue. Here is the new script.
    Code (CSharp):
    1.     public List<GameObject> gList = new List<GameObject>(3);
    2.     public GameObject TY;
    3.  
    4.     // Update is called once per frame
    5.     void Update()
    6.     {
    7.  
    8.         if (Input.GetMouseButton(0))
    9.         {
    10.             Vector2 worldPoint = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    11.             RaycastHit2D hit = Physics2D.Raycast(worldPoint, Vector2.zero);
    12.             if (hit.collider == null)
    13.             {
    14.                 TY = null;
    15.             }
    16.             else
    17.             {
    18.                 TY = hit.collider.gameObject;
    19.                 Debug.Log("UI : " + TY.transform.name);
    20.             }
    21.            
    22.         }
    23.          else
    24.         {
    25.         // clear list
    26.                 gList[0] = null;
    27.                 gList[1] = null;
    28.                 gList[2] = null;
    29.         }
    30.        
    31.     }// end update
    I removed the part of adding UI elements into my list. Because it keep repeating adding the same element twice in my list !! I'm tying to find a new way to solve this now. Still I need help.
     
  8. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    I just gave you a recipe for what information you should post to get help, and you are not following it.
     
  9. Quast

    Quast

    Joined:
    Jul 5, 2015
    Posts:
    560
    I do. Just I want to make the script short when post it here. Anyway, thanks God, I solve this problem by using:
    void OnMouseOver()
    Now my script is more better and easy to read. Thank you man.
     
  10. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    @Quast

    Do you really have Collider2D in your UI element? I'm asking because if you are using UGUI, you don't need colliders. For UI related raycasting you can use GraphicRaycaster component (which you can see in your Inspector screenshot). It only requires Image, text or other UI graphic element for hit testing.

    And if you are using Physics2D raycasting, I wouldn't use it like that - it operates in 2D plane (rays go sideways), not like screen to world ray. For that you need to use different method of Physics2D (GetRayIntersection).

    Also, your Canvas Scaler is set to mode Constant Pixel Size, which is usually a sign of not properly configuring your canvas, which can cause issues when you change screen resolution. Usually it is easiest and most predictable to use Scale With Screen Size mode unless you have some very specific setup.

    I personally haven't used those old OnMouse(Over) methods especially when using UGUI (there is no need).