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 ();
			}
		}
	}
}

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!

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.