diff --git a/Scripts/Controls/UI_Knob.cs b/Scripts/Controls/UI_Knob.cs index 0c14864..e7582ae 100644 --- a/Scripts/Controls/UI_Knob.cs +++ b/Scripts/Controls/UI_Knob.cs @@ -51,15 +51,11 @@ namespace UnityEngine.UI.Extensions private Vector2 _currentVector; private Quaternion _initRotation; private bool _canDrag = false; - [SerializeField] - private bool experimental = false; - - private RectTransform m_HandleRect; - + private bool _screenSpaceOverlay; protected override void Awake() { - m_HandleRect = GetComponent(); + _screenSpaceOverlay = GetComponentInParent().rootCanvas.renderMode == RenderMode.ScreenSpaceOverlay; } public override void OnPointerUp(PointerEventData eventData) @@ -83,13 +79,13 @@ namespace UnityEngine.UI.Extensions base.OnPointerDown(eventData); _initRotation = transform.rotation; - if (experimental) + if (_screenSpaceOverlay) { - RectTransformUtility.ScreenPointToLocalPointInRectangle(m_HandleRect, eventData.position, eventData.pressEventCamera, out _currentVector); + _currentVector = eventData.position - (Vector2)transform.position; } else { - _currentVector = eventData.position - (Vector2)transform.position; + _currentVector = eventData.position - (Vector2)Camera.main.WorldToScreenPoint(transform.position); } _initAngle = Mathf.Atan2(_currentVector.y, _currentVector.x) * Mathf.Rad2Deg; } @@ -102,14 +98,14 @@ namespace UnityEngine.UI.Extensions return; } - if (experimental) - { - RectTransformUtility.ScreenPointToLocalPointInRectangle(m_HandleRect, eventData.position, eventData.pressEventCamera, out _currentVector); - } - else - { - _currentVector = eventData.position - (Vector2)transform.position; - } + if (_screenSpaceOverlay) + { + _currentVector = eventData.position - (Vector2)transform.position; + } + else + { + _currentVector = eventData.position - (Vector2)Camera.main.WorldToScreenPoint(transform.position); + } _currentAngle = Mathf.Atan2(_currentVector.y, _currentVector.x) * Mathf.Rad2Deg; Quaternion addRotation = Quaternion.AngleAxis(_currentAngle - _initAngle, this.transform.forward); diff --git a/Scripts/Layout/ScrollSnapBase.cs b/Scripts/Layout/ScrollSnapBase.cs index 25c1096..6829fa7 100644 --- a/Scripts/Layout/ScrollSnapBase.cs +++ b/Scripts/Layout/ScrollSnapBase.cs @@ -94,11 +94,19 @@ namespace UnityEngine.UI.Extensions if (_isInfinate) { //Work out which infinite window we are in - _infiniteWindow = value / _screensContainer.childCount; + float infWindow = (float)value / (float)_screensContainer.childCount; + + if (infWindow < 0) + { + _infiniteWindow = (int)(Math.Floor(infWindow)); + } + else + { + _infiniteWindow = value / _screensContainer.childCount; + } //Invert the value if negative and differentiate from Window 0 - _infiniteWindow = value < 0 ? (-_infiniteWindow) + 1 : _infiniteWindow; - //Multiplying values by zero doesn't help, so start at Window 1 - //_infiniteWindow += 1; + _infiniteWindow = value < 0 ? (-_infiniteWindow) : _infiniteWindow; + //Calculate the page within the child count range value = value % _screensContainer.childCount; if (value < 0) @@ -369,6 +377,7 @@ namespace UnityEngine.UI.Extensions if (_isVertical) { _infiniteOffset = _screensContainer.localPosition.y < 0 ? -_screensContainer.sizeDelta.y * _infiniteWindow : _screensContainer.sizeDelta.y * _infiniteWindow; + _infiniteOffset = _infiniteOffset == 0 ? 0 : _infiniteOffset < 0 ? _infiniteOffset - _childSize * _infiniteWindow : _infiniteOffset + _childSize * _infiniteWindow; target.y = _childPos + _scrollStartPosition + _infiniteOffset; } else @@ -449,19 +458,22 @@ namespace UnityEngine.UI.Extensions { Debug.LogError("ScrollRect has to be unidirectional, only use either Horizontal or Vertical on the ScrollRect, NOT both."); } - - var children = gameObject.GetComponent().content.childCount; - if (children != 0 || ChildObjects != null) + var ScrollRectContent = gameObject.GetComponent().content; + if (ScrollRectContent != null) { - var childCount = ChildObjects == null || ChildObjects.Length == 0 ? children : ChildObjects.Length; - if (StartingScreen > childCount - 1) + var children = ScrollRectContent.childCount; + if (children != 0 || ChildObjects != null) { - StartingScreen = childCount - 1; - } + var childCount = ChildObjects == null || ChildObjects.Length == 0 ? children : ChildObjects.Length; + if (StartingScreen > childCount - 1) + { + StartingScreen = childCount - 1; + } - if (StartingScreen < 0) - { - StartingScreen = 0; + if (StartingScreen < 0) + { + StartingScreen = 0; + } } }