Updated with transtions and allowing to change children pivots

pull/474/head
GoShikhar 2024-05-14 12:49:24 +05:30
parent 204bc7bdd1
commit 6e1f1afca5
2 changed files with 16 additions and 10 deletions

View File

@ -17,7 +17,7 @@ namespace UnityEngine.UI.Extensions
internal float _scrollStartPosition;
internal float _childSize;
private float _childPos, _maskSize;
internal Vector2 _childAnchorPoint;
[SerializeField] internal Vector2 _childAnchorPoint = new Vector2(0.5f, 0);
internal ScrollRect _scroll_rect;
internal Vector3 _lerp_target;
internal bool _lerp;
@ -256,6 +256,11 @@ namespace UnityEngine.UI.Extensions
childRect.rotation = _screensContainer.rotation;
childRect.localScale = _screensContainer.localScale;
childRect.position = _screensContainer.position;
child.GetComponent<Button>().onClick.AddListener(() =>
{
ChangePage(i);
}
);
}
child.transform.SetParent(_screensContainer.transform);
@ -648,10 +653,9 @@ namespace UnityEngine.UI.Extensions
#endregion
#region Transition Effects
private Vector2 GetDisplacementFromCenter(int index)
private Vector2 GetDisplacementFromCenter(Transform _child)
{
return _screensContainer.GetChild(index).GetComponent<Transform>().position - _scroll_rect.viewport.transform.position;
return _child.position - _scroll_rect.viewport.transform.position;
}
private void HandleTransitionEffects()
@ -660,9 +664,10 @@ namespace UnityEngine.UI.Extensions
for (int i = 0; i < _screensContainer.childCount; i++)
{
Vector2 displacement = GetDisplacementFromCenter(i);
Transform _child = _screensContainer.GetChild(i);
Vector2 displacement = GetDisplacementFromCenter(_child);
float d = (_scroll_rect.horizontal ? displacement.x : displacement.y);
onTransitionEffects.Invoke(_screensContainer.GetChild(i).gameObject, d);
onTransitionEffects.Invoke(_child.gameObject, d);
}
}

View File

@ -17,14 +17,14 @@ namespace UnityEngine.UI.Extensions
void Start()
{
_isVertical = true;
_childAnchorPoint = new Vector2(0.5f,0);
_currentPage = StartingScreen;
panelDimensions = gameObject.GetComponent<RectTransform>().rect;
UpdateLayout();
}
void Update()
protected override void Update()
{
base.Update();
updated = false;
if (!_lerp && _scroll_rect.velocity == Vector2.zero)
@ -57,7 +57,7 @@ namespace UnityEngine.UI.Extensions
if (!_pointerDown)
{
if (_scroll_rect.velocity.y > 0.01 || _scroll_rect.velocity.y < -0.01)
{
{
// if the pointer is released and is moving slower than the threshold, then just land on a page
if (IsRectMovingSlowerThanThreshold(0))
{
@ -89,8 +89,9 @@ namespace UnityEngine.UI.Extensions
RectTransform child = _screensContainer.transform.GetChild(i).gameObject.GetComponent<RectTransform>();
currentYPosition = _offset + i * pageStepValue;
child.sizeDelta = new Vector2(panelDimensions.width, panelDimensions.height);
child.anchoredPosition = new Vector2(0f, currentYPosition);
child.anchorMin = child.anchorMax = child.pivot = _childAnchorPoint;
child.anchoredPosition = new Vector2(0f, currentYPosition);
}
_dimension = currentYPosition + _offset * -1;