- Home /
how to make UI Image inside Scroll Rect (ScrollRect) autoscroll?
My hierarchy looks like this:
Canvas->Panel->ScrollRect (with mask)->Image
Scroll Rect is set to: Vertical (only) and Movement Type is set to: Clamped.
What I'm trying to achieve is to force that image in some kind slow and looped motion until touched.
looped motion is a dream. Right now I can't even make it move.
using UnityEngine;
using System.Collections;
public class scroll : MonoBehaviour {
public float speed = 0.5f;
public float offset = 510;
void Start ()
{
}
void Update ()
{
this.offset -= Time.deltaTime * this.speed;
}
}
I can see offset decreasing during the "play mode" but Image is not moving even a little. Can anybody please help me or point into the right direction? Is it even possible?
It could be also useful for creating Scroll Rect with ie. heroes/cars to choose from etc. So I think that many devs would benefit from this question.
Answer by tkamruzzaman · Jul 18, 2016 at 04:00 AM
use scrollScrollRect.verticalNormalizedPosition
with Mathf.Lerp to change the position between 1 to 0
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class AutoScrollRect: MonoBehaviour {
public ScrollRect myScrollRect;
public float scrollTime;
public static WaitForSeconds P3 = new WaitForSeconds(0.3f);
void Start() {
StartCoroutine(AutoScroll(myScrollRect, 1, 0, scrollTime));
}
IEnumerator AutoScroll(ScrollRect srollRect, float startPosition, float endPosition, float duration) {
yield return P3;
float t0 = 0.0f;
while (t0 < 1.0f) {
t0 += Time.deltaTime / duration;
srollRect.verticalNormalizedPosition = Mathf.Lerp(startPosition, endPosition, t0);
yield return null;
}
}}
Your answer
Follow this Question
Related Questions
ScrollRect - Scroll outside the rect!? 0 Answers
ScrollRect is slowing down, when scrolling many (same) images placed in Grid Layout Group 0 Answers
How to play scrolling sounds when scrolling through ScrollRect? 1 Answer
Make UI ScrollRect end a drag 0 Answers
Drag on Scroll Rect Non Proportional 0 Answers