Hello guys, here I’m again.
I was struggling with the bug, found in Unity 2017.3.1p1 after upgrading from 5.4. How does it work:
We set the verticalNormalizedPosition or horizontalNormalizedPosition of the ScrollRect and after OnDrag was called the content of the scroll change its position to the last position that we have dragged before verticalNormalizedPosition was set.
We were changing scroll position to show the position of our character in the ranking or to show the last stage unlocked on the location.
But the bug, I think, is in the changing of the RectTransform.localPosition. In your UI Library:
protected virtual void SetNormalizedPosition(float value, int axis)
{
EnsureLayoutHasRebuilt();
UpdateBounds();
// How much the content is larger than the view.
float hiddenLength = m_ContentBounds.size[axis] - m_ViewBounds.size[axis];
// Where the position of the lower left corner of the content bounds should be, in the space of the view.
float contentBoundsMinPosition = m_ViewBounds.min[axis] - value * hiddenLength;
// The new content localPosition, in the space of the view.
float newLocalPosition = m_Content.localPosition[axis] + contentBoundsMinPosition - m_ContentBounds.min[axis];
Vector3 localPosition = m_Content.localPosition;
if (Mathf.Abs(localPosition[axis] - newLocalPosition) > 0.01f)
{
localPosition[axis] = newLocalPosition;
m_Content.localPosition = localPosition;
m_Velocity[axis] = 0;
UpdateBounds();
}
}
What is the problem with the code above is that you are changing content position by assigning new localPosition, after changing its local position the content in the game is showing in the right place, but then start to drag with the finger, and the content is going back to the last known anchoredPosition.
Local Position IS NOT updating Anchored Position.
I did a lot of debug logs in your code, so I’m 100% sure of that, the AnchoredPosition is not changed after updating local position:
My fix was pretty easy:
EnsureLayoutHasRebuilt();
UpdateBounds();
// How much the content is larger than the view.
float hiddenLength = m_ContentBounds.size[axis] - m_ViewBounds.size[axis];
// Where the position of the lower left corner of the content bounds should be, in the space of the view.
float contentBoundsMinPosition = m_ViewBounds.min[axis] - value * hiddenLength;
// The new content localPosition, in the space of the view.
float newLocalPosition = m_Content.anchoredPosition[axis] + contentBoundsMinPosition - m_ContentBounds.min[axis];
Vector3 localPosition = m_Content.anchoredPosition;
if (Mathf.Abs(localPosition[axis] - newLocalPosition) > 0.01f)
{
localPosition[axis] = newLocalPosition;
m_Content.anchoredPosition = localPosition;
m_Velocity[axis] = 0;
UpdateBounds();
}
Log("POSITION OF m_content after setting normalized position: " + m_Content.anchoredPosition);
EDIT:
Your UI is working well in Editor ( as expected ) the bug occures on every mobile platform, even in the emulators.