From 0845316a6c7de188434fa5376b338c4901134c07 Mon Sep 17 00:00:00 2001 From: Lior Tal Date: Wed, 14 Oct 2015 15:04:09 +0000 Subject: [PATCH 1/9] Improve memory behaviour by pre-allocating the right amount of memory. --HG-- branch : liortal/improve-memory-behaviour-by-preallocatin-1444835033293 --- Scripts/NicerOutline.cs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Scripts/NicerOutline.cs b/Scripts/NicerOutline.cs index 797d69f..bb0c68c 100644 --- a/Scripts/NicerOutline.cs +++ b/Scripts/NicerOutline.cs @@ -8,6 +8,9 @@ namespace UnityEngine.UI.Extensions [AddComponentMenu("UI/Effects/Extensions/Nicer Outline")] public class NicerOutline : BaseMeshEffect { + // A constant factor used when allocating the vertex list. + private const int GROWTH_FACTOR = 24; + [SerializeField] private Color m_EffectColor = new Color (0f, 0f, 0f, 0.5f); @@ -128,7 +131,7 @@ namespace UnityEngine.UI.Extensions { return; } - List < UIVertex > verts = new List(); + List < UIVertex > verts = new List(GROWTH_FACTOR * mesh.vertices.Length); using (var helper = new VertexHelper(mesh)) { helper.GetUIVertexStream(verts); From 6cba848e43668dfbd199ac60beaa67d9513801be Mon Sep 17 00:00:00 2001 From: Lior Tal Date: Thu, 15 Oct 2015 06:53:35 +0000 Subject: [PATCH 2/9] Fix the previous commit for improving memory consumption. Allocate the list with the correct capacity ahead, and avoid any needs for increasing the capacity when applying the shadow later. --HG-- branch : liortal/improve-memory-behaviour-by-preallocatin-1444835033293 --- Scripts/NicerOutline.cs | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/Scripts/NicerOutline.cs b/Scripts/NicerOutline.cs index bb0c68c..b284765 100644 --- a/Scripts/NicerOutline.cs +++ b/Scripts/NicerOutline.cs @@ -8,8 +8,7 @@ namespace UnityEngine.UI.Extensions [AddComponentMenu("UI/Effects/Extensions/Nicer Outline")] public class NicerOutline : BaseMeshEffect { - // A constant factor used when allocating the vertex list. - private const int GROWTH_FACTOR = 24; + private const int VERTICES_PER_QUAD = 6; [SerializeField] private Color m_EffectColor = new Color (0f, 0f, 0f, 0.5f); @@ -94,10 +93,6 @@ namespace UnityEngine.UI.Extensions { UIVertex vt; - var neededCpacity = verts.Count * 2; - if (verts.Capacity < neededCpacity) - verts.Capacity = neededCpacity; - for (int i = start; i < end; ++i) { vt = verts[i]; @@ -117,10 +112,6 @@ namespace UnityEngine.UI.Extensions protected void ApplyShadow(List verts, Color32 color, int start, int end, float x, float y) { - var neededCpacity = verts.Count * 2; - if (verts.Capacity < neededCpacity) - verts.Capacity = neededCpacity; - ApplyShadowZeroAlloc(verts, color, start, end, x, y); } @@ -131,8 +122,12 @@ namespace UnityEngine.UI.Extensions { return; } - List < UIVertex > verts = new List(GROWTH_FACTOR * mesh.vertices.Length); - using (var helper = new VertexHelper(mesh)) + + // Initilize a list with the correct capacity, to avoid unneeded allocations later. + // The list will hold 9 copies of the vertex data (original + 8 copies). + List < UIVertex > verts = new List(9 * (mesh.vertices.Length / 4 * VERTICES_PER_QUAD)); + + using (var helper = new VertexHelper(mesh)) { helper.GetUIVertexStream(verts); } From 0c15f750010589224850ab757d29323125711aac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C4=B9=E2=80=9A=20Kapalla?= Date: Sat, 17 Oct 2015 18:30:30 +0200 Subject: [PATCH 4/9] tooltip positionig relative to triggering object --HG-- branch : xesenix_upgrades --- Scripts/BoundTooltip/BoundTooltipTrigger.cs | 93 ++++++++++++--------- 1 file changed, 54 insertions(+), 39 deletions(-) diff --git a/Scripts/BoundTooltip/BoundTooltipTrigger.cs b/Scripts/BoundTooltip/BoundTooltipTrigger.cs index 9776722..415d70e 100644 --- a/Scripts/BoundTooltip/BoundTooltipTrigger.cs +++ b/Scripts/BoundTooltip/BoundTooltipTrigger.cs @@ -1,39 +1,54 @@ -///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/ - -using UnityEngine.EventSystems; - -namespace UnityEngine.UI.Extensions -{ - [AddComponentMenu("UI/Extensions/Bound Tooltip/Tooltip Trigger")] - public class BoundTooltipTrigger : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler, ISelectHandler, IDeselectHandler - { - public string text; - - public void OnPointerEnter(PointerEventData eventData) - { - StartHover(new Vector3(eventData.position.x, eventData.position.y, 0f)); - } - public void OnSelect(BaseEventData eventData) - { - StartHover(transform.position); - } - public void OnPointerExit(PointerEventData eventData) - { - StopHover(); - } - public void OnDeselect(BaseEventData eventData) - { - StopHover(); - } - - void StartHover(Vector3 position) - { - BoundTooltipItem.Instance.ShowTooltip(text, position); - } - void StopHover() - { - BoundTooltipItem.Instance.HideTooltip(); - } - } -} +///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/ +using UnityEngine.EventSystems; + +namespace UnityEngine.UI.Extensions +{ + [AddComponentMenu("UI/Extensions/Bound Tooltip/Tooltip Trigger")] + public class BoundTooltipTrigger : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler, ISelectHandler, IDeselectHandler + { + [TextAreaAttribute] + public string text; + + public bool useMousePosition = false; + + public Vector3 offset; + + public void OnPointerEnter(PointerEventData eventData) + { + if (useMousePosition) + { + StartHover(new Vector3(eventData.position.x, eventData.position.y, 0f)); + } + else + { + StartHover(transform.position + offset); + } + } + + public void OnSelect(BaseEventData eventData) + { + StartHover(transform.position); + } + + public void OnPointerExit(PointerEventData eventData) + { + StopHover(); + } + + public void OnDeselect(BaseEventData eventData) + { + StopHover(); + } + + void StartHover(Vector3 position) + { + BoundTooltipItem.Instance.ShowTooltip(text, position); + } + + void StopHover() + { + BoundTooltipItem.Instance.HideTooltip(); + } + } +} From 0c68f929f6363c8d17767cc8852e6ed54e7f240e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C4=B9=E2=80=9A=20Kapalla?= Date: Sat, 17 Oct 2015 18:34:31 +0200 Subject: [PATCH 5/9] - enabling disabling prev/next button based on position of scrollsnap - linking items size with scrollrect scroll sensitivity --HG-- branch : xesenix_upgrades --- Scripts/ScrollSnap.cs | 558 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 558 insertions(+) create mode 100644 Scripts/ScrollSnap.cs diff --git a/Scripts/ScrollSnap.cs b/Scripts/ScrollSnap.cs new file mode 100644 index 0000000..0331db4 --- /dev/null +++ b/Scripts/ScrollSnap.cs @@ -0,0 +1,558 @@ +/// Credit BinaryX +/// Sourced from - http://forum.unity3d.com/threads/scripts-useful-4-6-scripts-collection.264161/page-2#post-1945602 +/// Updated by ddreaper - removed dependency on a custom ScrollRect script. Now implements drag interfaces and standard Scroll Rect. +/// Update by xesenix - rewrited almost entire code +/// - configuration for direction move instead of 2 concurrent class (easiear to change direction in editor) +/// - supports list layouted with horizontal or vertical layout need to match direction with type of layout used +/// - dynamicly checks if scrolled list size changes and recalculates anchor positions +/// and item size based on itemsVisibleAtOnce and size of root container +/// if you dont wish to use this auto resize turn of autoLayoutItems +/// - fixed current page made it independant from pivot +/// - replaced pagination with delegate function +using System; +using UnityEngine.EventSystems; + +namespace UnityEngine.UI.Extensions +{ + [ExecuteInEditMode] + [RequireComponent(typeof(ScrollRect))] + [AddComponentMenu("UI/Extensions/Scroll Snap")] + public class ScrollSnap : MonoBehaviour, IBeginDragHandler, IEndDragHandler, IDragHandler + { + // 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) + public enum ScrollDirection + { + Horizontal, + Vertical + } + + public delegate void PageSnapChange(int page); + + public event PageSnapChange onPageChange; + + public ScrollDirection direction = ScrollDirection.Horizontal; + + protected ScrollRect scrollRect; + + protected RectTransform scrollRectTransform; + + protected Transform listContainerTransform; + + protected RectTransform rectTransform; + + int pages; + + protected int startingPage = 0; + + // anchor points to lerp to to see child on certain indexes + protected Vector3[] pageAnchorPositions; + + protected Vector3 lerpTarget; + + protected bool lerp; + + // item list related + protected float listContainerMinPosition; + + protected float listContainerMaxPosition; + + protected float listContainerSize; + + protected RectTransform listContainerRectTransform; + + protected Vector2 listContainerCachedSize; + + protected float itemSize; + + protected int itemsCount = 0; + + [Tooltip("Button to go to the next page. (optional)")] + public Button nextButton; + + [Tooltip("Button to go to the previous page. (optional)")] + public Button prevButton; + + [Tooltip("Number of items visible in one page of scroll frame.")] + [RangeAttribute(1,100)] + public int itemsVisibleAtOnce = 1; + + [Tooltip("Sets minimum width of list items to 1/itemsVisibleAtOnce.")] + public bool autoLayoutItems = true; + + [Tooltip("If you wish to update scrollbar numberOfSteps to number of active children on list.")] + 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 int fastSwipeThreshold = 100; + + // drag related + protected bool startDrag = true; + + protected Vector3 positionOnDragStart = new Vector3(); + + protected int pageOnDragStart; + + protected bool fastSwipeTimer = false; + + protected int fastSwipeCounter = 0; + + protected int fastSwipeTarget = 10; + + // Use this for initialization + void Awake() + { + lerp = false; + + scrollRect = gameObject.GetComponent(); + scrollRectTransform = gameObject.GetComponent(); + listContainerTransform = scrollRect.content; + listContainerRectTransform = listContainerTransform.GetComponent(); + + rectTransform = listContainerTransform.gameObject.GetComponent(); + UpdateListItemsSize(); + UpdateListItemPositions(); + + ChangePage(CurrentPage()); + + if (nextButton) + { + nextButton.GetComponent