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 dju4ic · Jun 12, 2011 at 12:03 AM · guiraycastguitexture

Yet Another GUI vs. Raycasting question.

I am moving forward with my dungeon/housing creation system and have come across another issue, I have read a few ways on here regarding making gui ignore raycast. The method I was attempting was somewhat as follows:

 if (Event.current != null && Event.current.isMouse)
 {
     ignoringRaycast = true;
     Debug.Log("Ignoring Raycast - true");
 }
 else {
     ignoringRaycast = false;
     //Debug.Log("Ignoring Raycast - false");
 }

It worked great for my background gui elements, but buttons would still fire through on clicking them. I have read about using a GUI Texture behind it but have yet to see any examples. Also had no like trying the tooltip method - I was hoping to be pointed in the right direction or hopefully someone has a working project that tackles this?

I can post the code if necessary although I removed everything that had to utilize this, it is quite large and tangles between many different scripts so will be quite a bit of work just to make readable.

Comment
Add comment · Show 7
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 dju4ic · Jun 12, 2011 at 04:50 AM 0
Share

I've had some advice to use "Event.current.Use();" within the if blocks of the buttons, this does not work either.

avatar image dju4ic · Jun 12, 2011 at 06:27 AM 0
Share

I have also had a comment on this regarding what i actually need to do.. basically.. If clicking a button, ignore raycast.. which will ignore the preinstantiate and creation.. sorry if i was vague.. if any more inf is needed please ask

avatar image dju4ic · Jun 13, 2011 at 12:12 AM 0
Share

another update, i have not also tried GUILayer.HitTest(); but it as well always thinks the mouse is over the GUI layer no matter where it is on screen.. Still no other responses? please, if i need to clarify the issue any more, just ask what you need from me. thanks.

avatar image aldonaletto · Jun 13, 2011 at 01:36 AM 0
Share

I run your game, but I didn't understand exactly what you want to do. Do you want to click on the terrain even under the buttons? Or do you want to stop raycasting when the mouse pointer is over the buttons?

avatar image dju4ic · Jun 13, 2011 at 01:41 AM 0
Share

Stop raycasting when over the buttons or all GUI elements.

Show more comments

2 Replies

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

Answer by aldonaletto · Jun 13, 2011 at 02:48 AM

You can define two rectangles (one for each menu) in pixel coordinates, and disable raycasting when the mouse pointer is inside any of these rectangles:

 var rMenu1 = Rect(20,200,180,400);
 var rMenu2 = Rect(100,80,500,170);
 
 function Update(){
   ...
   var mousePos = Input.mousePosition;
   ignoringRaycast = rMenu1.Contains(mousePos) || rMenu2.Contains(mousePos);
   ...

I'm assuming that ignoringRaycast == true disables raycasting.
If you're using GUI.Box to draw the menus, rMenu1 and rMenu2 will be the same rects that define the vertical and horizontal menus - or you can even define bigger rects, so raycasting will be disabled when the mouse gets near to the menus.

Comment
Add comment · Show 1 · 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 dju4ic · Jun 13, 2011 at 03:09 AM 0
Share

great.. I knew it was simple, I was just looking in every wrong direction. Very greatly appreciated.

avatar image
1

Answer by JasonM · Jul 22, 2011 at 06:07 PM

Your solution works, but for if you are using a GUILayout window (like one that can be dragged around) youll find that the y position is wrong. heres my quick fix if someone is still struggling

 var windowRect = new Rect(0,0,200,200);
 mousePos.x = Input.mousePosition.x;
 mousePos.y = Screen.height - Input.mousePosition.y;
 ignoreRaycast = windowRect.Contains(mousePos);
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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Smart Crosshair 4 Answers

Get GUITexture to follow ScreenPointToRay ray cast 1 Answer

Reduce Draw call for Multiple GUI Textures with same Texture 1 Answer

How will i get animated gif images in scene? 6 Answers

GUI texture touch input problem 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