Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Continues Horizontal Scroll

Discussion in 'Immediate Mode GUI (IMGUI)' started by Artpen, Mar 1, 2022.

  1. Artpen

    Artpen

    Joined:
    Jan 24, 2015
    Posts:
    291
    Hey guys,

    I am working on a simple continuous horizontal scroll. I know there are a couple of assets available, but I want something simple.

    My approach:
    I have a list of buttons that I instantiate at Start to a content holder. (Typical)
    I use
    Code (CSharp):
    1. scrollRect.onValueChanged.AddListener(UpdatedButtonsPosition);
    to detect the scroll.
    If I am scrolling to the right I just move buttons back in the list and in inspector using
    Code (CSharp):
    1. newButton.transform.SetSiblingIndex(siblingIndex);
    So now where I have a problem. For the above to work I have to also shift the actual content holder.
    But while dragging it will not allow it. As soon as I let go of the mouse it works perfectly.

    Here is a code and a video with a problem.
    Help will be much appreciated.

    LoopScroll.gif

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.EventSystems;
    5. using UnityEngine.UI;
    6.  
    7. public class ScrollSnap : MonoBehaviour
    8. {
    9.     [SerializeField] private List<StreetData> streetsData = new List<StreetData>();
    10.     private List<GameObject> buttons = new List<GameObject>();
    11.     [SerializeField] private GameObject buttonPrefab;
    12.     [SerializeField] private GameObject contentPanel;
    13.     private ScrollRect scrollRect;
    14.     private float startScrollPosition;
    15.  
    16.     // Start is called before the first frame update
    17.     void Start()
    18.     {
    19.         // Access ScrollRect
    20.         scrollRect = this.GetComponent<ScrollRect>();
    21.         scrollRect.onValueChanged.AddListener(UpdatedButtonsPosition);
    22.         startScrollPosition = scrollRect.horizontalNormalizedPosition;
    23.  
    24.         // Instantiate buttons in content parent game object
    25.         AddButtons();
    26.     }
    27.  
    28.     // Instantiate buttons
    29.     private void AddButtons()
    30.     {
    31.         int siblingIndex = 0;
    32.  
    33.         foreach (var streetData in streetsData)
    34.         {
    35.             GameObject newButton = Instantiate(buttonPrefab);
    36.             newButton.GetComponent<Image>().color = streetData.Color;
    37.             newButton.name = streetData.StreetName;
    38.             newButton.transform.SetParent(contentPanel.transform);
    39.             newButton.GetComponent<RectTransform>().localScale = new Vector3(1, 1, 1);
    40.             newButton.transform.SetSiblingIndex(siblingIndex);
    41.             siblingIndex++;
    42.             buttons.Add(newButton);
    43.         }
    44.     }
    45.  
    46.     // Check scroll direction
    47.     private bool IsScrollingRight(Vector2 value)
    48.     {
    49.         if (value.x < startScrollPosition)
    50.         {
    51.             //Debug.Log("You are scrolling right");
    52.             startScrollPosition = value.x;
    53.             return true;
    54.         }
    55.         else
    56.         {
    57.             //Debug.Log("You are scrolling left");
    58.             startScrollPosition = value.x;
    59.             return false;
    60.         }
    61.     }
    62.  
    63.     // Updated buttons position if outside the mask boundaries
    64.     private void UpdatedButtonsPosition(Vector2 scrollValue)
    65.     {
    66.         // 1. Check which direction is scrolling
    67.         // 2. Check if button moved beyond threshold
    68.         // 3. Move button to the begining of the content list
    69.        
    70.         if (IsScrollingRight(scrollValue))
    71.         {
    72.             //Debug.Log(buttons[buttons.Count - 1].transform.position.x);
    73.            
    74.             if (buttons[buttons.Count - 1].transform.position.x > 1750)
    75.             {
    76.                 buttons[buttons.Count - 1].transform.SetAsFirstSibling();
    77.                 GameObject button = buttons[buttons.Count - 1];
    78.                 buttons.RemoveAt(buttons.Count - 1);
    79.                 buttons.Insert(0, button);
    80.                 contentPanel.GetComponent<RectTransform>().anchoredPosition -= new Vector2 (110, 0);
    81.                 //scrollRect.horizontalNormalizedPosition = 0.5f;
    82.             }
    83.         }
    84.         else
    85.         {
    86.             //Debug.Log(buttons[0].transform.position.x);
    87.  
    88.             if (buttons[0].transform.position.x < 1100)
    89.             {
    90.                 buttons[0].transform.SetAsFirstSibling();
    91.                 GameObject button = buttons[0];
    92.                 buttons.RemoveAt(0);
    93.                 buttons.Insert(buttons.Count - 1, button);
    94.                 contentPanel.GetComponent<RectTransform>().anchoredPosition += new Vector2(110, 0);
    95.                 //scrollRect.horizontalNormalizedPosition = 0.5f;
    96.             }
    97.         }
    98.  
    99.     }
    100.  
    101.  
    102. }
    103.