diff --git a/Scripts/Controls/BoxSlider.cs b/Scripts/Controls/BoxSlider.cs index ccc8d9e..68ede86 100644 --- a/Scripts/Controls/BoxSlider.cs +++ b/Scripts/Controls/BoxSlider.cs @@ -7,8 +7,8 @@ using UnityEngine.EventSystems; namespace UnityEngine.UI.Extensions { - [AddComponentMenu("UI/BoxSlider", 35)] [RequireComponent(typeof(RectTransform))] + [AddComponentMenu("UI/Extensions/BoxSlider")] public class BoxSlider : Selectable, IDragHandler, IInitializePotentialDragHandler, ICanvasElement { public enum Direction @@ -30,15 +30,15 @@ namespace UnityEngine.UI.Extensions [SerializeField] private float m_MinValue = 0; - public float MinValue { get { return m_MinValue; } set { if (SetStruct(ref m_MinValue, value)) { Set(m_ValueX); SetY(m_ValueY); UpdateVisuals(); } } } + public float MinValue { get { return m_MinValue; } set { if (SetStruct(ref m_MinValue, value)) { SetX(m_ValueX); SetY(m_ValueY); UpdateVisuals(); } } } [SerializeField] private float m_MaxValue = 1; - public float MaxValue { get { return m_MaxValue; } set { if (SetStruct(ref m_MaxValue, value)) { Set(m_ValueX); SetY(m_ValueY); UpdateVisuals(); } } } + public float MaxValue { get { return m_MaxValue; } set { if (SetStruct(ref m_MaxValue, value)) { SetX(m_ValueX); SetY(m_ValueY); UpdateVisuals(); } } } [SerializeField] private bool m_WholeNumbers = false; - public bool WholeNumbers { get { return m_WholeNumbers; } set { if (SetStruct(ref m_WholeNumbers, value)) { Set(m_ValueX); SetY(m_ValueY); UpdateVisuals(); } } } + public bool WholeNumbers { get { return m_WholeNumbers; } set { if (SetStruct(ref m_WholeNumbers, value)) { SetX(m_ValueX); SetY(m_ValueY); UpdateVisuals(); } } } [SerializeField] private float m_ValueX = 1f; @@ -52,7 +52,7 @@ namespace UnityEngine.UI.Extensions } set { - Set(value); + SetX(value); } } @@ -109,9 +109,6 @@ namespace UnityEngine.UI.Extensions // Private fields - //private Image m_FillImage; - //private Transform m_FillTransform; - //private RectTransform m_FillContainerRect; private Transform m_HandleTransform; private RectTransform m_HandleContainerRect; @@ -137,7 +134,7 @@ namespace UnityEngine.UI.Extensions m_MaxValue = Mathf.Round(m_MaxValue); } UpdateCachedReferences(); - Set(m_ValueX, false); + SetX(m_ValueX, false); SetY(m_ValueY, false); // Update rects since other things might affect them even if value didn't change. UpdateVisuals(); @@ -189,7 +186,7 @@ namespace UnityEngine.UI.Extensions { base.OnEnable(); UpdateCachedReferences(); - Set(m_ValueX, false); + SetX(m_ValueX, false); SetY(m_ValueY, false); // Update rects since they need to be initialized correctly. UpdateVisuals(); @@ -217,12 +214,12 @@ namespace UnityEngine.UI.Extensions } // Set the valueUpdate the visible Image. - void Set(float input) + void SetX(float input) { - Set(input, true); + SetX(input, true); } - void Set(float input, bool sendCallback) + void SetX(float input, bool sendCallback) { // Clamp the input float newValue = Mathf.Clamp(input, MinValue, MaxValue); @@ -320,14 +317,14 @@ namespace UnityEngine.UI.Extensions } } - private bool MayDrag(PointerEventData eventData) + private bool CanDrag(PointerEventData eventData) { return IsActive() && IsInteractable() && eventData.button == PointerEventData.InputButton.Left; } public override void OnPointerDown(PointerEventData eventData) { - if (!MayDrag(eventData)) + if (!CanDrag(eventData)) return; base.OnPointerDown(eventData); @@ -349,85 +346,12 @@ namespace UnityEngine.UI.Extensions public virtual void OnDrag(PointerEventData eventData) { - if (!MayDrag(eventData)) + if (!CanDrag(eventData)) return; UpdateDrag(eventData, eventData.pressEventCamera); } - //public override void OnMove(AxisEventData eventData) - //{ - // if (!IsActive() || !IsInteractable()) - // { - // base.OnMove(eventData); - // return; - // } - - // switch (eventData.moveDir) - // { - // case MoveDirection.Left: - // if (axis == Axis.Horizontal && FindSelectableOnLeft() == null) { - // Set(reverseValue ? value + stepSize : value - stepSize); - // SetY (reverseValue ? valueY + stepSize : valueY - stepSize); - // } - // else - // base.OnMove(eventData); - // break; - // case MoveDirection.Right: - // if (axis == Axis.Horizontal && FindSelectableOnRight() == null) { - // Set(reverseValue ? value - stepSize : value + stepSize); - // SetY(reverseValue ? valueY - stepSize : valueY + stepSize); - // } - // else - // base.OnMove(eventData); - // break; - // case MoveDirection.Up: - // if (axis == Axis.Vertical && FindSelectableOnUp() == null) { - // Set(reverseValue ? value - stepSize : value + stepSize); - // SetY(reverseValue ? valueY - stepSize : valueY + stepSize); - // } - // else - // base.OnMove(eventData); - // break; - // case MoveDirection.Down: - // if (axis == Axis.Vertical && FindSelectableOnDown() == null) { - // Set(reverseValue ? value + stepSize : value - stepSize); - // SetY(reverseValue ? valueY + stepSize : valueY - stepSize); - // } - // else - // base.OnMove(eventData); - // break; - // } - //} - - //public override Selectable FindSelectableOnLeft() - //{ - // if (navigation.mode == Navigation.Mode.Automatic && axis == Axis.Horizontal) - // return null; - // return base.FindSelectableOnLeft(); - //} - - //public override Selectable FindSelectableOnRight() - //{ - // if (navigation.mode == Navigation.Mode.Automatic && axis == Axis.Horizontal) - // return null; - // return base.FindSelectableOnRight(); - //} - - //public override Selectable FindSelectableOnUp() - //{ - // if (navigation.mode == Navigation.Mode.Automatic && axis == Axis.Vertical) - // return null; - // return base.FindSelectableOnUp(); - //} - - //public override Selectable FindSelectableOnDown() - //{ - // if (navigation.mode == Navigation.Mode.Automatic && axis == Axis.Vertical) - // return null; - // return base.FindSelectableOnDown(); - //} - public virtual void OnInitializePotentialDrag(PointerEventData eventData) { eventData.useDragThreshold = false; diff --git a/Scripts/Layout/HorizontalScrollSnap.cs b/Scripts/Layout/HorizontalScrollSnap.cs index ae58ffd..2be79a9 100644 --- a/Scripts/Layout/HorizontalScrollSnap.cs +++ b/Scripts/Layout/HorizontalScrollSnap.cs @@ -14,10 +14,11 @@ namespace UnityEngine.UI.Extensions void Start() { isVertical = false; + childAnchorPoint = new Vector2(0, 0.5f); DistributePages(); if(MaskArea) CalculateVisible(); _lerp = false; - _currentPage = StartingScreen - 1; + _currentPage = StartingScreen; SetScrollContainerPosition(); } @@ -51,10 +52,10 @@ namespace UnityEngine.UI.Extensions } } - //used for changing between screen resolutions private void DistributePages() { _screens = _screensContainer.childCount; + _scroll_rect.horizontalNormalizedPosition = 0; int _offset = 0; float _dimension = 0; @@ -69,9 +70,7 @@ namespace UnityEngine.UI.Extensions currentXPosition = _offset + (int)(i * pageStepValue); child.sizeDelta = new Vector2(panelDimensions.width, panelDimensions.height); child.anchoredPosition = new Vector2(currentXPosition, 0f); - child.anchorMin = new Vector2(0f, child.anchorMin.y); - child.anchorMax = new Vector2(0f, child.anchorMax.y); - child.pivot = new Vector2(0f, child.pivot.y); + child.anchorMin = child.anchorMax = child.pivot = childAnchorPoint; } _dimension = currentXPosition + _offset * -1; @@ -129,6 +128,16 @@ namespace UnityEngine.UI.Extensions _scroll_rect.horizontalNormalizedPosition = (float)(_currentPage) / (_screens - 1); } + /// + /// used for changing / updating between screen resolutions + /// + public void UpdateLayout() + { + _lerp = false; + DistributePages(); + SetScrollContainerPosition(); + } + #region Interfaces /// /// Release screen to swipe diff --git a/Scripts/Layout/ScrollSnapBase.cs b/Scripts/Layout/ScrollSnapBase.cs index 58ffd14..466dfa6 100644 --- a/Scripts/Layout/ScrollSnapBase.cs +++ b/Scripts/Layout/ScrollSnapBase.cs @@ -14,6 +14,7 @@ namespace UnityEngine.UI.Extensions internal float _scrollStartPosition; internal float _childSize; private float _childPos; + internal Vector2 childAnchorPoint; internal ScrollRect _scroll_rect; internal Vector3 _lerp_target; internal bool _lerp; @@ -22,7 +23,8 @@ namespace UnityEngine.UI.Extensions [Tooltip("The currently active page")] internal int _currentPage; internal int _previousPage; - + internal int HalfNoVisibleItems; + [Serializable] public class SelectionChangeStartEvent : UnityEvent { } [Serializable] @@ -30,38 +32,42 @@ namespace UnityEngine.UI.Extensions [Serializable] public class SelectionChangeEndEvent : UnityEvent { } - [Tooltip("The visible bounds area, controls which items are visible/enabled. *Note Should use a RectMask. (optional)")] - public RectTransform MaskArea; - [Tooltip("Pixel size to buffer arround Mask Area. (optional)")] - public float MaskBuffer = 1; - public int HalfNoVisibleItems; - - [Tooltip("The gameobject that contains toggles which suggest pagination. (optional)")] - public GameObject Pagination; - - [Tooltip("Button to go to the next page. (optional)")] - public GameObject NextButton; - [Tooltip("Button to go to the previous page. (optional)")] - public GameObject PrevButton; - [Tooltip("Transition speed between pages. (optional)")] - public float transitionSpeed = 7.5f; - - [Tooltip("Fast Swipe makes swiping page next / previous (optional)")] - public Boolean UseFastSwipe = false; - [Tooltip("How far swipe has to travel to initiate a page change (optional)")] - public int FastSwipeThreshold = 100; - [Tooltip("Speed at which the ScrollRect will keep scrolling before slowing down and stopping (optional)")] - public int SwipeVelocityThreshold = 200; - - [Tooltip("The screen / page to start the control on")] + [Tooltip("The screen / page to start the control on\n*Note, this is a 0 indexed array")] [SerializeField] - public int StartingScreen = 1; + public int StartingScreen = 0; [Tooltip("The distance between two pages based on page height, by default pages are next to each other")] [SerializeField] [Range(1, 8)] public float PageStep = 1; + [Tooltip("The gameobject that contains toggles which suggest pagination. (optional)")] + public GameObject Pagination; + + [Tooltip("Button to go to the next page. (optional)")] + public GameObject NextButton; + + [Tooltip("Button to go to the previous page. (optional)")] + public GameObject PrevButton; + + [Tooltip("Transition speed between pages. (optional)")] + public float transitionSpeed = 7.5f; + + [Tooltip("Fast Swipe makes swiping page next / previous (optional)")] + public Boolean UseFastSwipe = false; + + [Tooltip("How far swipe has to travel to initiate a page change (optional)")] + public int FastSwipeThreshold = 100; + + [Tooltip("Speed at which the ScrollRect will keep scrolling before slowing down and stopping (optional)")] + public int SwipeVelocityThreshold = 200; + + [Tooltip("The visible bounds area, controls which items are visible/enabled. *Note Should use a RectMask. (optional)")] + public RectTransform MaskArea; + + [Tooltip("Pixel size to buffer arround Mask Area. (optional)")] + public float MaskBuffer = 1; + public int CurrentPage { get @@ -80,6 +86,9 @@ namespace UnityEngine.UI.Extensions } } + [Tooltip("Scroll Snap children. (optional)\nEither place objects in the scene as children OR\nPrefabs in this array, NOT BOTH")] + public GameObject[] ChildObjects; + [SerializeField] private SelectionChangeStartEvent m_OnSelectionChangeStartEvent = new SelectionChangeStartEvent(); public SelectionChangeStartEvent OnSelectionChangeStartEvent { get { return m_OnSelectionChangeStartEvent; } set { m_OnSelectionChangeStartEvent = value; } } @@ -92,7 +101,6 @@ namespace UnityEngine.UI.Extensions private SelectionChangeEndEvent m_OnSelectionChangeEndEvent = new SelectionChangeEndEvent(); public SelectionChangeEndEvent OnSelectionChangeEndEvent { get { return m_OnSelectionChangeEndEvent; } set { m_OnSelectionChangeEndEvent = value; } } - public GameObject[] ChildObjects; // Use this for initialization void Awake() @@ -104,6 +112,11 @@ namespace UnityEngine.UI.Extensions Debug.LogWarning("Warning, using scrollbars with the Scroll Snap controls is not advised as it causes unpredictable results"); } + if (StartingScreen < 0) + { + StartingScreen = 0; + } + _screensContainer = _scroll_rect.content; if (ChildObjects != null && ChildObjects.Length > 0) { @@ -168,6 +181,9 @@ namespace UnityEngine.UI.Extensions internal void UpdateVisible() { + //If there are no objects in the scene, exit + if (ChildObjects == null || ChildObjects.Length < 1 || _screensContainer.childCount < 1) return; + int BottomItem = _currentPage - HalfNoVisibleItems < 0 ? 0 : HalfNoVisibleItems; int TopItem = _screensContainer.childCount - _currentPage < HalfNoVisibleItems ? _screensContainer.childCount - _currentPage : HalfNoVisibleItems; @@ -267,14 +283,21 @@ namespace UnityEngine.UI.Extensions private void OnValidate() { - var childCount = ChildObjects == null ? _screensContainer.childCount : ChildObjects.Length; - if (StartingScreen > childCount - 1) + if (_screensContainer || ChildObjects != null) { - StartingScreen = childCount - 1; + var childCount = ChildObjects == null ? _screensContainer.childCount : ChildObjects.Length; + if (StartingScreen > childCount - 1) + { + StartingScreen = childCount - 1; + } + if (StartingScreen < 0) + { + StartingScreen = 0; + } } - if (StartingScreen < 0) + if (MaskBuffer <= 0) { - StartingScreen = 0; + MaskBuffer = 1; } } diff --git a/Scripts/Layout/VerticalScrollSnap.cs b/Scripts/Layout/VerticalScrollSnap.cs index 6f770e4..dcef88c 100644 --- a/Scripts/Layout/VerticalScrollSnap.cs +++ b/Scripts/Layout/VerticalScrollSnap.cs @@ -14,10 +14,11 @@ namespace UnityEngine.UI.Extensions void Start() { isVertical = true; + childAnchorPoint = new Vector2(0.5f,0); DistributePages(); if(MaskArea) CalculateVisible(); _lerp = false; - _currentPage = StartingScreen - 1; + _currentPage = StartingScreen; SetScrollContainerPosition(); } @@ -52,10 +53,10 @@ namespace UnityEngine.UI.Extensions } - //used for changing between screen resolutions public void DistributePages() { _screens = _screensContainer.childCount; + _scroll_rect.verticalNormalizedPosition = 0; float _offset = 0; float _dimension = 0; @@ -69,9 +70,7 @@ namespace UnityEngine.UI.Extensions currentYPosition = _offset + i * pageStepValue; child.sizeDelta = new Vector2(panelDimensions.width, panelDimensions.height); child.anchoredPosition = new Vector2(0f, currentYPosition); - child.anchorMin = new Vector2(child.anchorMin.x, 0f); - child.anchorMax = new Vector2(child.anchorMax.x, 0f); - child.pivot = new Vector2(child.pivot.x, 0f); + child.anchorMin = child.anchorMax = child.pivot = childAnchorPoint; } _dimension = currentYPosition + _offset * -1; @@ -130,6 +129,15 @@ namespace UnityEngine.UI.Extensions _scroll_rect.verticalNormalizedPosition = (float)(_currentPage) / (_screens - 1); } + /// + /// used for changing / updating between screen resolutions + /// + public void UpdateLayout() + { + _lerp = false; + DistributePages(); + SetScrollContainerPosition(); + } #region Interfaces /// /// Release screen to swipe diff --git a/Scripts/Utilities/ExtensionsToggle.cs b/Scripts/Utilities/ExtensionsToggle.cs index 2f554d9..7c52d50 100644 --- a/Scripts/Utilities/ExtensionsToggle.cs +++ b/Scripts/Utilities/ExtensionsToggle.cs @@ -130,7 +130,7 @@ namespace UnityEngine.UI protected override void OnDidApplyAnimationProperties() { // Check if isOn has been changed by the animation. - // Unfortunately there is no way to check if we don�t have a graphic. + // Unfortunately there is no way to check if we don't have a graphic. if (graphic != null) { bool oldValue = !Mathf.Approximately(graphic.canvasRenderer.GetColor().a, 0); @@ -194,7 +194,7 @@ namespace UnityEngine.UI m_IsOn = value; if (m_Group != null && IsActive()) { - if (m_IsOn || (!m_Group.AnyTogglesOn() && !m_Group.allowSwitchOff)) + if (m_IsOn || (!m_Group.AnyTogglesOn() && !m_Group.AllowSwitchOff)) { m_IsOn = true; m_Group.NotifyToggleOn(this); diff --git a/Scripts/Utilities/ExtensionsToggleGroup.cs b/Scripts/Utilities/ExtensionsToggleGroup.cs index 17adb08..6feb5b6 100644 --- a/Scripts/Utilities/ExtensionsToggleGroup.cs +++ b/Scripts/Utilities/ExtensionsToggleGroup.cs @@ -12,7 +12,7 @@ namespace UnityEngine.UI { [SerializeField] private bool m_AllowSwitchOff = false; - public bool allowSwitchOff { get { return m_AllowSwitchOff; } set { m_AllowSwitchOff = value; } } + public bool AllowSwitchOff { get { return m_AllowSwitchOff; } set { m_AllowSwitchOff = value; } } private List m_Toggles = new List();