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
1
Question by katasteel · Feb 21, 2015 at 11:43 PM · user interface

UI element following mouse when using "Scale With Screen Size"

When I change the "Canvas Scaler" from "Constant Pixel Size" to "Scale With Screen Size" my current code to have a UI element follow the mouse stops working.

Here is the old code:

Vector3 newPosition = (Input.mousePosition - canvas.GetComponent().localPosition);

cursorIcon.GetComponent().localPosition = newPosition;

What happens if I use the old code now is the icon position is either too far to the left(if mouse is on the right side of the screen) or too far right(if the mouse is on the left side of the screen).

Has anyone experienced this problem and know a solution?

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
2
Best Answer

Answer by katasteel · Feb 22, 2015 at 06:37 PM

I was able to get much closer to having the ui element follow the mouse by using this code. But it still is not 100% correct. The further it gets away from 0,0 the less accurate it is.

If anyone has any ideas let me know.

     public void OnPointerDown(PointerEventData data)
     {
         Vector2 mousePos = Input.mousePosition;
 
         Vector2 canvasPos = GetComponent<RectTransform>().localPosition;
 
         float xMultiplier = Constants.REFERENCE_RESOLUTION.x / Screen.width;
         float yMultiplier = Constants.REFERENCE_RESOLUTION.y / Screen.height;
 
         Vector2 imagePos = new Vector2(mousePos.x * xMultiplier, mousePos.y * yMultiplier);
 
         pfImage.GetComponent<RectTransform>().localPosition = imagePos;
     }

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 katasteel · Feb 27, 2015 at 05:49 PM 2
Share

I was able to get it to work using this code:

 Vector2 pos;
 RectTransformUtility.ScreenPointToLocalPointInRectangle(transform as RectTransform, Input.mousePosition, Camera.main, out pos);
 
 cursorIcon.transform.position = transform.TransformPoint(pos);

avatar image dappledore · Aug 29, 2016 at 02:22 AM 1
Share

thanks this helped alot

avatar image
2

Answer by hardcorebadger · Oct 07, 2017 at 12:57 AM

I thought I'd clear this all up - when using a canvas scaler "Scale With Screen Size", Screen coordinate space can vary with respect to Canvas space (because you canvas is scaling) - normally, plugging screen space coordinates (like Input.mousePosition) straight into canvas coordinates works fine, but the scaling canvas changes the canvas's scale factor, thus changing the conversion factor between screen and canvas coordinates.

Basically, I had this code before:

 float width = Input.mousePosition - startPos.x;
 float height = Input.mousePosition - startPos.y;
 dragSelector.sizeDelta = new Vector2 (width, height);


And I had to change it to this:

 float scaleFactor = GetComponent<Canvas> ().scaleFactor;
 
 dragSelector.sizeDelta = new Vector2 (width/scaleFactor, height/scaleFactor);


So it's really just adding that scalar conversion to reverse the effects of the screen scaler.

Comment
Add comment · Show 3 · 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 solclovser · Mar 05, 2020 at 09:59 AM 0
Share

Thanks man. I know it was long time ago but helped me. I wish I searched first before trying 500 code combinations :'D

avatar image PeterMcteague · Mar 23, 2020 at 01:26 PM 0
Share

Cheers this was really helpful!

Just thought I'd chime in for anyone that made the same novice mistake I did. If you're still getting an offset after this on some aspect ratio's, you also need to scale using your aspectratiofitter or even consider whether you need it.

avatar image Cani23 · Jan 19, 2021 at 08:00 PM 0
Share

A simple solution after a long search. Thank you.

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

25 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

Related Questions

UI System for Non-minimal User Interfaces 0 Answers

Simple UI not appearing once deployed to iOS (4.6.2) 1 Answer

Universal Method of finding pixel dimensions of UI elements 1 Answer

Set InputField non-interactable without clearing text 1 Answer

Slider and Input Field Value Updates 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