Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Giakaama · Apr 27, 2014 at 01:23 PM · guibuttonsmouseclickthrough

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");
     }
 }
 
Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image robertbu · Apr 27, 2014 at 02:38 PM 0
Share

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.

avatar image Giakaama · Apr 27, 2014 at 02:49 PM 0
Share

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. :)

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Giakaama · Apr 28, 2014 at 08:56 AM 0
Share

GUIUtility.hotControl it works, thank you :)

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

I want to move my cube using GUI buttons? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges