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
3
Question by yoyo · Feb 22, 2011 at 09:48 PM · editorscenemenu-item

How can I find the world position of the center of the Scene view?

I have a custom editor menu that creates new objects in the world. I would like the new objects to appear in the middle of the current scene view. My world is primarily a flat ground plane at y = 0, so I thought HandleUtility.GUIPointToWorldRay plus Plane.Raycast would do the trick, as per code fragment below.

private static Vector3 GetViewCenterWorldPos() { Vector2 screenCenter = new Vector3(Screen.width/2.0f, Screen.height/2.0f); Ray worldRay = HandleUtility.GUIPointToWorldRay(screenCenter); Plane groundPlane = new Plane(Vector3.up, Vector3.zero); float distanceToGround; groundPlane.Raycast(worldRay, out distanceToGround); Vector3 worldPos = worldRay.GetPoint(distanceToGround);

 return worldPos;

}

Everything looks reasonable -- screenCenter is correct for the Scene view, and worldPos has y = 0, as expected for a ground plane intersection. However, when I place an object at the worldPos location, it appears just offscreen above and left of the Scene view. This is consistent wherever I move the camera, the new object is always above and left.

Turns out that finding the worldRay like this works perfectly:

Ray worldRay = Camera.current.ViewportPointToRay(new Vector3(0.5f, 0.5f, 1.0f));

but only when Camera.current is accessible, which is true only if the Scene view was the most recently selected tab before I selected my custom menu item.

I also sometimes get this message:

Unable to convert GUI point to world ray if a camera has not been set up!
UnityEditor.HandleUtility:GUIPointToWorldRay(Vector2)

Overall it seems that either HandleUtility.GUIPointToWorldRay has a bug, or that I can't use it reliably from a custom menu item.

Advice appreciated.

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 yoyo · Feb 22, 2011 at 09:59 PM 0
Share

Aha, one final google turned up a forum post with this undocumented feature -- SceneView.lastActiveSceneView.camera. Using this camera did the trick.

1 Reply

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

Answer by yoyo · Feb 24, 2011 at 12:06 AM

This forum post revealed the undocumented feature I needed. Replacing the appropriate line above with the following did the trick:

Ray worldRay = SceneView.lastActiveSceneView.camera.ViewportPointToRay(new Vector3(0.5f, 0.5f, 1.0f));
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

No one has followed this question yet.

Related Questions

Scene Editor Problem - Unable to Move Objects 3 Answers

Saving a scene within the game? 1 Answer

Objects that appear in the editor's game view do not appear in the game build 0 Answers

Unity Scene View's right-click + WASD doesn't look 3D anymore? 2 Answers

Stop Unity snap settings rounding to nearest hundred 2 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