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
0
Question by GADoyle · Mar 16, 2013 at 10:08 PM · editorraycastscreenpointtorayonscenegui

Scripting a Click event in the Unity Editor

I'm trying to create a system to allow my fellow developers and me to place and position a bunch of objects within a given planar grid space, by clicking on it in the scene view of editor.

To do this, I have created a BoxCollider that is the exact size of the grid, to register the click. I would like to call the BoxCollider's Raycast function to then register the click. I have created a custom editor class to go along with it as well.

It seems that the best way to check for a click event would be in the OnSceneGUI function of my editor class. However, I have not been able to get any results from this approach.

As you can see in the code below, I have several methods to get the proper ray from the mouse position:

Camera.main.ScreenPointToRay (Input.mousePosition);

Camera.current.ScreenPointToRay (Input.mousePosition);

HandleUtility.GUIPointToWorldRay(Input.mousePosition);

none of these were able to register any collision with the boxCollider.Raycast function. Also, when the colision does happen.

Is OnSceneGUI the right place to try to be doing this?

How can I get a working ray from the mouse position, while in the Editor in the Scene view?

Also I am aware I am not checking for the actual click yet, leaving it out made it easier to identify that the ray is the issue.

Here is the code:

 function OnSceneGUI(){
             
             transform = (target as Transform);     
                 
             var ray : Ray ;//= Camera.current.ScreenPointToRay (Input.mousePosition);
              var hit : RaycastHit;
              
              Debug.Log("ONSCENEGUI COLLISION CHECK TIME!!");
              
              //ray = Camera.main.ScreenPointToRay (Input.mousePosition);
              //ray = Camera.current.ScreenPointToRay (Input.mousePosition);
              ray = HandleUtility.GUIPointToWorldRay(Input.mousePosition);
              
              
              if (boxCollider.Raycast (ray, hit, 100000.0)) {
             
                 var pos =  transform.InverseTransformPoint(hit.point);
             
                  var gridPos:Vector2 = new Vector2(pos.x / scaleProp.intValue , pos.y / scaleProp.intValue);
              
                  Debug.Log("CLICK ON SHIP GROUP AREA: "+ pos);
              
                  Debug.DrawLine (ray.origin, hit.point);
              }
             
         }
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

Answer by nsxdavid · Mar 16, 2013 at 11:02 PM

This'll give you an idea how to get a ray in editor mode (in C#):

 void OnSceneGUI()
 {
     Event e = Event.current;
     Ray ray = Camera.current.ScreenPointToRay(e.mousePosition);
     Debug.DrawRay(ray.origin, ray.direction, Color.blue,10);
 }
Comment
Add comment · Show 2 · 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 Psyco92 · Sep 29, 2014 at 03:44 PM 0
Share

This does absolutely nothing in my scene. Is there something else I need to do to get that to work? $$anonymous$$ust i tell the script to ExecuteInEdit$$anonymous$$ode?

Any advice is appreciated thanks :)

avatar image Louis Watson · Sep 29, 2014 at 04:01 PM 0
Share

yes you need ExecuteInEdit$$anonymous$$ode for OnSceneGui to be called.

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

11 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

Related Questions

OnSceneGUI has odd behaviour with multiple objects selected 1 Answer

Raycast hit done wrong using ScreenPointToRay coordinates 2 Answers

Triangles Index always zero - Custom Editor 1 Answer

Raycast to middle screen doesn't work 1 Answer

Animation stops Recast from working 0 Answers


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