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
1
Question by Wouter_Eskens · Apr 11, 2016 at 03:04 PM · dropdownscrollingarrow-keys

Unity dropdown doesn't scroll when navigating with arrow keys

The unity dropdown that I've made in my scene doesn't scroll to the selected object when there are more options than the screen can handle.

It happens when I try to navigate with the arrow keys through the dropdown list.

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 el_rolas · Jun 09, 2016 at 09:36 PM 0
Share

Same issue here but with gamepad how can I fix this?

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by GibTreaty · Jun 19, 2016 at 11:54 AM

Here's a script I just made for dropdown menus in my project. You won't be able to use it without some modifications as I've coded it to work with some Rewired inputs that are specific to my project. I also put the script on the "Template" child game object of the Dropdown menu object. It's not perfect and it only supports vertical auto scrolling but it gets the job done in my case.

 using System.Collections.Generic;
 using Rewired;
 using UnityEngine;
 using UnityEngine.EventSystems;
 using UnityEngine.UI;
 
 public class ScrollRectAutoScroll : MonoBehaviour {
 
     List<Selectable> selectables = new List<Selectable>();
 
     int elementCount;
 
     #region Properties
     Player RewiredPlayer { get; set; }
 
     ScrollRect ScrollRectComponent { get; set; }
     #endregion
 
     void OnEnable() {
         if(ScrollRectComponent) {
             ScrollRectComponent.content.GetComponentsInChildren(selectables);
             elementCount = selectables.Count;
         }
     }
 
     void Awake() {
         RewiredPlayer = ReInput.players.GetPlayer(0);
         ScrollRectComponent = GetComponent<ScrollRect>();
     }
 
     void Start() {
         ScrollRectComponent.content.GetComponentsInChildren(selectables);
         elementCount = selectables.Count;
     }
 
     void Update() {
         if(elementCount > 0)
             if(RewiredPlayer.GetButtonDown("Menu Horizontal") || RewiredPlayer.GetNegativeButtonDown("Menu Horizontal") || RewiredPlayer.GetButtonDown("Menu Vertical") || RewiredPlayer.GetNegativeButtonDown("Menu Vertical")) {
                 int selectedIndex = -1;
                 Selectable selectedElement = EventSystem.current.currentSelectedGameObject ? EventSystem.current.currentSelectedGameObject.GetComponent<Selectable>() : null;
 
                 if(selectedElement)
                     selectedIndex = selectables.IndexOf(selectedElement);
 
                 if(selectedIndex > -1)
                     ScrollRectComponent.verticalNormalizedPosition = 1 - (selectedIndex / ((float)elementCount - 1));
             }
     }
 }
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 Wouter_Eskens · Jun 19, 2016 at 06:03 PM 0
Share

Ayyyyy, thanks for replying! I totally forgot about this post.

I found a solution online some time ago, maybe you can optimize yours, if it is even necessary combining the two?

avatar image Wouter_Eskens Wouter_Eskens · Jun 19, 2016 at 06:03 PM 0
Share
 using UnityEngine;
 //using DG.Tweening;
 using UnityEngine.UI;
 
 [RequireComponent(typeof(ScrollRect))]
 public class ScrollRectEnsureVisible : $$anonymous$$onoBehaviour
 {
 
     #region Public Variables
 
     public float _AnimTime = 0.15f;
     public bool _Snap = false;
     public RectTransform _$$anonymous$$askTransform;
 
     #endregion
 
     #region Private Variables
     
     private ScrollRect mScrollRect;
     private RectTransform mScrollTransform;
     private RectTransform mContent;
 
     #endregion
 
     #region Unity $$anonymous$$ethods
 
     private void Awake ()
     {
         mScrollRect = GetComponent<ScrollRect> ();
         mScrollTransform = mScrollRect.transform  as RectTransform;
         mContent = mScrollRect.content;
     }
 
     #endregion
 
     #region Public $$anonymous$$ethods
 
     public void CenterOnItem(RectTransform target)
     {
         // Item is here
         var itemCenterPositionInScroll = GetWorldPointInWidget(mScrollTransform, GetWidgetWorldPoint(target));
         Debug.Log("Item Anchor Pos In Scroll: " + itemCenterPositionInScroll);
         // But must be here
         var targetPositionInScroll = GetWorldPointInWidget(mScrollTransform, GetWidgetWorldPoint(_$$anonymous$$askTransform));
         Debug.Log("Target Anchor Pos In Scroll: " + targetPositionInScroll);
         // So it has to move this distance
         var difference = targetPositionInScroll - itemCenterPositionInScroll;
         difference.z = 0f;
 
         //clear axis data that is not enabled in the scrollrect
         if (!mScrollRect.horizontal)
         {
             difference.x = 0f;
         }
 
         if (!mScrollRect.vertical)
         {
             difference.y = 0f;
         }
 
         //this is the wanted new position for the content
         var newAnchoredPosition = mContent.anchoredPosition3D + difference;
 
         if (_Snap) {
             mContent.anchoredPosition3D = newAnchoredPosition;
         } else {
             //DOTween.To (() => mContent.anchoredPosition, x => mContent.anchoredPosition = x, newAnchoredPosition, _AnimTime);
         }
     }
 
     #endregion
 
     #region Private $$anonymous$$ethods
 
     Vector3 GetWidgetWorldPoint(RectTransform target)
     {
         //pivot position + item size has to be included
         var pivotOffset = new Vector3(
             (0.5f - target.pivot.x) * target.rect.size.x,
             (0.5f - target.pivot.y) * target.rect.size.y,
             0f);
         var localPosition = target.localPosition + pivotOffset;
         return target.parent.TransformPoint(localPosition);
     }
 
     Vector3 GetWorldPointInWidget(RectTransform target, Vector3 worldPoint)
     {
         return target.InverseTransformPoint(worldPoint);
     }
     #endregion
 }
