Merged in ContentScrollSnapupdate (pull request #103)

ContentScrollSnapupdate
pull/413/head
Simon Jackson 2020-10-23 11:43:25 +00:00
commit b268970a79
2 changed files with 89 additions and 12 deletions

View File

@ -1,4 +1,8 @@
using System.Collections; /// Credit Beka Westberg
/// Sourced from - https://bitbucket.org/UnityUIExtensions/unity-ui-extensions/pull-requests/28
/// Updated by SimonDarksideJ - Added some exception management and a SetNewItems to replace the content programmatically
using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using UnityEngine.EventSystems; using UnityEngine.EventSystems;
using UnityEngine.Events; using UnityEngine.Events;
@ -185,11 +189,60 @@ namespace UnityEngine.UI.Extensions
if (prevButton) if (prevButton)
prevButton.GetComponent<Button>().onClick.AddListener(() => { PreviousItem(); }); prevButton.GetComponent<Button>().onClick.AddListener(() => { PreviousItem(); });
SetupDrivenTransforms(); if (IsScrollRectAvailable)
SetupSnapScroll(); {
scrollRect.horizontalNormalizedPosition = 0; SetupDrivenTransforms();
_closestItem = 0; SetupSnapScroll();
GoTo(startInfo); scrollRect.horizontalNormalizedPosition = 0;
_closestItem = 0;
GoTo(startInfo);
}
}
public void SetNewItems(ref List<Transform> newItems)
{
if (scrollRect && contentTransform)
{
for (int i = scrollRect.content.childCount - 1; i >= 0; i--)
{
Transform child = contentTransform.GetChild(i);
child.SetParent(null);
GameObject.DestroyImmediate(child.gameObject);
}
foreach (Transform item in newItems)
{
GameObject newItem = item.gameObject;
if (newItem.IsPrefab())
{
newItem = Instantiate(item.gameObject, contentTransform);
}
else
{
newItem.transform.SetParent(contentTransform);
}
}
SetupDrivenTransforms();
SetupSnapScroll();
scrollRect.horizontalNormalizedPosition = 0;
_closestItem = 0;
GoTo(startInfo);
}
}
private bool IsScrollRectAvailable
{
get
{
if (scrollRect &&
contentTransform &&
contentTransform.childCount > 0)
{
return true;
}
return false;
}
} }
private void OnDisable() private void OnDisable()
@ -470,16 +523,22 @@ namespace UnityEngine.UI.Extensions
public void OnEndDrag(PointerEventData ped) public void OnEndDrag(PointerEventData ped)
{ {
StartCoroutine("SlideAndLerp"); if (IsScrollRectAvailable)
{
StartCoroutine("SlideAndLerp");
}
} }
private void Update() private void Update()
{ {
if (_closestItem != ClosestItemIndex) if (IsScrollRectAvailable)
{ {
CurrentItemChanged.Invoke(ClosestItemIndex); if (_closestItem != ClosestItemIndex)
ChangePaginationInfo(ClosestItemIndex); {
_closestItem = ClosestItemIndex; CurrentItemChanged.Invoke(ClosestItemIndex);
ChangePaginationInfo(ClosestItemIndex);
_closestItem = ClosestItemIndex;
}
} }
} }

View File

@ -1,4 +1,6 @@
namespace UnityEngine.UI.Extensions using System;
namespace UnityEngine.UI.Extensions
{ {
public static class ExtentionMethods public static class ExtentionMethods
{ {
@ -11,5 +13,21 @@
} }
return result; return result;
} }
public static bool IsPrefab(this GameObject gameObject)
{
if (gameObject == null)
{
throw new ArgumentNullException(nameof(gameObject));
}
return
!gameObject.scene.IsValid() &&
!gameObject.scene.isLoaded &&
gameObject.GetInstanceID() >= 0 &&
// I noticed that ones with IDs under 0 were objects I didn't recognize
!gameObject.hideFlags.HasFlag(HideFlags.HideInHierarchy);
// I don't care about GameObjects *inside* prefabs, just the overall prefab.
}
} }
} }