- Home /
catch GUI click to avoid raycasting after a click? pls help
Hey guys ! i'm new to this, so here is your chance ! Do a good deed and help a noob to get the "noob+1" achievement;)
i'm constantly run into the same problems with my projects -.-' ... i want to select an object in my scene by clicking it. After that there should appear a GUI menu where i could do any kind of stuff. Clicking and selecting a building is no problem i found lots of help for that. But when i try to click a button at the menu my code recognizes a click, casts a ray, deselect my object and ignoes the GUI.Button click -.-' now i need a solution for taht ... :-/ at my last project i listened for any click above the menu rect but now i have a dynamic and dragable menu and this solutions seems a bit sloppy to me ;) Now i tried to set a bool after the button click and check for that in SelectionManager() but it seems that the window disappears before the button click happens -.-' A friend of mine mentioned the event.use stuff .. but i don't get that at all and it isn't working (because i have no idea how to use that) o.0
here is my code of my menu script:
using UnityEngine;
using System.Collections;
public class BuyBuilding : MonoBehaviour {
private Building building;
private const int BUY_WINDOW_ID = 0;
private const string BUY_BUTTON_LABEL = "Buy Building";
private const string BUY_WINDOW_LABEL = "Buy ";
private Rect _buyWindowRect = new Rect( 10,100, 150,250);
private void Start()
{
building = gameObject.GetComponent<Building>();
}
private void OnGUI()
{
if(building._isSelected)
{
_buyWindowRect = GUI.Window( BUY_WINDOW_ID, _buyWindowRect, ShowBuyWindow, BUY_WINDOW_LABEL + building.name);
}
}
private void ShowBuyWindow(int windowID)
{
if(GUI.Button(new Rect(0, _buyWindowRect.height-25, _buyWindowRect.width, 25), BUY_BUTTON_LABEL))
{
building.gm.anyButtonPressed = true;
building.SetBuildingStatus(Building.BuildingStatus.PlayerBuilding);
PlayerClass.curMoney -= building.curBuildingValue;
Destroy(this);
}
GUI.DragWindow();
}
}
and the Manager script :
public GameObject selectedObject;
public bool anyButtonPressed = false;
void Update () {
SelectionManager();
}
private void SelectionManager() {
if(Input.GetMouseButtonDown(0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
Debug.Log("checking for hit");
RaycastHit hit;
if(Physics.Raycast(ray, out hit, 100))
{
if(anyButtonPressed)
{
Debug.Log("returning");
return;
}
else
{
if(hit.transform.tag == "Building")
{
if(selectedObject!= null)
{
if(selectedObject != hit.transform.gameObject)
{
selectedObject.SendMessage("SelectBuilding", false);
selectedObject = hit.transform.gameObject;
selectedObject.SendMessage("SelectBuilding", true);
}
else
{
return;
}
}
Debug.Log("Hit Building");
selectedObject = hit.transform.gameObject;
hit.transform.SendMessage("SelectBuilding", true);
}
else
{
if(selectedObject!= null)
{
selectedObject.SendMessage("SelectBuilding", false);
Debug.Log ("Deselect " + selectedObject.name);
}
selectedObject = null;
}
Debug.Log("hit");
}
}
}
}
a .. an answer in c# would be AWESOME :D hope you hav a heart for a helpless little newbie :-)
Best and thanks in advance!! Cxyda
Answer by robertbu · Jun 03, 2013 at 04:11 AM
There are a number of ways you can approach this problem, and which one depends on how you want your menus to function.
The easiest would be to disable selecting in the manager script if something is already selected. Then in the BuyBuilding class, the object would be deselected when the menu is taken down.
Another way would be to communicate between the selecting code and the Manager script. You already do so between the Building script and the BuyBuilding script. The mechanism is just the same.
Another way would be to use C# events and delegates. Anytime a menu comes up or goes down, a event would fire, and any code that was interested could take note.
And finally here is a hackish way. Place a box collider in front of the camera and make it a child of the camera. Any time a menu comes up, the box collider would be enabled. The selection code would bail out if it hit the box collider.
Hi ! THX for you answer. I dont want to use the solution 1 and 4 :-) But 2 and 3 sound interesting. I tried to understand the events / delegates thing but i still dont get it :(
What do you mean with "communicate" at solution 2 ? I made a bool (button_clicked) and set it to true when i click a button but that code never gets executed because the Raycast deselects the object and the GUI menu disappears :(
Do you have any tutorial links or sample code for that problem?
Thanks Cxyda
Here is a video tutorial on Events and Delegates:
http://www.youtube.com/watch?v=N2zdw$$anonymous$$IsXJs
As for solution #2, if I were "hacking" it into existing code, I'd add the following method to the BuyBuilding class:
public bool IsGUIHit() {
if (building._isSelected) {
Vector3 v3Pos = Input.mousePosition;
v3Pos.y = Screen.height - v3Pos.y;
return _buyWindowRect.Contains(v3Pos);
}
return false;
}
Just as you've created a reference to the Building class in BuyBuilding class, you would also create a reference to the BuyBuilding class in the $$anonymous$$anager class. And then you could do something like:
if(!buyBuilding.IsGUIHit() && Physics.Raycast(ray, out hit, 100))
nice ! Thanks for the video tutorial!! I watched it and it's getting clearer :-) and it works now !
Thanks!
Cxyda and robertbu, thank you so very muchly for this!
I currently have like 12 different threads upon right now for solutions to this problem, and this was the only one that actually managed to solve this for me completely!
robertbu you are a champion!
Thanks both for this posts and the answers and comments, this has been hugely helpful to me!
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
GUI, SpellBar and C#-related questions 1 Answer
How to check if a button left the boundaries of a Viewport in ScrollRect? 1 Answer
OnMouseUp() not working like meant 1 Answer