diff --git a/Scripts/Layout/HorizontalScrollSnap.cs b/Scripts/Layout/HorizontalScrollSnap.cs index d1358b1..ae58ffd 100644 --- a/Scripts/Layout/HorizontalScrollSnap.cs +++ b/Scripts/Layout/HorizontalScrollSnap.cs @@ -18,8 +18,7 @@ namespace UnityEngine.UI.Extensions if(MaskArea) CalculateVisible(); _lerp = false; _currentPage = StartingScreen - 1; - _scrollStartPosition = _screensContainer.localPosition.x; - _scroll_rect.horizontalNormalizedPosition = (float)(_currentPage) / (_screens - 1); + SetScrollContainerPosition(); } void Update() @@ -89,8 +88,9 @@ namespace UnityEngine.UI.Extensions _scroll_rect.horizontalNormalizedPosition = 0; GO.transform.SetParent(_screensContainer); DistributePages(); + if (MaskArea) UpdateVisible(); - _scroll_rect.horizontalNormalizedPosition = (float)(_currentPage) / (_screens - 1); + SetScrollContainerPosition(); } /// @@ -107,26 +107,25 @@ namespace UnityEngine.UI.Extensions return; } _scroll_rect.horizontalNormalizedPosition = 0; - var children = _screensContainer.transform; - int i = 0; - foreach (Transform child in children) - { - if (i == index) - { - child.SetParent(null); - ChildRemoved = child.gameObject; - break; - } - i++; - } + + Transform child = _screensContainer.transform.GetChild(index); + child.SetParent(null); + ChildRemoved = child.gameObject; DistributePages(); + if (MaskArea) UpdateVisible(); if (_currentPage > _screens - 1) { CurrentPage = _screens - 1; } + SetScrollContainerPosition(); + } + + private void SetScrollContainerPosition() + { + _scrollStartPosition = _screensContainer.localPosition.x; _scroll_rect.horizontalNormalizedPosition = (float)(_currentPage) / (_screens - 1); } diff --git a/Scripts/Layout/ScrollSnapBase.cs b/Scripts/Layout/ScrollSnapBase.cs index de479c4..81478cb 100644 --- a/Scripts/Layout/ScrollSnapBase.cs +++ b/Scripts/Layout/ScrollSnapBase.cs @@ -109,7 +109,6 @@ namespace UnityEngine.UI.Extensions } _screensContainer = _scroll_rect.content; - int childCount; if (ChildObjects != null && ChildObjects.Length > 0) { if (_screensContainer.transform.childCount > 0) @@ -117,29 +116,11 @@ namespace UnityEngine.UI.Extensions Debug.LogError("ScrollRect Content has children, this is not supported when using managed Child Objects\n Either remove the ScrollRect Content children or clear the ChildObjects array"); return; } - childCount = ChildObjects.Length; - for (int i = 0; i < childCount; i++) - { - ChildObjects[i] = GameObject.Instantiate(ChildObjects[i]); - ChildObjects[i].transform.SetParent(_screensContainer.transform); - if (MaskArea && ChildObjects[i].activeSelf) - { - ChildObjects[i].SetActive(false); - } - } + InitialiseChildObjectsFromArray(); } else { - childCount = ChildObjects.Length; - ChildObjects = new GameObject[childCount]; - for (int i = 0; i < childCount; i++) - { - ChildObjects[i] = _screensContainer.transform.GetChild(i).gameObject; - if (MaskArea && ChildObjects[i].activeSelf) - { - ChildObjects[i].SetActive(false); - } - } + InitialiseChildObjectsFromScene(); } if (NextButton) @@ -149,6 +130,34 @@ namespace UnityEngine.UI.Extensions PrevButton.GetComponent