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 UnityEngine.EventSystems;
using UnityEngine.Events;
@ -185,12 +189,61 @@ namespace UnityEngine.UI.Extensions
if (prevButton)
prevButton.GetComponent<Button>().onClick.AddListener(() => { PreviousItem(); });
if (IsScrollRectAvailable)
{
SetupDrivenTransforms();
SetupSnapScroll();
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()
{
@ -469,11 +522,16 @@ namespace UnityEngine.UI.Extensions
}
public void OnEndDrag(PointerEventData ped)
{
if (IsScrollRectAvailable)
{
StartCoroutine("SlideAndLerp");
}
}
private void Update()
{
if (IsScrollRectAvailable)
{
if (_closestItem != ClosestItemIndex)
{
@ -482,6 +540,7 @@ namespace UnityEngine.UI.Extensions
_closestItem = ClosestItemIndex;
}
}
}
private IEnumerator SlideAndLerp()
{

View File

@ -1,4 +1,6 @@
namespace UnityEngine.UI.Extensions
using System;
namespace UnityEngine.UI.Extensions
{
public static class ExtentionMethods
{
@ -11,5 +13,21 @@
}
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.
}
}
}