Merged in xesenix/unity-ui-extensions/xesenix_upgrades (pull request #13)

Xesenix_upgrades

--HG--
branch : develop_5.2
release
Simon Jackson 2015-10-21 10:37:13 +01:00
commit 2e14fd1fd6
2 changed files with 267 additions and 177 deletions

View File

@ -1,6 +1,5 @@
///Credit Martin Nerurkar // www.martin.nerurkar.de // www.sharkbombs.com ///Credit Martin Nerurkar // www.martin.nerurkar.de // www.sharkbombs.com
///Sourced from - http://www.sharkbombs.com/2015/02/10/tooltips-with-the-new-unity-ui-ugui/ ///Sourced from - http://www.sharkbombs.com/2015/02/10/tooltips-with-the-new-unity-ui-ugui/
using UnityEngine.EventSystems; using UnityEngine.EventSystems;
namespace UnityEngine.UI.Extensions namespace UnityEngine.UI.Extensions
@ -8,20 +7,35 @@ namespace UnityEngine.UI.Extensions
[AddComponentMenu("UI/Extensions/Bound Tooltip/Tooltip Trigger")] [AddComponentMenu("UI/Extensions/Bound Tooltip/Tooltip Trigger")]
public class BoundTooltipTrigger : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler, ISelectHandler, IDeselectHandler public class BoundTooltipTrigger : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler, ISelectHandler, IDeselectHandler
{ {
[TextAreaAttribute]
public string text; public string text;
public bool useMousePosition = false;
public Vector3 offset;
public void OnPointerEnter(PointerEventData eventData) public void OnPointerEnter(PointerEventData eventData)
{
if (useMousePosition)
{ {
StartHover(new Vector3(eventData.position.x, eventData.position.y, 0f)); StartHover(new Vector3(eventData.position.x, eventData.position.y, 0f));
} }
else
{
StartHover(transform.position + offset);
}
}
public void OnSelect(BaseEventData eventData) public void OnSelect(BaseEventData eventData)
{ {
StartHover(transform.position); StartHover(transform.position);
} }
public void OnPointerExit(PointerEventData eventData) public void OnPointerExit(PointerEventData eventData)
{ {
StopHover(); StopHover();
} }
public void OnDeselect(BaseEventData eventData) public void OnDeselect(BaseEventData eventData)
{ {
StopHover(); StopHover();
@ -31,6 +45,7 @@ namespace UnityEngine.UI.Extensions
{ {
BoundTooltipItem.Instance.ShowTooltip(text, position); BoundTooltipItem.Instance.ShowTooltip(text, position);
} }
void StopHover() void StopHover()
{ {
BoundTooltipItem.Instance.HideTooltip(); BoundTooltipItem.Instance.HideTooltip();

View File

@ -9,7 +9,6 @@
/// if you dont wish to use this auto resize turn of autoLayoutItems /// if you dont wish to use this auto resize turn of autoLayoutItems
/// - fixed current page made it independant from pivot /// - fixed current page made it independant from pivot
/// - replaced pagination with delegate function /// - replaced pagination with delegate function
using System; using System;
using UnityEngine.EventSystems; using UnityEngine.EventSystems;
@ -22,10 +21,11 @@ namespace UnityEngine.UI.Extensions
{ {
// needed becouse of reversed behavior of axis Y compared to X // needed becouse of reversed behavior of axis Y compared to X
// (positions of children lower in children list in horizontal directions grows when in vertical it gets smaller) // (positions of children lower in children list in horizontal directions grows when in vertical it gets smaller)
public enum ScrollDirection { public enum ScrollDirection
{
Horizontal, Horizontal,
Vertical Vertical
}; }
public delegate void PageSnapChange(int page); public delegate void PageSnapChange(int page);
@ -34,11 +34,12 @@ namespace UnityEngine.UI.Extensions
public ScrollDirection direction = ScrollDirection.Horizontal; public ScrollDirection direction = ScrollDirection.Horizontal;
protected ScrollRect scrollRect; protected ScrollRect scrollRect;
protected RectTransform scrollRectTransform;
protected Transform listContainerTransform;
protected RectTransform rectTransform;
protected int items = 0; protected RectTransform scrollRectTransform;
protected Transform listContainerTransform;
protected RectTransform rectTransform;
int pages; int pages;
@ -46,22 +47,31 @@ namespace UnityEngine.UI.Extensions
// anchor points to lerp to to see child on certain indexes // anchor points to lerp to to see child on certain indexes
protected Vector3[] pageAnchorPositions; protected Vector3[] pageAnchorPositions;
protected Vector3 lerpTarget; protected Vector3 lerpTarget;
protected bool lerp; protected bool lerp;
// item list related // item list related
protected float listContainerMinPosition; protected float listContainerMinPosition;
protected float listContainerMaxPosition; protected float listContainerMaxPosition;
protected float listContainerSize; protected float listContainerSize;
protected RectTransform listContainerRectTransform; protected RectTransform listContainerRectTransform;
protected Vector2 listContainerCachedSize; protected Vector2 listContainerCachedSize;
protected float itemSize; protected float itemSize;
protected int itemsCount = 0;
[Tooltip("Button to go to the next page. (optional)")] [Tooltip("Button to go to the next page. (optional)")]
public GameObject nextButton; public Button nextButton;
[Tooltip("Button to go to the previous page. (optional)")] [Tooltip("Button to go to the previous page. (optional)")]
public GameObject prevButton; public Button prevButton;
[Tooltip("Number of items visible in one page of scroll frame.")] [Tooltip("Number of items visible in one page of scroll frame.")]
[RangeAttribute(1,100)] [RangeAttribute(1,100)]
@ -73,20 +83,28 @@ namespace UnityEngine.UI.Extensions
[Tooltip("If you wish to update scrollbar numberOfSteps to number of active children on list.")] [Tooltip("If you wish to update scrollbar numberOfSteps to number of active children on list.")]
public bool linkScrolbarSteps = false; public bool linkScrolbarSteps = false;
[Tooltip("If you wish to update scrollrect sensitivity to size of list element.")]
public bool linkScrolrectScrollSensitivity = false;
public Boolean useFastSwipe = true; public Boolean useFastSwipe = true;
public int fastSwipeThreshold = 100; public int fastSwipeThreshold = 100;
// drag related // drag related
protected bool startDrag = true; protected bool startDrag = true;
protected Vector3 positionOnDragStart = new Vector3(); protected Vector3 positionOnDragStart = new Vector3();
protected int pageOnDragStart; protected int pageOnDragStart;
protected bool fastSwipeTimer = false; protected bool fastSwipeTimer = false;
protected int fastSwipeCounter = 0; protected int fastSwipeCounter = 0;
protected int fastSwipeTarget = 10; protected int fastSwipeTarget = 10;
// Use this for initialization // Use this for initialization
void Start() void Awake()
{ {
lerp = false; lerp = false;
@ -99,34 +117,49 @@ namespace UnityEngine.UI.Extensions
UpdateListItemsSize(); UpdateListItemsSize();
UpdateListItemPositions(); UpdateListItemPositions();
ChangePage (CurrentPage ()); PageChanged(CurrentPage());
if (nextButton) if (nextButton)
{ {
nextButton.GetComponent<Button> ().onClick.AddListener (() => { NextScreen (); }); nextButton.GetComponent<Button>().onClick.AddListener(() => {
NextScreen(); });
} }
if (prevButton) if (prevButton)
{ {
prevButton.GetComponent<Button> ().onClick.AddListener (() => { PreviousScreen (); }); prevButton.GetComponent<Button>().onClick.AddListener(() => {
PreviousScreen(); });
} }
} }
void Start()
{
Awake();
}
public void UpdateListItemsSize() public void UpdateListItemsSize()
{ {
float size = 0; float size = 0;
float currentSize = 0;
if (direction == ScrollSnap.ScrollDirection.Horizontal) if (direction == ScrollSnap.ScrollDirection.Horizontal)
{ {
size = scrollRectTransform.rect.width / itemsVisibleAtOnce; size = scrollRectTransform.rect.width / itemsVisibleAtOnce;
currentSize = listContainerRectTransform.rect.width / itemsCount;
} }
else else
{ {
size = scrollRectTransform.rect.height / itemsVisibleAtOnce; size = scrollRectTransform.rect.height / itemsVisibleAtOnce;
currentSize = listContainerRectTransform.rect.height / itemsCount;
} }
itemSize = size; itemSize = size;
if (autoLayoutItems && size != itemSize) if (linkScrolrectScrollSensitivity)
{
scrollRect.scrollSensitivity = itemSize;
}
if (autoLayoutItems && currentSize != size && itemsCount > 0)
{ {
if (direction == ScrollSnap.ScrollDirection.Horizontal) if (direction == ScrollSnap.ScrollDirection.Horizontal)
{ {
@ -174,14 +207,16 @@ namespace UnityEngine.UI.Extensions
// checking how many children of list are active // checking how many children of list are active
int activeCount = 0; int activeCount = 0;
foreach (var tr in listContainerTransform) { foreach (var tr in listContainerTransform)
if (((Transform)tr).gameObject.activeInHierarchy) { {
if (((Transform) tr).gameObject.activeInHierarchy)
{
activeCount++; activeCount++;
} }
} }
// if anything changed since last check reinitialize anchors list // if anything changed since last check reinitialize anchors list
items = 0; itemsCount = 0;
Array.Resize(ref pageAnchorPositions, activeCount); Array.Resize(ref pageAnchorPositions, activeCount);
if (activeCount > 0) if (activeCount > 0)
@ -190,6 +225,7 @@ namespace UnityEngine.UI.Extensions
if (direction == ScrollDirection.Horizontal) if (direction == ScrollDirection.Horizontal)
{ {
//Debug.Log ("-------------looking for list spanning range----------------");
// looking for list spanning range min/max // looking for list spanning range min/max
scrollRect.horizontalNormalizedPosition = 0; scrollRect.horizontalNormalizedPosition = 0;
listContainerMaxPosition = listContainerTransform.localPosition.x; listContainerMaxPosition = listContainerTransform.localPosition.x;
@ -197,6 +233,8 @@ namespace UnityEngine.UI.Extensions
listContainerMinPosition = listContainerTransform.localPosition.x; listContainerMinPosition = listContainerTransform.localPosition.x;
listContainerSize = listContainerMaxPosition - listContainerMinPosition; listContainerSize = listContainerMaxPosition - listContainerMinPosition;
//itemSize = pages > 1 ? listContainerSize / (float)(pages - 1) : listContainerSize / activeCount;
//itemSize *= (pages + itemsVisibleAtOnce) / (pages + 1);
for (var i = 0; i < pages; i ++) for (var i = 0; i < pages; i ++)
{ {
@ -206,9 +244,12 @@ namespace UnityEngine.UI.Extensions
listContainerTransform.localPosition.z listContainerTransform.localPosition.z
); );
} }
//Debug.Log (String.Format("min:{0:0.0}, max:{1:0.0}, pages: {2:0}, width: {3:0}", listContainerMinPosition, listContainerMaxPosition, pages, listContainerSize/pages));
} }
else else
{ {
//Debug.Log ("-------------looking for list spanning range----------------");
// looking for list spanning range // looking for list spanning range
scrollRect.verticalNormalizedPosition = 1; scrollRect.verticalNormalizedPosition = 1;
listContainerMinPosition = listContainerTransform.localPosition.y; listContainerMinPosition = listContainerTransform.localPosition.y;
@ -216,6 +257,8 @@ namespace UnityEngine.UI.Extensions
listContainerMaxPosition = listContainerTransform.localPosition.y; listContainerMaxPosition = listContainerTransform.localPosition.y;
listContainerSize = listContainerMaxPosition - listContainerMinPosition; listContainerSize = listContainerMaxPosition - listContainerMinPosition;
//itemSize = pages > 1 ? listContainerSize / (float)(pages - 1) : listContainerSize / activeCount;
//itemSize *= (pages + itemsVisibleAtOnce) / (pages + 1);
for (var i = 0; i < pages; i ++) for (var i = 0; i < pages; i ++)
{ {
@ -224,21 +267,23 @@ namespace UnityEngine.UI.Extensions
listContainerMinPosition + itemSize * i, listContainerMinPosition + itemSize * i,
listContainerTransform.localPosition.z listContainerTransform.localPosition.z
); );
//Debug.Log (String.Format("pos[{0:0}]={1} {2:0%}", i, pageAnchorPositions[i], pages > 1 ? i/(float)(pages-1) : (float)i));
} }
foreach (var tr in listContainerTransform) { //Debug.Log (String.Format("min:{0:0.0}, max:{1:0.0}, pages: {2:0}, width: {3:0}", listContainerMinPosition, listContainerMaxPosition, pages, listContainerSize/pages));
if (((Transform)tr).gameObject.activeInHierarchy) {
activeCount++;
}
}
} }
UpdateScrollbar(linkScrolbarSteps); UpdateScrollbar(linkScrolbarSteps);
startingPage = Mathf.Min(startingPage, pages); startingPage = Mathf.Min(startingPage, pages);
ResetPage(); ResetPage();
} }
items = activeCount;
if (itemsCount != activeCount)
{
PageChanged(CurrentPage());
}
itemsCount = activeCount;
listContainerCachedSize.Set(listContainerRectTransform.rect.size.x, listContainerRectTransform.rect.size.y); listContainerCachedSize.Set(listContainerRectTransform.rect.size.x, listContainerRectTransform.rect.size.y);
} }
@ -294,7 +339,7 @@ namespace UnityEngine.UI.Extensions
} }
} }
void Update() void LateUpdate()
{ {
UpdateListItemsSize(); UpdateListItemsSize();
UpdateListItemPositions(); UpdateListItemPositions();
@ -311,12 +356,14 @@ namespace UnityEngine.UI.Extensions
lerp = false; lerp = false;
UpdateScrollbar(linkScrolbarSteps); UpdateScrollbar(linkScrolbarSteps);
//Debug.Log(String.Format("Lerp: {0}/{1} page: {2:0}", lerpTarget, listContainerTransform.localPosition, CurrentPage()));
} }
//change the info bullets at the bottom of the screen. Just for visual effect //change the info bullets at the bottom of the screen. Just for visual effect
if (Vector3.Distance(listContainerTransform.localPosition, lerpTarget) < 10f) if (Vector3.Distance(listContainerTransform.localPosition, lerpTarget) < 10f)
{ {
ChangePage(CurrentPage()); PageChanged(CurrentPage());
} }
} }
@ -324,6 +371,8 @@ namespace UnityEngine.UI.Extensions
{ {
fastSwipeCounter++; fastSwipeCounter++;
} }
//Debug.Log(String.Format("Update: {0}", listContainerTransform.localPosition));
} }
private bool fastSwipe = false; //to determine if a fast swipe was performed private bool fastSwipe = false; //to determine if a fast swipe was performed
@ -339,7 +388,7 @@ namespace UnityEngine.UI.Extensions
lerp = true; lerp = true;
lerpTarget = pageAnchorPositions[CurrentPage() + 1]; lerpTarget = pageAnchorPositions[CurrentPage() + 1];
ChangePage(CurrentPage() + 1); PageChanged(CurrentPage() + 1);
} }
} }
@ -353,7 +402,7 @@ namespace UnityEngine.UI.Extensions
lerp = true; lerp = true;
lerpTarget = pageAnchorPositions[CurrentPage() - 1]; lerpTarget = pageAnchorPositions[CurrentPage() - 1];
ChangePage(CurrentPage() - 1); PageChanged(CurrentPage() - 1);
} }
} }
@ -367,7 +416,7 @@ namespace UnityEngine.UI.Extensions
lerpTarget = pageAnchorPositions[targetPage]; lerpTarget = pageAnchorPositions[targetPage];
ChangePage(targetPage); PageChanged(targetPage);
} }
} }
@ -381,7 +430,7 @@ namespace UnityEngine.UI.Extensions
lerpTarget = pageAnchorPositions[targetPage]; lerpTarget = pageAnchorPositions[targetPage];
ChangePage(targetPage); PageChanged(targetPage);
} }
} }
@ -404,14 +453,38 @@ namespace UnityEngine.UI.Extensions
float page = pos / itemSize; float page = pos / itemSize;
//Debug.Log (String.Format("min:{0:0.0}, max:{1:0.0}, current:{2:0.0}/{4:0.0}, page:{3:0.0}, target:{5}", listContainerMinPosition, listContainerMaxPosition, pos, page, listContainerTransform.localPosition.x, pageAnchorPositions[Mathf.RoundToInt(page)]));
return Mathf.Clamp(Mathf.RoundToInt(page), 0, pages); return Mathf.Clamp(Mathf.RoundToInt(page), 0, pages);
} }
public void ChangePage(int page)
{
if (0 <= page && page < pages)
{
lerp = true;
lerpTarget = pageAnchorPositions[page];
PageChanged(page);
}
}
//changes the bullets on the bottom of the page - pagination //changes the bullets on the bottom of the page - pagination
private void ChangePage(int currentPage) private void PageChanged(int currentPage)
{ {
startingPage = currentPage; startingPage = currentPage;
if (nextButton)
{
nextButton.interactable = currentPage < pages - 1;
}
if (prevButton)
{
prevButton.interactable = currentPage > 0;
}
if (onPageChange != null) if (onPageChange != null)
{ {
onPageChange(currentPage); onPageChange(currentPage);
@ -428,6 +501,8 @@ namespace UnityEngine.UI.Extensions
positionOnDragStart = eventData.position; positionOnDragStart = eventData.position;
pageOnDragStart = CurrentPage(); pageOnDragStart = CurrentPage();
//Debug.Log (String.Format("pageOnDragStart:{0:0}", pageOnDragStart));
} }
public void OnEndDrag(PointerEventData eventData) public void OnEndDrag(PointerEventData eventData)