diff --git a/Scripts/Layout/HorizontalScrollSnap.cs b/Scripts/Layout/HorizontalScrollSnap.cs index 7cf8ca7..58b3578 100644 --- a/Scripts/Layout/HorizontalScrollSnap.cs +++ b/Scripts/Layout/HorizontalScrollSnap.cs @@ -92,9 +92,19 @@ namespace UnityEngine.UI.Extensions /// /// GameObject to add to the ScrollSnap public void AddChild(GameObject GO) + { + AddChild(GO, false); + } + + /// + /// Add a new child to this Scroll Snap and recalculate it's children + /// + /// GameObject to add to the ScrollSnap + /// Should the world position be updated to it's parent transform? + public void AddChild(GameObject GO, bool WorldPositionStays) { _scroll_rect.horizontalNormalizedPosition = 0; - GO.transform.SetParent(_screensContainer); + GO.transform.SetParent(_screensContainer, WorldPositionStays); DistributePages(); if (MaskArea) UpdateVisible(); @@ -131,6 +141,28 @@ namespace UnityEngine.UI.Extensions SetScrollContainerPosition(); } + /// + /// Remove all children from this ScrollSnap + /// + /// + public void RemoveAllChildren(out GameObject[] ChildrenRemoved) + { + var _screenCount = _screensContainer.childCount; + ChildrenRemoved = new GameObject[_screenCount]; + + for (int i = _screenCount - 1; i >= 0; i--) + { + ChildrenRemoved[i] = _screensContainer.GetChild(i).gameObject; + ChildrenRemoved[i].transform.SetParent(null); + } + + _scroll_rect.horizontalNormalizedPosition = 0; + CurrentPage = 0; + InitialiseChildObjectsFromScene(); + DistributePages(); + if (MaskArea) UpdateVisible(); + } + private void SetScrollContainerPosition() { _scrollStartPosition = _screensContainer.localPosition.x; diff --git a/Scripts/Layout/ScrollSnapBase.cs b/Scripts/Layout/ScrollSnapBase.cs index 8764619..21b9b2e 100644 --- a/Scripts/Layout/ScrollSnapBase.cs +++ b/Scripts/Layout/ScrollSnapBase.cs @@ -78,7 +78,7 @@ namespace UnityEngine.UI.Extensions } internal set { - if (value != _currentPage && value >= 0 && value < _screensContainer.childCount) + if ((value != _currentPage && value >= 0 && value < _screensContainer.childCount) || (value == 0 && _screensContainer.childCount == 0)) { _previousPage = _currentPage; _currentPage = value; @@ -190,7 +190,10 @@ namespace UnityEngine.UI.Extensions internal void UpdateVisible() { //If there are no objects in the scene or a mask, exit - if (!MaskArea && (ChildObjects == null || ChildObjects.Length < 1 || _screensContainer.childCount < 1)) return; + if (!MaskArea || ChildObjects == null || ChildObjects.Length < 1 || _screensContainer.childCount < 1) + { + return; + } _maskSize = _isVertical ? MaskArea.rect.height : MaskArea.rect.width; _halfNoVisibleItems = (int)Math.Round(_maskSize / (_childSize * MaskBuffer), MidpointRounding.AwayFromZero) / 2; diff --git a/Scripts/Layout/VerticalScrollSnap.cs b/Scripts/Layout/VerticalScrollSnap.cs index 2f098ed..0d92abb 100644 --- a/Scripts/Layout/VerticalScrollSnap.cs +++ b/Scripts/Layout/VerticalScrollSnap.cs @@ -87,9 +87,19 @@ namespace UnityEngine.UI.Extensions /// /// GameObject to add to the ScrollSnap public void AddChild(GameObject GO) + { + AddChild(GO, false); + } + + /// + /// Add a new child to this Scroll Snap and recalculate it's children + /// + /// GameObject to add to the ScrollSnap + /// Should the world position be updated to it's parent transform? + public void AddChild(GameObject GO, bool WorldPositionStays) { _scroll_rect.verticalNormalizedPosition = 0; - GO.transform.SetParent(_screensContainer); + GO.transform.SetParent(_screensContainer, WorldPositionStays); InitialiseChildObjectsFromScene(); DistributePages(); if (MaskArea) UpdateVisible(); @@ -127,6 +137,28 @@ namespace UnityEngine.UI.Extensions SetScrollContainerPosition(); } + /// + /// Remove all children from this ScrollSnap + /// + /// + public void RemoveAllChildren(out GameObject[] ChildrenRemoved) + { + var _screenCount = _screensContainer.childCount; + ChildrenRemoved = new GameObject[_screenCount]; + + for (int i = _screenCount - 1; i >= 0; i--) + { + ChildrenRemoved[i] = _screensContainer.GetChild(i).gameObject; + ChildrenRemoved[i].transform.SetParent(null); + } + + _scroll_rect.verticalNormalizedPosition = 0; + CurrentPage = 0; + InitialiseChildObjectsFromScene(); + DistributePages(); + if (MaskArea) UpdateVisible(); + } + private void SetScrollContainerPosition() { _scrollStartPosition = _screensContainer.localPosition.y;