Search Unity

[2D - GUI] Clickable Menus on GameObjects - Disappear when clicked outside gameobject.

Discussion in 'Community Learning & Teaching' started by CarterG81, Aug 15, 2014.

  1. CarterG81

    CarterG81

    Joined:
    Jul 25, 2013
    Posts:
    1,773
    It took me a bit looking into Physics2D.Raycast to figure this out.

    You cannot do this in OnMouseDown, and IMO the entire idea of OnMouseDown for use with a GUI is a bad idea from the start. Then again, the entire GUI is pretty awful. Until they update it (or if the update doesn't really help much) here is a script to...

    1) Have Clickable GameObjects, that when clicked - open up a menu.
    2) When you click outside the gameobject, the menu disappears. So if you have multiple gameobjects of the same type, when you click on one the menu will appear (ex. to click GUI buttons to perform actions in game), and when you click a different gameobject, that menu will disappear. If that different gameobject has this script also, then it will start the same menu. You will never have more than one menu opened for gameobjects with this same script.



    This has a use, if you would want to have the player click on items in a 2D game, and once the item is clicked, do actions like "Take Item", "Destroy Item", "Inspect Item".

    Extremely useful for games, but a PITA for me to figure out (just like all things dealing with Unity's awful GUI).
    To be honest, I don't even understand why a GUI was even created when one could simply use gameobjects.

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class ClickableGameObject: MonoBehaviour
    6. {
    7.    bool GUIon = false;
    8.  
    9.    // Update is called once per frame
    10.    void Update ()
    11.    {
    12.        if(Input.GetMouseButtonUp(0))
    13.        {
    14. //Cast the Ray
    15.          RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);
    16.  
    17. //If NOTHING is hit
    18.          if(!hit)
    19.            GUIon = false;
    20.  
    21. //If SOMETHING is hit
    22.          if(hit)
    23.          {
    24. //If that "something" hit is THIS gameobject
    25.            if (hit.collider.gameObject == this.gameObject)
    26.            {
    27.              GUIon = true;
    28.            }
    29. //If that "something" hit is a DIFFERENT gameobject
    30.            else
    31.            {
    32.              GUIon = false;
    33.            }
    34.          }
    35.  
    36.  
    37.  
    38.        }
    39.  
    40.    }
    41.  
    42.    void OnGUI()
    43.    {
    44.  
    45.      if(GUIon)
    46.      {
    47.        Vector3 world2screen = Camera.main.WorldToScreenPoint(this.transform.position);
    48.        Rect rect = new Rect (world2screen.x - 75 ,world2screen.y * -1 - 30 + Screen.height, 150, 60);
    49.  
    50.         if(GUI.Button(rect, "Take Itemt"))
    51.          {
    52.          }
    53.      }
    54.    }
    55.  
    56. }
    57.  
    58.  
     
    Last edited: Aug 15, 2014