avatar image
2

Answer by kylewill713 · Jul 21, 2016 at 04:29 PM

This script did the trick for me

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 using UnityEngine.EventSystems;
 
 [RequireComponent(typeof(ScrollRect))]
 public class ScrollRectEnsureVisible : MonoBehaviour
 {
     RectTransform scrollRectTransform;
     RectTransform contentPanel;
     RectTransform selectedRectTransform;
     GameObject lastSelected;
 
     void Start()
     {
         scrollRectTransform = GetComponent<RectTransform>();
     }
 
     void Update()
     {
         //just incase content panel gets created in start.
         if(contentPanel == null) contentPanel = GetComponent<ScrollRect>().content;
 
         GameObject selected = EventSystem.current.currentSelectedGameObject;
 
         if (selected == null)
         {
             return;
         }
         if (selected.transform.parent != contentPanel.transform)
         {
             return;
         }
         if (selected == lastSelected)
         {
             return;
         }
 
         selectedRectTransform = selected.GetComponent<RectTransform>();
         contentPanel.anchoredPosition = new Vector2(contentPanel.anchoredPosition.x, - (selectedRectTransform.localPosition.y) - (selectedRectTransform.rect.height/2));
 
         lastSelected = selected;
     }
 }
 
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 Vipsu · Jul 21, 2017 at 08:17 AM 2
Share

The code above works well, but had some problems with ScrollRect content position which I fixed with simple $$anonymous$$athf.Clamp.

[Edit] Fixed weird behavior when using the dropdown menu with mouse with On$$anonymous$$ouseOver and On$$anonymous$$ouseExit events. Small downside for this being that the dropdown will not scroll with gamepad if the mouse is hovering the dropdown menu.

 using UnityEngine;
 using UnityEngine.UI;
 using UnityEngine.EventSystems;
 
 [RequireComponent(typeof(ScrollRect))]
 public class ScrollRectEnsureVisible : $$anonymous$$onoBehaviour, IPointerEnterHandler, IPointerExitHandler
 {
     RectTransform scrollRectTransform;
     RectTransform contentPanel;
     RectTransform selectedRectTransform;
     GameObject lastSelected;
 
     Vector2 targetPos;
 
     void Start()
     {
         scrollRectTransform = GetComponent<RectTransform>();
 
         if (contentPanel == null)
             contentPanel = GetComponent<ScrollRect>().content;
 
         targetPos = contentPanel.anchoredPosition;
     }
 
     void Update()
     {
         if (!_mouseHover)
             Autoscroll();
     }
 
 
     public void Autoscroll()
     {
         if (contentPanel == null)
             contentPanel = GetComponent<ScrollRect>().content;
 
         GameObject selected = EventSystem.current.currentSelectedGameObject;
 
         if (selected == null)
         {
             return;
         }
         if (selected.transform.parent != contentPanel.transform)
         {
             return;
         }
         if (selected == lastSelected)
         {
             return;
         }
 
         selectedRectTransform = (RectTransform)selected.transform;
         targetPos.x = contentPanel.anchoredPosition.x;
         targetPos.y = -(selectedRectTransform.localPosition.y) - (selectedRectTransform.rect.height / 2);
         targetPos.y = $$anonymous$$athf.Clamp(targetPos.y, 0, contentPanel.sizeDelta.y - scrollRectTransform.sizeDelta.y);
 
         contentPanel.anchoredPosition = targetPos;
         lastSelected = selected;
     }
 
     bool _mouseHover;
     public void OnPointerEnter(PointerEventData eventData)
     {
         _mouseHover = true;
     }
 
     public void OnPointerExit(PointerEventData eventData)
     {
         _mouseHover = false;
     }
 }
avatar image ZeZempest Vipsu · Dec 21, 2017 at 12:45 PM 0
Share

Just logged in to say that I love you man. This worked perfectly fine for me. Thank you and have some sweet Christmas days!

avatar image shawnsi Vipsu · May 09, 2019 at 03:35 PM 0
Share

Thank you @Vipsu! The script works perfectly. Though this should be added in Unity UI.

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

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Fixing Issues with custom RTS Camera Controll Script 1 Answer

Editor's Inspector Dropdown Lists scroll VERY slowly with no mousewheel 1 Answer

Move Dropdown using arrow buttons 0 Answers

camera boundary and movement in rtype style scrolling shooter 3 Answers

Scrollview is positioning things incorrectly 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