Hi there.
So here’s my goal : Show players a menu with three buttons by clicking a MenuButton,and if the player click the “X” on top-right of the menu,the menu would be closed.
So I create a GUITexture as the “Open-Menu Button”,and use “OnMouseUpAsButton() - if(GUI.button (new…))” to create other buttons.
(Menu Button will be beneath the menu,and when the button is being clicked,it will turn to green.)
As you can see,although my cursor is on the “X” button,the “MenuButton” is still green,which means I’m still clicking it.
After testing for many times,I found that once the menu is opened,my cursor is just like freeze at the position where I clicked the MenuButton.I have to move my cursor away to make the MenuButton back to red. ( Normal situation,not clcking.)
I’ve tried “gameObject.guiTexture.enabled = false when clicking the button”,but still in vain.
Because even though I enable the guiTexture of the MenuButton successfully,I can still hear the “Clicking Sound” attached to it.
Getting stuck here for about two hours but got nothing,need help here.
Here’s my code ( ClickingSound is NOT included.)
(I add “Press Escape to close the menu” in and it worked,but mouse click still not.)
(OnMouseDown() can make this all work,but I’m wondering why OnMouseUpAsButton() can’t.)
using UnityEngine;
using System.Collections;
public class ButtonOption : MonoBehaviour {
public bool MenuIsActive = false;
void OnMouseUpAsButton()
{
MenuIsActive = true;
}
void OnGUI()
{
if ( MenuIsActive == true )
{
GUI.Box (new Rect(500,280,400,400),"");
GUI.Label ( new Rect(635, 315, 200, 200),"Options", BoldFont);
if(GUI.Button(new Rect(865,290,26,26), "X") || (Input.GetKeyDown(KeyCode.Escape)) )
{
MenuIsActive = false;
}
if(GUI.Button(new Rect(650,410,100,50),"Button 1"))
{
//Do Something here.
}
if(GUI.Button(new Rect(650,500,100,50),"Button 2"))
{
//Do Something here.
}
if(GUI.Button(new Rect(650,590,100,50),"Button 3"))
{
//Do Something here.
}
}
}
}