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 /
  • Help Room /
avatar image
0
Question by a-dibacco · Dec 10, 2019 at 03:34 PM · 2d gamecoordinates

Unity UI extensions mouse drawing: annoying offset between touch position and drawing position

Many people are using Unity UI Extensions (that is a very good and useful piece of software by Simon Jackson). Unfortunately when using UILineRenderer to draw using the finger I'm getting a really annoying offset between where I touch and where the line is drawn. The Input.mousePosition uses screen coordinates (in px) while the UILineRenderer uses world coordinates, to convert from screen to world coords I use the ScreenToWorld method from Camera object but still it seems to work for some phones and not for others. The offset between touched and drawn is 0 when I touch the center of the screen and keep increasing while I move to the edges of the phone.

Anyone found a similar problem? I found this other issue online: Unity UI Extensions issue

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
0

Answer by a-dibacco · Dec 10, 2019 at 04:14 PM

I think I found a solution to my problem or, better, an approach to follow when you want to design a 2D game where a Canvas covers completely the phone screen and the camera is rendering exactly the phone screen. With this approach I got rid of many problems I was facing with coordinates conversion.

Here are my advices:

1) Design you interface (buttons and the like) using a screen size (in Game tab) of 1920x1080 or 1080x1920 (landscape or portrait)

2) Set you camera in orthographic mode
with a size of 9.6 (100 pixel per world unit)

3) Add a Canvas to hold your UI
elements with Render mode set to Screen Space - Camera

4) Add a Canvas Scaler script and set a reference resolution of 1080 x 1920 (or opposite for landscape) and set the Screen Match Mode to Expand

With these settings everything is coherent if you use a phone with a resolution of 1080 x 2340 but if you try with another phone resolution (for example 1080x2340) conversion of coordinates will start to become weird. Then I did the following

5) I setup the camera in the Awake method in a script associated with an object of the scene (can be the camera itself) with the following code:

 void Awake ()
 {
     ...
     Camera.main.orthographic = true;
     if (portrait == true)
     Camera.main.orthographicSize = (Screen.height*1080/Screen.width)/200.0f;
     else
     Camera.main.orthographicSize = (Screen.height*1920/Screen.width)/200.0f;

     ...
 }

6) I set up the canvas in the Start method of a script associated with a game object in the scene with the following code:

 void Start() {
     ...
    canvas.GetComponent<CanvasScaler>().referenceResolution = new Vector2(1080, Screen.height*1080/Screen.width);
     ...
 }

7) Because I have a background image that has a resolution of 1920x1080 I use also to change its local scale with the following code:

     float cameraHeight = Camera.main.orthographicSize * 2;
     Vector2 cameraSize = new Vector2(Camera.main.aspect * cameraHeight, cameraHeight);
     Vector3 extents = background.GetComponent<SpriteRenderer>().bounds.extents;
     background.transform.localScale = new Vector3 (cameraSize.x/(2*extents.x),  cameraSize.y/(2*extents.y), 1.0f);

With the previous steps (valid for portrait) now I can draw with my UILineRender and using the ScreenToWorld method with no offset at all.

I hope this can help.

Antonio.

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

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

Related Questions

Can you store multiple x or y coordinates from different gameobjects in one variable and then compare all the stored values in a function? 0 Answers

Please Help. How to add score on collision mobile? 2 Answers

How to make font look sharp and smooth 1 Answer

OnTriggerEnter2D gets executed repeatedly by each collided object with the same Tag 3 Answers

2D Slide Animation 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