- Home /
Don't register Mouse Clicks through GUI.
Hello, I have this code. The code is creating a window and pausing the game, the window contains the skills I want to increment. The menu is working great, I only have a problem with it.
When I want to increment the skills the mouse clicks registers through the GUI into the world and I want to prevent that, I only want the clicks to register in the GUI when the GUI is displayed.
Can someone help ? :)
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class LevelUpSystem : MonoBehaviour {
// Values For Attributes
[HideInInspector]
public int attack, defence, manaPool, manaRegen;
private int lvl;
private const int STARTING_POINTS = 3;
[HideInInspector]
public int currXP = 0;
[HideInInspector]
public int maxXP = 40;
private int pointsLeft;
public GUISkin mySkin;
public int xpIncrement = 20;
public Texture2D levelUpTexture;
public Texture2D cancelButton;
public Texture2D acceptButton;
public GameObject levelUpEffect;
private List<Skills> skills;
/**********************************
* Character Window Variables
* ******************************/
private bool displayCharacterWindow = false;
private int charWindowID = 2;
private Rect characterWindowRect = new Rect (Screen.width/3,Screen.height/3, 512, 512 );
private int characterPanel = 0;
private string[] characterPanelNames = new string[]{"Attributes","Skills"};
private ProjectileDirectDamage damageRec;
private PlayerHPMPSystem manager;
private bool levelUp;
void Start()
{
levelUp = false;
manager = GameObject.FindGameObjectWithTag("Player").GetComponent<PlayerHPMPSystem>();
skills = new List<Skills>();
skills.Add(new Skills("Attack", attack, 10));
skills.Add(new Skills("Defence", defence, 1));
skills.Add(new Skills("Mana Pool", manaPool, 10));
skills.Add(new Skills("Mana Regen", manaRegen, 1));
pointsLeft = STARTING_POINTS;
}
void Update ()
{
if (currXP >= maxXP)
{
LeveledUpSystem();
}
if(Input.GetButtonDown("CharacterWindow"))
{
ToggleAttributes();
}
}
void LeveledUpSystem()
{
levelUp = true;
GameObject myLevelUpEffect = Instantiate(levelUpEffect,transform.position,transform.rotation) as GameObject;
myLevelUpEffect.transform.parent = transform;
currXP = 0;
maxXP = maxXP+xpIncrement;
lvl++;
pointsLeft += 3;
manager.maxHealth = manager.maxHealth + 20;
manager._currHealth = manager.maxHealth;
}
void OnGUI()
{
#region Character Window Display
GUI.skin = mySkin;
if (displayCharacterWindow)
{
characterWindowRect = GUI.Window(charWindowID, characterWindowRect, CharacterWindow, "Character" );
}
#endregion
GUI.Box(new Rect(5,100,40,20), "XP");
GUI.Box(new Rect(5,120,40,20), "Level");
GUI.Box(new Rect(45,100,100,20), currXP+"/"+maxXP);
GUI.Box(new Rect(45,120,100,20), lvl+" ");
if (levelUp == true)
{
GUI.Label(new Rect (50,350,100,25), "Level Up");
if (GUI.Button(new Rect (50,300,50,50), levelUpTexture))
{
ToggleAttributes();
}
}
if (pointsLeft <= 0)
{
levelUp = false;
}
}
public void CharacterWindow(int id)
{
characterPanel = GUI.Toolbar(new Rect(5,20,characterWindowRect.width - 10, 30), characterPanel,characterPanelNames);
switch(characterPanel)
{
case 0:
DisplayAttributes();
break;
case 1:
DisplaySkills();
break;
}
GUI.DragWindow();
}
private void ToggleAttributes()
{
displayCharacterWindow = !displayCharacterWindow;
if (displayCharacterWindow)
{
Time.timeScale = 0f;
}
else if (!displayCharacterWindow)
{
Time.timeScale = 1f;
}
}
private void DisplayAttributes()
{
GUI.BeginGroup(new Rect(5,70, characterWindowRect.width-10, characterWindowRect.height-70));
GUI.Label(new Rect(10,0,100,25), "Points Left"+pointsLeft);
for (int i = 0; i < skills.Count; i++)
{
GUI.Label(new Rect(0,40 + (i*25),100,25), skills[i].name );
GUI.Label(new Rect(80,40 + (i*25),100,25), skills[i].value.ToString() );
//Buttons
if(GUI.Button(new Rect(100,40 + (i* 25),25,25), "+"))
{
if (pointsLeft > 0)
{
skills[i].LevelUp();
pointsLeft--;
}
}
}
if(GUI.Button(new Rect (characterWindowRect.width/2,characterWindowRect.height/2, 198,63), cancelButton))
{
ToggleAttributes();
}
if(GUI.Button(new Rect (characterWindowRect.width/10,characterWindowRect.height/2, 198,63), acceptButton))
{
ToggleAttributes();
}
GUI.EndGroup();
}
private void DisplaySkills()
{
Debug.Log("Displaying Skills");
}
}
A hack solution is to put a box collider behind your window at the near clip plane of the camera and childed to the camera. The collider is enabled any time your window is enabled.
This question has come up a number of times in the last year, but I don't remember the other solutions.
Thank you for the answer, I'll keep in $$anonymous$$d the hack, but maybe a solution with the GUI will present itself :).
Thanks anyway. :)
Answer by Cherno · Apr 28, 2014 at 01:35 AM
Use (Rect).Contains(Event.current.mousePosition) to check if your mouse cursor is inside a GUI box, and set a boolean variable like "mouseOverWindow" to true if it is. Then you check all mouse clicky in your main, non-GUI view against this variable.
Alternatively, if it's just about clicking buttons, you can also use GUIUtility.hotControl.
Your answer
![](https://koobas.hobune.stream/wayback/20220613143705im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
What's a good book to learn scripts for Unity? 1 Answer
Scaling the space between GUI elements 2 Answers
Tutorials for 2D game GUI 2 Answers
Error on movement script. 1 Answer