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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Need Help On Knowing if Click happen on GUI. (OnGUI)

Discussion in 'Scripting' started by Korigoth, Sep 10, 2015.

  1. Korigoth

    Korigoth

    Joined:
    Jul 21, 2014
    Posts:
    105
    Hi everyone,

    I'm trying to block my Click Input when i do a Click on a GUI. (OnGUI)

    OnGUI doesn't block the GetMouseButtonDown in other Script.
    So i need to find a way to tell my other Script to not execute this click!

    I can do something like that =>
    Input.GetMouseButtonDown(0) && (MouseNotOnGUIFunction || Model.Variable.ToTellThatMouseIsNotOnGUI)

    but i dont know how i can tell my mouse is over a GUI.

    It's a bit breaking the game if i dont implement this because its a Click to Move to MousePosition. So if we click OnGUI, the game move the player and we doesn't want this behaviour!

    Some help would be awesome :)
     
  2. gcoope

    gcoope

    Joined:
    Dec 20, 2012
    Posts:
    11
    Quite tricky with the old OnGUI - have you considered upgrading to the 4.6+ uGUI? There's some great tutorials on how to use it and uprgade from the old OnGUI.
     
  3. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,744
    As @gcoops says, with the old GUI, it's an ordeal. Basically, for each window or control you want to block interactions, somewhere in OnGUI, you have to check to see whether Event.current.mousePosition is inside that Rect. Then you set some global isMouseBlockedByGUI boolean, and check against that before you do mouse interaction.

    ....but yeah you should definitely consider using Unity UI, it's precisely 27 times better than OnGUI.
     
  4. Korigoth

    Korigoth

    Joined:
    Jul 21, 2014
    Posts:
    105
    Thx for the answers,

    i will switch the GUI System in my 0.2.0 or 0.3.0 release at the moment im more on a Prototype so i need to show something pretty quickly and the new GUI i'm not familiar with it yet.

    I found a way to do it, it's not pretty Clean but will do the job for the 0.1.0 Prototype.