- Home /
How can I make my scroll list scroll 1 item per button click?
Hello Unity Community,
Once again I come to this website in search of help. I am working on a scroll list that displays three items at once. Per button click I want to scroll one item in from the left or right depending if they click the right or left scroll button. I am really close to that functionality but there is a bit of inconsistency in my scroll list. Depending where I am in the scroll list the scroll list may or may not scroll 1 item. Sometimes it scrolls 2 items. I've been staring at this code all day and decided to ask the Unity Community. Please let me know if you help me in any sort.
So if this is my scroll list: < | item 2 item 3 item 4 | > I hit the left button ( < ) < | item 3 item 4 item 5 | > as you can see the list moved by 1 item. But sometimes this happens < | item 2 item 3 item 4 | > I hit the left button ( < ) < | item 4 item 5 item 6 | > as you can see the list moved by 2 items.
Also I should note that my list has the ability to wrap and can continuously scroll if the left/right scroll button is held down. But I believe through many debug statements that doesn't have any effect(affect?) on the inconsistency of the scrolling.
Oh, and I'm using the AnBSoft EZ GUI API using the UIScrollList.
If any one needs any addition details please ask I shall proved them. Thank you very much Unity Community! Any help is greatly appreciated!!!!
Here is how I scroll the items:
public void gameScrollRight(){
int currentIndex = (int)(gameScroller.ScrollPosition * gameScroller.Count);
int toIndex = currentIndex + 2;
print("SCROLL TO " + toIndex);
if(toIndex >= gameScroller.Count) {
toIndex = gameScroller.Count - 2;
IUIListObject leftObj = gameScroller.GetItem(0);
gameScroller.RemoveItem(0, false);
gameScroller.AddItem(leftObj);
leftObj.gameObject.SetActiveRecursively(true);
}
gameScroller.ScrollToItem(toIndex, .75f);
}
public void gameScrollLeft(){
int currentIndex = (int)(gameScroller.ScrollPosition * gameScroller.Count);
int toIndex = currentIndex - 2;
print("SCROLL TO " + toIndex);
if(toIndex < 0) {
float itemWidth = gameScroller.ContentExtents / gameScroller.Count;
toIndex = 0;
IUIListObject rightObj = gameScroller.GetItem(gameScroller.Count - 1);
rightObj.gameObject.SetActiveRecursively(true);
gameScroller.RemoveItem(gameScroller.Count - 1, false);
gameScroller.InsertItem(rightObj, 0);
gameScroller.PositionItems();
gameScroller.ScrollPosition += 1.0f / (gameScroller.Count - (gameScroller.viewableArea.x / itemWidth));
}
gameScroller.ScrollToItem(toIndex, .751f);
}
gameScroller.ScrollPosition = itemId * itemHeight > Or.. scrollPosition += itemHeight;
I'm sorry can you clarify the above comment. gameScroller.ScrollPosition = itemID * itemHeight? What variable is itemID and what variable is itemHeight? I assume you mean itemWidth ins$$anonymous$$d of itemHeight.
first question is gameScrollRight() -> currentIndex + 2; why is plus 2 ? and in gameScrollLeft() why is it - 2; ... ok..
trying to understand:
currentIndex -> is index of first item to show;
toIndex -> currentIndex - 2; -> here may be a problem...
what is gameScroller.Count is it number of items ? or size of horisontal width of item ? or what is it ??
Ok let me explain. First, gameScrollerRight/Left have +/-2 respectively because currently we use that magic number to move the items. Without the two the items wouldn't move. If it was 3 items would move more than expected and if it was 1 the items wouldn't move at all. The two should represent the number of indexes you want to move.
CurrentIndex is the index of the first shown item. toIndex is where the scrollList should scroll to (obviously this doesn't always happen or I wouldnt' be asking this question)
gameScoller.Count is the number of items in the scrollList gameScoller.viewable.x(or y) is the width of the viewable area
Answer by XienDev · Dec 20, 2012 at 06:12 PM
try this:
public void gameScrollRight(){
int currentIndex = (int)(gameScroller.ScrollPosition * (gameScroller.Count - 2));
int toIndex = currentIndex + 2;
//print("SCROLL TO " + toIndex);
if(toIndex >= (gameScroller.Count - 1)){
toIndex = gameScroller.Count - 2; // sroll to before last
IUIListObject leftObj = gameScroller.GetItem(0);
gameScroller.RemoveItem(0, false);
gameScroller.AddItem(leftObj);
leftObj.gameObject.SetActiveRecursively(true);
}
gameScroller.ScrollToItem(toIndex, .75f);
}
public void gameScrollLeft() {
int currentIndex = (int)(gameScroller.ScrollPosition * (gameScroller.Count - 2));
int toIndex = currentIndex;
//print("SCROLL TO " + toIndex);
if(toIndex <= 0){
toIndex = 1;
//float itemWidth = gameScroller.ContentExtents / gameScroller.Count;
IUIListObject rightObj = gameScroller.GetItem(gameScroller.Count - 1);
rightObj.gameObject.SetActiveRecursively(true);
gameScroller.RemoveItem(gameScroller.Count - 1, false);
gameScroller.InsertItem(rightObj, 0);
gameScroller.PositionItems();
gameScroller.ScrollPosition += 1.0f / (gameScroller.Count - 2);
}
gameScroller.ScrollToItem(toIndex, 1f);
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613085435im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
A node in a childnode? 1 Answer
List of C# scripting definitions 1 Answer
Object Method returning null when calling Object List from another Class 1 Answer