Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 areFranz · Sep 28, 2015 at 12:04 PM · c#guibuttonmouserelease

How to fire gui button on mouse right release

Hi, I have a menu that shows on right clock and i need that when i release (mouseup) the right mouse button, it fires a gui button (used to change weapons). Here is the actual code:

 using UnityEngine;
 using System.Collections;
 
 namespace SpaceShooter
 {
     public class MainGUI : MonoBehaviour {
 
         public Texture2D weponsTex;
         public Texture2D cursorTex;
         public GUIStyle laserBtn;
         public GUIStyle plasmaBtn;
         public GUIStyle bombBtn;
 
         private Rect weaponsRect;
         private Rect laserRect;
         private Rect plasmaRect;
         private Rect bombRect;
         private bool isDown = false;
 
         void OnGUI ()
         {
             float width = Screen.width / 2;
             float height = Screen.height / 2;
 
             weaponsRect = new Rect (width - 128, height - 88, 256, 176);
             laserRect = new Rect (96, 8, 64, 64);
             plasmaRect = new Rect (192, 104, 64, 64);
             bombRect = new Rect (0, 104, 64, 64);
 
             if (GameManager.instance.GetMenu () == GameManager.Menu.WEAPONS)
             {
                 GUI.BeginGroup (weaponsRect, weponsTex);
                 GUI.Button (laserRect, "", laserBtn);
                 if (GUI.RepeatButton (plasmaRect, "", plasmaBtn))
                 {
                     isDown = true;
                 } 
                 else if (isDown == true && Event.current.type == EventType.MouseUp)
                 {
                     Debug.Log ("Plasma");
                     isDown = false;
                 }
                 GUI.Button (bombRect, "", bombBtn);
                 GUI.EndGroup ();
             }
         }
     }
 }
Comment
Add comment · Show 1
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 Ali-hatem · Sep 28, 2015 at 02:22 PM 0
Share

http://docs.unity3d.com/ScriptReference/$$anonymous$$onoBehaviour.On$$anonymous$$ouseUp.html

2 Replies

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

Answer by areFranz · Sep 28, 2015 at 10:17 PM

I've found the problem using Events like:

 Event e = Event.curent;
 
 if ((e.type = EventType.MouseUp) && rect.Contains (Event.current.mousePosition)) {
 // code to run
 }

That with absolute values in Rects (not Groups).

But your solution is interesting...

Anyway thanks for your attenction.

Comment
Add comment · 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
0

Answer by Ricna · Sep 28, 2015 at 04:25 PM

I didn't understand where should be the mouse to call the menu, seems that it come from the GameManager.instance. GetMenu()...

Whatever... Considering that you click in some button, all that you need is a a flag to define if the application must show the menu. To define it you'll verify the Input.GetMouseButtonUp(1) value.

Something like this:

 using UnityEngine;
 using System.Collections;
 
 namespace SpaceShooter{    {
     public class MainGUI : MonoBehaviour {    
         //----------------------------------------------------------
         // ATTRIBUTES
         //----------------------------------------------------------

         public Texture2D weponsTex;
         public Texture2D cursorTex;
         public GUIStyle laserBtn;
         public GUIStyle plasmaBtn;
         public GUIStyle bombBtn;


         private Rect weaponsRect;
         private Rect laserRect;
         private Rect plasmaRect;
         private Rect bombRect;
         private bool isDown = false;    

         private bool showWeaponsMenu = false;

         //----------------------------------------------------------
         // UNITY EVENTS
         //----------------------------------------------------------

         public void Update(){
             if (!showWeaponsMenu){ 
                 VerifyReleaseOfRightMouseButton (); 
                 /* 
                  * This method ^ must be called if the mouse is over 
                  * the button or object where the mouse should be.
                  * Probably you will need add some "&&" in the condition above. 
                 */
             }
         }

         public void OnGUI(){
             //... 
             if (showWeaponsMenu){
                 OnGUICodeToShowTheWeaponsMenu();//TODO Code to show the your menu ;)
             }
             //..
         }

         //----------------------------------------------------------
         // METHODS - PUBLIC
         //----------------------------------------------------------

         //Should be called when you want to hide the menu again. 
         public void HideWeaponsMenu(){
             //TODO Code to apply effect or whatever you want or need to do before to hide the menu.
             showWeaponsMenu = false;
         }

         //----------------------------------------------------------
         // METHODS - PRIVATE
         //----------------------------------------------------------
         private bool VerifyReleaseOfRightMouseButton(){
             showWeaponsMenu = Input.GetButtonUp(1);
             //Maybe you'll wish to check if the left button is not been used.
             //Example: showWeaponsMenu = Input.GetButtonUp(1) && !Input.GetButton(0);
         }

         //Method to be called in OnGUI event to show the Weapons Menu.
         private void OnGUICodeToShowTheWeaponsMenu(){
             GUI.BeginGroup (weaponsRect, weponsTex);
             GUI.Button (laserRect, "", laserBtn);
             if (GUI.RepeatButton (plasmaRect, "", plasmaBtn)){
                 isDown = true;
             } 
             else if (isDown == true && Event.current.type == EventType.MouseUp){
                 Debug.Log ("Plasma");
                 isDown = false;
             }
             GUI.Button (bombRect, "", bombBtn);
             GUI.EndGroup ();
         }
     }
 }


PS: Try to avoid the OnGUI. Start to use the new Unity UI system ;). Good luck!

Comment
Add comment · 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

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Overlapping GUI Button priority 3 Answers

GUI.Button on MouseHover 1 Answer

GUI Button OnMouseUp? 2 Answers

How to make a or array of GUI.Button's? 1 Answer

Instantiate and Scale with Mouse Click. 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