Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 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 /
avatar image
0
Question by Razputin · May 14, 2021 at 07:08 PM · gridinventoryscrollviewscroll

Auto Scroll To Selected Button in Grid When Outside Viewport

I have a grid setup which has items spawned into it.

The items have a lambda expression on them that calls an EventTrigger when Selected, like so.

                 EventTrigger.Entry eT = new EventTrigger.Entry();
                 eT.eventID = EventTriggerType.Select;
                 eT.callback.AddListener((eventData) => { SnapTo(g.GetComponent<RectTransform>()); });
                 g.GetComponent<EventTrigger>().triggers.Add(eT);
 
 

I have a method SnapTo, which moves to the selected gameobject.

     public void SnapTo(RectTransform target)
     {
         Canvas.ForceUpdateCanvases();
 
         inventoryContentPanel.anchoredPosition =
             (Vector2)inventoryScrollRect.transform.InverseTransformPoint(inventoryContentPanel.position)
             - (Vector2)inventoryScrollRect.transform.InverseTransformPoint(target.position);
     }
 

However I'd like to only SnapTo when the selected item is outside the scroll views viewport, that's what makes it tricky. As if it's visible within the viewport and not being masked it should simply select it without scrolling.

Real Game Example : https://www.youtube.com/watch?v=k49kXZ2lFZs&ab_channel=AdamTheD

Is there an easy way to do this, or even an asset/plugin I can use instead?

EDIT : I found an easy way to tell if the selected object is outside of the viewport. Now I just need help figuring out how to push the out of view selection into view.

     public void SnapTo(RectTransform target)
     {
         int index = inventoryChildrenList.IndexOf(target.gameObject);
         RectTransform rect = inventoryChildrenList[index].GetComponent<RectTransform>();
         Vector2 v = rect.position;
         bool inView = RectTransformUtility.RectangleContainsScreenPoint(inventoryScrollRectTransform, v);
 
         if (!inView)
         {
 
         }
     }

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 Razputin · May 16, 2021 at 03:36 AM 0
Share

bumpinggggg

2 Replies

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

Answer by Razputin · May 17, 2021 at 01:34 PM

Here is my solution for those struggling in the future... basically after you determine if it's visible with RectTransformUtility.RectangleContainsScreenPoint, you compare the last entry you had selected with the next entry you're selecting. If the next entry is higher up than the current one, move the content panel down. If it's lower, than move the content panel up.

     public void SnapTo(RectTransform target)
     {
             int index = inventoryChildrenList.IndexOf(target.gameObject); //Inventory Children List contains all the Children of the ScrollView's Content. We are getting the index of the selected one.
             RectTransform rect = inventoryChildrenList[index].GetComponent<RectTransform>(); //We are getting the RectTransform of the selected Inventory Item.
             Vector2 v = rect.position; //We are getting the Position of the Selected Inventory Item's RectTransform.
             bool inView = RectTransformUtility.RectangleContainsScreenPoint(inventoryScrollRectTransform, v); //We are checking if the Selected Inventory Item is visible from the camera.
 
             float incrimentSize = rect.rect.height; //We are getting the height of our Inventory Items
 
             if (!inView) //If the selected Item is not visible.
             {
                 if (oldRect != null) //If we haven't assigned Old before we do nothing.
                 {
                     if (oldRect.localPosition.y < rect.localPosition.y) //If the last rect we were selecting is lower than our newly selected rect.
                     {
                         inventoryContentPanel.anchoredPosition += new Vector2(0, -incrimentSize); //We move the content panel down.
                     }
                     else if (oldRect.localPosition.y > rect.localPosition.y) //if the last rect we were selecting is higher than our newly selected rect.
                     {
                         inventoryContentPanel.anchoredPosition += new Vector2(0, incrimentSize); //We move the content panel up.
                     }
                 }
             }
       
             oldRect = rect; //We assign our newly selected rect as the OldRect.
         }
 
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
0

Answer by a-fatin · Dec 02, 2021 at 06:22 AM

@Razputin Thanks for sharing your code here. I managed to get it to work with a few tweaks. Had to figure out how to do a few things with the EventTrigger first though but you really helped me. Really thank you.

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

121 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

Related Questions

Scroll view using Mesh 2 Answers

[Solved] need help regarding autolayout in scrollrect 1 Answer

How to limit scrolling at same position not depending on aspect ratio 0 Answers

Nested Scroll Views: How can I pass drag control from an inner scroll view to its outer scroll view? 2 Answers

how do I scroll thru a array 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