2 patches for HSS/VSS

* Added new overload to AddChild, allows to set WorldPositionStays for added childen
* New RemoveAllChildren function

--HG--
branch : develop_5.3
pull/413/head
Simon Jackson 2017-01-04 12:16:11 +00:00
parent 21ad76580e
commit cd3f79ef7c
3 changed files with 71 additions and 4 deletions

View File

@ -92,9 +92,19 @@ namespace UnityEngine.UI.Extensions
/// </summary> /// </summary>
/// <param name="GO">GameObject to add to the ScrollSnap</param> /// <param name="GO">GameObject to add to the ScrollSnap</param>
public void AddChild(GameObject GO) public void AddChild(GameObject GO)
{
AddChild(GO, false);
}
/// <summary>
/// Add a new child to this Scroll Snap and recalculate it's children
/// </summary>
/// <param name="GO">GameObject to add to the ScrollSnap</param>
/// <param name="WorldPositionStays">Should the world position be updated to it's parent transform?</param>
public void AddChild(GameObject GO, bool WorldPositionStays)
{ {
_scroll_rect.horizontalNormalizedPosition = 0; _scroll_rect.horizontalNormalizedPosition = 0;
GO.transform.SetParent(_screensContainer); GO.transform.SetParent(_screensContainer, WorldPositionStays);
DistributePages(); DistributePages();
if (MaskArea) UpdateVisible(); if (MaskArea) UpdateVisible();
@ -131,6 +141,28 @@ namespace UnityEngine.UI.Extensions
SetScrollContainerPosition(); SetScrollContainerPosition();
} }
/// <summary>
/// Remove all children from this ScrollSnap
/// </summary>
/// <param name="ChildrenRemoved"></param>
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() private void SetScrollContainerPosition()
{ {
_scrollStartPosition = _screensContainer.localPosition.x; _scrollStartPosition = _screensContainer.localPosition.x;

View File

@ -78,7 +78,7 @@ namespace UnityEngine.UI.Extensions
} }
internal set 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; _previousPage = _currentPage;
_currentPage = value; _currentPage = value;
@ -190,7 +190,10 @@ namespace UnityEngine.UI.Extensions
internal void UpdateVisible() internal void UpdateVisible()
{ {
//If there are no objects in the scene or a mask, exit //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; _maskSize = _isVertical ? MaskArea.rect.height : MaskArea.rect.width;
_halfNoVisibleItems = (int)Math.Round(_maskSize / (_childSize * MaskBuffer), MidpointRounding.AwayFromZero) / 2; _halfNoVisibleItems = (int)Math.Round(_maskSize / (_childSize * MaskBuffer), MidpointRounding.AwayFromZero) / 2;

View File

@ -87,9 +87,19 @@ namespace UnityEngine.UI.Extensions
/// </summary> /// </summary>
/// <param name="GO">GameObject to add to the ScrollSnap</param> /// <param name="GO">GameObject to add to the ScrollSnap</param>
public void AddChild(GameObject GO) public void AddChild(GameObject GO)
{
AddChild(GO, false);
}
/// <summary>
/// Add a new child to this Scroll Snap and recalculate it's children
/// </summary>
/// <param name="GO">GameObject to add to the ScrollSnap</param>
/// <param name="WorldPositionStays">Should the world position be updated to it's parent transform?</param>
public void AddChild(GameObject GO, bool WorldPositionStays)
{ {
_scroll_rect.verticalNormalizedPosition = 0; _scroll_rect.verticalNormalizedPosition = 0;
GO.transform.SetParent(_screensContainer); GO.transform.SetParent(_screensContainer, WorldPositionStays);
InitialiseChildObjectsFromScene(); InitialiseChildObjectsFromScene();
DistributePages(); DistributePages();
if (MaskArea) UpdateVisible(); if (MaskArea) UpdateVisible();
@ -127,6 +137,28 @@ namespace UnityEngine.UI.Extensions
SetScrollContainerPosition(); SetScrollContainerPosition();
} }
/// <summary>
/// Remove all children from this ScrollSnap
/// </summary>
/// <param name="ChildrenRemoved"></param>
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() private void SetScrollContainerPosition()
{ {
_scrollStartPosition = _screensContainer.localPosition.y; _scrollStartPosition = _screensContainer.localPosition.y;