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
1
Question by Cxyda · Jun 02, 2013 at 08:15 PM · c#guiraycast

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

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

1 Reply

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

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.

Comment
Add comment · Show 4 · 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 Cxyda · Jun 09, 2013 at 07:43 PM 1
Share

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

avatar image robertbu · Jun 09, 2013 at 09:17 PM 1
Share

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

avatar image Cxyda · Jun 09, 2013 at 09:45 PM 1
Share

nice ! Thanks for the video tutorial!! I watched it and it's getting clearer :-) and it works now !

Thanks!

avatar image Radact · May 01, 2014 at 02:52 PM 0
Share

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

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

15 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

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


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