- Home /
Question by
Yura1999 · Nov 14, 2017 at 10:20 PM ·
c#uiscrollviewscrolling
Scroll rect, autoscroll content
Hi all,
I have a scroll view and buttons inside. It controlls only by arrow keys, i.e. without mouse input.
So, when I select a button, for example, which is in the bottom of a viewport and is not visible, I need to scroll to this button automatically.
Can someone help with an implementation of this feature ?
Comment
Answer by Al-Maliki · May 27, 2018 at 06:57 PM
Try this.
[SerializeField, Range(1f, 5f)]
private float scrollSpeed = 2f;
private float watingTime = 5f;
private float maxScroll;
private ScrollRect scrollRect;
private RectTransform contenRectTransform;
private Vector2 defaultPosition;
private bool canMove;
private void Start()
{
this.scrollRect = GetComponent<ScrollRect>();
this.contenRectTransform = this.scrollRect.content;
this.maxScroll = this.contenRectTransform.rect.yMax;
this.StartCoroutine(this.ActivateAutoMove());
}
private void Update()
{
bool hasScrolled = this.contenRectTransform.position.y > this.maxScroll;
if (canMove & !hasScrolled)
this.Move();
}
private void Move()
{
Vector2 contentPosition = this.contenRectTransform.position;
Vector2 newPosition = new Vector2(contentPosition.x, contentPosition.y + this.scrollSpeed);
this.contenRectTransform.position = newPosition;
}
I tested, fixed
1)
bool hasScrolled = this.contenRectTransform.position.y < this.maxScroll;
2)
private void $$anonymous$$ove()
{
Vector3 contentPosition = this.contenRectTransform.position;
float newPositionY = contentPosition.y + this.scrollSpeed;
Vector3 newPosition = new Vector3(contentPosition.x, newPositionY ,contentPosition.z);
this.contenRectTransform.position = newPosition;
}
Can you provide ActivateAuto$$anonymous$$ove() method, please
Your answer
Follow this Question
Related Questions
How to access scrollview in VR? 0 Answers
Scrollview inside another scrollview ignore raycasts 0 Answers
Focus on Selected Item in Vertical Scrollview 0 Answers
Multiple Cars not working 1 Answer
Scroll A Text Once It Reaches The End Of The Viewport 0 Answers