• 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 JasonBricco · Feb 18, 2014 at 06:03 AM · editorcoordinatesconverting

Problem Converting GUI to World Coords in Editor

Hi everyone,

I'm making a type of board game that I wish to design myself in the editor (there are many types of boards). I'm trying to write a level editor to make this easier.

What I want is to be able to select a type of tile from a list and then click in the scene view to make it appear where I clicked (while snapping into a grid).

I have been able to make it put a tile when I click, however it's putting the tile at an offset (30-40 pixels above where I click). And I can't seem to figure out why.

My code looks like this (didn't get the grid part in yet):

 if (Event.current.type == EventType.MouseUp)
 {
     // load the correct prefab and create an instance of it
     GameObject space = Resources.LoadAssetAtPath(pathString, typeof(GameObject)) as GameObject;
     GameObject spaceInstance = PrefabUtility.InstantiatePrefab(space) as GameObject;
     spaceInstance.name = current;
 
     // set its position to where we click
     Vector2 mousePos = Event.current.mousePosition;
     mousePos.y = Screen.height - mousePos.y;
 
     Vector3 position = Camera.current.ScreenToWorldPoint(mousePos);
     position.z = 0;
     spaceInstance.transform.position = position;
 
     EditorUtility.SetDirty(spaceInstance);
 }

I'm currently working around it simply by subtracting an additional 35 from mousePos.y, but there has to be a better fix than that.

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

2 Replies

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

Answer by Phles · Feb 18, 2014 at 06:32 AM

In the editor I have found Screen.height is often inaccurate, where you have...

mousePos.y = Screen.height - mousePos.y;

Try this instead:

mousePos.y = Camera.current.pixelHeight - mousePos.y;

The camera pixelHeight property should be the real height of the scene view.

Hope this helps

Phill

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
2

Answer by Bunny83 · Feb 18, 2014 at 06:41 AM

You should use the Handles and HandleUtility class for anything related to the SceneView. The problem is that Screen.height returns the windows device context height which includes the tab-bar and the header bar.

SceneViewViewport

The green area is the GUI area while the red one is the actual screen buffer. btw: the green one is enclosed by the red area.

The Handles class also let you draw cameras, handles, gui and other things in your own editor window. Just play around a bit. The documentation is your friend. Just switch to "editor classes" and browse the classes ;)

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

20 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

Related Questions

How determine exact anchor coordinates for Distance Joint 2D 1 Answer

Objects that have been scaled have weird X, Y, Z value in the Scene Editor 2 Answers

PostprocessBuildPlayer wont start on Unity 5.1.1f1, Mac 1 Answer

Change defaults for standard components 1 Answer

From 2D UI coordinates to WorlSpace 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