Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
0
Question by hexdump · Sep 09, 2015 at 09:24 AM · uiscroll view

Calculating normalized horizontal position based on container size with fixed item size

Hi,

I'm building a little list of items inside a ScrollRect and would like to jump to any of them giving an index.

Based on inspector my items are 56x56 and the container has a width of 932. As an example I have tried to jump to an item manually (5th item in this example) just by setting normalizedHorizontalPostion to (5*56)/932. But this seems not quite right. If I set the Content rect position to -5*56 it shows my item at correct position (in the inspector) but when i try to set this through the scrollrect normalized horizontal position I can see in the inspector that the container is not jumping to the expected position.

Is there anything I am missing? Must I have any other internal screen rect or container rect position variables into account for this?.

Cheers.

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 Scribe · Sep 09, 2015 at 09:51 AM

This is untested:

 w = visible area of scrollrect
 mW = 932; //total width of scroll rect
 pos = (itemcount - 1)*56; //0 indexed, hence 1st item should read (1-1)*56 = 0
 
 return ((Mathf.Clamp(pos+w/2f, w/2f, mW - w/2f)-w/2f)/(mW-w))

Maybe that will work, if the scroll bar interpolates linearly I believe it should!

Comment
Add comment · Show 4 · 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 hexdump · Sep 09, 2015 at 10:06 AM 0
Share

Thanks for the answer but this is not what I'm looking for. The math I'm applying is correct. I have checked several times. The main problem is when i try to translate it to set the normalizedHorizontalPosition because I think I'm missing something related to RectTransform inner workings. A simple example like, I have 10 items evently positioned (56 units wide) under the content transform. Each item should be reached just by assigning (1/10)*item to the normalizedhorizontalPosition. But when I set normalizedHorizontalPosition to for example (1/10)*4 it is not at correct position. On the other hand I expect my container x position to be at -4*56 units but it is not either.

Cheers.

avatar image Scribe · Sep 09, 2015 at 10:12 AM 0
Share

The maths you are applying is not correct, it assumes the view area can move a total of 932 pixels, but that implies that when you get to the end of the scroll view, you have scrolled beyond the edge.

Think about it. At position 0 you can see pixel 1 on the left, at position 1, you see pixel 932 on the RIGHT not the left. so you have not moved 932 pixels despite that being the width of the scroll rect.

avatar image hexdump Scribe · Sep 09, 2015 at 10:29 AM 1
Share

I fell so ashamed now... How the hell I did miss that? Thanks for noticing the problem, I was so focuses that I did not see it. By the way how can I accept this at the correct answer?. It seems I can't because my reputation is too low?

avatar image Scribe hexdump · Sep 09, 2015 at 10:40 AM 0
Share

No problem, it is a little confusing, especially when the other way feels so obviously correct! You should be able to accept it now, as I have converted it from a comment to an answer :)

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

31 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

Related Questions

How do I control drag speed of scroll view to make it slower than mouse drag? 0 Answers

UI Date Picker (roller) 0 Answers

Make Scroll View size fitting to children size in one dimension 0 Answers

Curiously, Viewport seems to need an Image? 1 Answer

How to start Scroll Rect content off screen 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