- Home /
Repositioning Items in ScrollRect During Scroll While Maintaining Offset
TL(and confusing)DR: I want to reposition items in an infinitely-scrolling ScrollRect and offset the menu by the newly-moved item's width + spacing, but maintain OnDrag with the new offset being taken into account.
TL Part: I'm creating an infinitely-scrolling ScrollRect and I'm just curious as to how I can reposition items in HorizontalLayout, let's say, last item in list moves to first item, therefore, first item becomes second item, BUT I want to maintain OnDrag while doing so. So far, the only way I've been able to do this is by deactivating ScrollRect, repositioning items, activating ScrollRect (loses EventData), then using previous EventData from ScrollRect to keep OnDrag, but modify the OnDrag data to take into account the offset.
I minimized the code for easier reading:
namespace GE.GamingEssentials.Layout
{
[System.Serializable]
public class SnapScrollRevolving : MonoBehaviour
{
#region Public Variables
public Transform MainParent = null;
public ScrollRect ScrollRect = null;
public HorizontalLayoutGroup LayoutGroup = null;
public List<Transform> ElementPlaceholders = new List<Transform>();
public float DisappearDistance = 1.5f;
public float SnapVelocity = 20f;
public bool Loop = true;
#endregion
#region Private Variables
#endregion
#region Public Enumerators
#endregion
#region Private Enumerators
#endregion
#region Event Functions
#endregion
#region Public Functions
#endregion
#region Private Functions
private void Update ()
{
if (MainParent == null ||
ScrollRect == null ||
LayoutGroup == null)
return;
if (ScrollRect.velocity != new Vector2(0, 0))
SetDistanceEnabled();
if (Loop)
LoopItems();
}
private void LoopItems ()
{
if (MainParent.InverseTransformPoint(ElementPlaceholders[0].position).x > LayoutGroup.spacing &&
ScrollRect.velocity.x > 0)
{
Debug.Log("Hmmmm 2");
//Current velocity.
Vector2 PrevVelocity = ScrollRect.velocity;
//Temp Fix: Bye.
ScrollRect.enabled = false;
ScrollRect.content.anchoredPosition = new Vector2
{
x = ScrollRect.content.anchoredPosition.x - ElementPlaceholders[0].GetComponent<RectTransform>().rect.width - LayoutGroup.spacing,
y = ScrollRect.content.anchoredPosition.y
};
//Move element to beginning of content.
ElementPlaceholders[ElementPlaceholders.Count - 1].SetSiblingIndex(0);
//Move last element in list to beginning of list.
ElementPlaceholders.Insert(0, ElementPlaceholders[ElementPlaceholders.Count - 1]);
//Remove last element.
ElementPlaceholders.RemoveAt(ElementPlaceholders.Count - 1);
//Temp Fix: Hello.
ScrollRect.enabled = true;
//Temp fix: Lose EventData yet either keeping velocity or applying SnapVelocity.
if (ScrollRect.velocity.x > SnapVelocity)
ScrollRect.velocity = PrevVelocity;
else
ScrollRect.velocity = new Vector2(SnapVelocity, 0);
}
}
private void SetDistanceEnabled ()
{
foreach (Transform ElementPlaceholder in ElementPlaceholders)
{
if (Vector2.Distance(ElementPlaceholder.position, MainParent.position) < DisappearDistance &&
!ElementPlaceholder.GetComponentInChildren<Image>().enabled)
ElementPlaceholder.GetComponentInChildren<Image>().enabled = true;
else if (Vector2.Distance(ElementPlaceholder.position, MainParent.position) >= DisappearDistance &&
ElementPlaceholder.GetComponentInChildren<Image>().enabled)
ElementPlaceholder.GetComponentInChildren<Image>().enabled = false;
}
}
#endregion
#region Public Coroutines
#endregion
#region Private Coroutines
#endregion
#region Public Properties
#endregion
#region Private Properties
#endregion
}
}
Your answer
Follow this Question
Related Questions
UI Button onclick not detected 0 Answers
Can I use EventSystem + InputModule to implement scroll via button? 0 Answers
Canvas appearing over my cursor doesn'tr trigger OnPointerExit ! 0 Answers
How to stop EventSystem from triggering on two different displays/canvases? 0 Answers
Broadcast mouse event on Canvas 0 Answers