Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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
2
Question by Darn_ · Jul 15, 2017 at 02:29 PM · editorwindowscene viewmouse positionhandle

Trying to get mouse location in world while in scene, HandleUtility.GUIPointToWorldRay not helpful

I want to make a tile editor so I need to get where my cursor is pointing at in the world in the scene window. This is what I tried:

  public class MyEditor : EditorWindow {
      private static void TilemapEditor()
      {
          EditorWindow.GetWindow(typeof(RoomEditor));
      }
      public void OnInspectorUpdate()
      {
          Repaint();
      }
  void OnGUI()
  {
      //other stuff here, not relevant to my issue
      Debug.Log(HandleUtility.GUIPointToWorldRay(Event.current.mousePosition));
  }

HandleUtility.GUIPointToWorldRay simply doesn't work, console error being "Unable to convert GUI point to world ray if a camera has not been set up!". Any way to fix that or a different way of doing what I want?

Comment
Add comment · Show 1
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 davidrochin · Apr 23, 2018 at 11:22 PM 0
Share

Same problem here...

2 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by stektpotet · Aug 04, 2018 at 02:50 PM

The problem here is (afaik) that during OnGUI you don't have an "active camera", as EditorWindow's aren't at all connected with the SceneView. You might be able to call

 Vector3 mousePos = Event.current.mousePosition;

The mouse position must be flipped into ScreenSpace because we only have access to the ScreenPointToRay-function, which is similar to the GUIPointToWorldRay, except it takes use of screenspace:

 mousePos.y = SceneView.lastActiveSceneView.camera.pixelHeight - mousePos.y;

Then we can create the ray:

 Ray ray = SceneView.lastActiveSceneView.camera.ScreenPointToRay(mousePos);

if you however want to incorporate more GUI/Handles into the sceneview itself for the EditorWindow, I reccoment to do this:

 public class SceneInteractingWindow : EditorWindow
 {    
    private void OnEnable()
    {
       //register that OnSceneGUI of this window 
       //should be called when drawing the scene.
       SceneView.onSceneGUIDelegate += OnSceneGUI;
    }
    private void OnDisable()
    {
       //cleanup: when the window is gone
       //we don't want to try and call it's function!
       SceneView.onSceneGUIDelegate -= OnSceneGUI;
    }
    private void OnSceneGUI(SceneView sv)
    {
       //DO SceneGUI stuff here...
       //for example something like this:
       Vector3 mousePos = Event.current.mousePosition;
       mousePos.y = sv.camera.pixelHeight - mousePos.y;
       Ray ray = sv.camera.ScreenPointToRay(mousePos);
       RaycastHit hit;
       if (Physics.Raycast(brushRay, out hit))
       {
          Handles.color = Color.red;
          Handles.DrawWireDisc(hit.point,hit.normal, 5);
          sv.Repaint();
       }
    }
 }




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
avatar image
0

Answer by Bunny83 · Aug 04, 2018 at 04:22 PM

The Handles are mainly meant for the usage within the SceneView so those methods are usually used inside any OnSceneGUI callbacks. If you want to work with handles in your own editor window you have to use Handles.SetCamera to provide a reference camera view.


GUIPointToWorldRay simply does this:

 public static Ray GUIPointToWorldRay(Vector2 position)
 {
     Ray result;
     if (!Camera.current)
     {
         Debug.LogError("Unable to convert GUI point to world ray if a camera has not been set up!");
         result = new Ray(Vector3.zero, Vector3.forward);
     }
     else
     {
         Vector2 v = HandleUtility.GUIPointToScreenPixelCoordinate(position);
         Camera current = Camera.current;
         result = current.ScreenPointToRay(v);
     }
     return result;
 }


Keep in mind that the resulting Ray is not a "position" but just a ray in worldspace. Like "stektpotet" said if you want to actually work in the sceneview you want to execute your code in the sceneview callback where the sceneview camera is the active one by default. Keep in mind that there could be more than one sceneview open at the same time. Inside the SceneView gui function you can use

 HandleUtility.GUIPointToWorldRay(Event.current.mousePosition)

just fine.

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

71 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Get the Scene View Position Relative to Camera View Position on Mouse Click 1 Answer

Mouse Wrapping Near Edge of Play Space 0 Answers

Custom scene view in editor window 2 Answers

Can't move GameObject in Scene view with Unity Components attached 2021.2.2f 0 Answers

Get Project View position in editor 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