diff --git a/Editor/UIExtensionsMenuOptions.cs b/Editor/UIExtensionsMenuOptions.cs index 863f60d..ec94484 100644 --- a/Editor/UIExtensionsMenuOptions.cs +++ b/Editor/UIExtensionsMenuOptions.cs @@ -379,7 +379,7 @@ namespace UnityEditor.UI ScrollSnap scrollSnap = scrollSnapRoot.AddComponent(); scrollSnap.direction = direction; - scrollSnap.itemsVisibleAtOnce = itemVisible; + scrollSnap.ItemsVisibleAtOnce = itemVisible; //Setup Content container RectTransform rectTransformContent = itemList.GetComponent(); diff --git a/Examples/HSS-VSS-ScrollSnap/ScrollSnapManagedTests.unity b/Examples/HSS-VSS-ScrollSnap/ScrollSnapManagedTests.unity index e2fdc37..7c09200 100644 --- a/Examples/HSS-VSS-ScrollSnap/ScrollSnapManagedTests.unity +++ b/Examples/HSS-VSS-ScrollSnap/ScrollSnapManagedTests.unity @@ -290,6 +290,7 @@ RectTransform: m_LocalScale: {x: 1, y: 1, z: 1} m_Children: - {fileID: 1826600021} + - {fileID: 472879517} m_Father: {fileID: 448991457} m_RootOrder: 0 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} @@ -392,7 +393,7 @@ MonoBehaviour: m_DecelerationRate: 0.135 m_ScrollSensitivity: 1 m_Viewport: {fileID: 0} - m_HorizontalScrollbar: {fileID: 0} + m_HorizontalScrollbar: {fileID: 472879516} m_VerticalScrollbar: {fileID: 0} m_HorizontalScrollbarVisibility: 0 m_VerticalScrollbarVisibility: 0 @@ -899,6 +900,122 @@ Canvas: m_SortingLayerID: 0 m_SortingOrder: 0 m_TargetDisplay: 0 +--- !u!1 &472879515 +GameObject: + m_ObjectHideFlags: 0 + m_PrefabParentObject: {fileID: 0} + m_PrefabInternal: {fileID: 0} + serializedVersion: 5 + m_Component: + - component: {fileID: 472879517} + - component: {fileID: 472879519} + - component: {fileID: 472879518} + - component: {fileID: 472879516} + m_Layer: 5 + m_Name: Scrollbar + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!114 &472879516 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_PrefabParentObject: {fileID: 0} + m_PrefabInternal: {fileID: 0} + m_GameObject: {fileID: 472879515} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: -2061169968, guid: f70555f144d8491a825f0804e09c671c, type: 3} + m_Name: + m_EditorClassIdentifier: + m_Navigation: + m_Mode: 3 + m_SelectOnUp: {fileID: 0} + m_SelectOnDown: {fileID: 0} + m_SelectOnLeft: {fileID: 0} + m_SelectOnRight: {fileID: 0} + m_Transition: 1 + m_Colors: + m_NormalColor: {r: 1, g: 1, b: 1, a: 1} + m_HighlightedColor: {r: 0.9607843, g: 0.9607843, b: 0.9607843, a: 1} + m_PressedColor: {r: 0.78431374, g: 0.78431374, b: 0.78431374, a: 1} + m_DisabledColor: {r: 0.78431374, g: 0.78431374, b: 0.78431374, a: 0.5019608} + m_ColorMultiplier: 1 + m_FadeDuration: 0.1 + m_SpriteState: + m_HighlightedSprite: {fileID: 0} + m_PressedSprite: {fileID: 0} + m_DisabledSprite: {fileID: 0} + m_AnimationTriggers: + m_NormalTrigger: Normal + m_HighlightedTrigger: Highlighted + m_PressedTrigger: Pressed + m_DisabledTrigger: Disabled + m_Interactable: 1 + m_TargetGraphic: {fileID: 557949556} + m_HandleRect: {fileID: 557949554} + m_Direction: 0 + m_Value: 0 + m_Size: 1 + m_NumberOfSteps: 0 + m_OnValueChanged: + m_PersistentCalls: + m_Calls: [] + m_TypeName: UnityEngine.UI.Scrollbar+ScrollEvent, UnityEngine.UI, Version=1.0.0.0, + Culture=neutral, PublicKeyToken=null +--- !u!224 &472879517 +RectTransform: + m_ObjectHideFlags: 0 + m_PrefabParentObject: {fileID: 0} + m_PrefabInternal: {fileID: 0} + m_GameObject: {fileID: 472879515} + m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} + m_LocalPosition: {x: 0, y: 0, z: 0} + m_LocalScale: {x: 1, y: 1, z: 1} + m_Children: + - {fileID: 1513549040} + m_Father: {fileID: 67156822} + m_RootOrder: 1 + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} + m_AnchorMin: {x: 0.5, y: 0.5} + m_AnchorMax: {x: 0.5, y: 0.5} + m_AnchoredPosition: {x: -2.6999998, y: -73.5} + m_SizeDelta: {x: 384.7, y: 20} + m_Pivot: {x: 0.5, y: 0.5} +--- !u!114 &472879518 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_PrefabParentObject: {fileID: 0} + m_PrefabInternal: {fileID: 0} + m_GameObject: {fileID: 472879515} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: -765806418, guid: f70555f144d8491a825f0804e09c671c, type: 3} + m_Name: + m_EditorClassIdentifier: + m_Material: {fileID: 0} + m_Color: {r: 1, g: 1, b: 1, a: 1} + m_RaycastTarget: 1 + m_OnCullStateChanged: + m_PersistentCalls: + m_Calls: [] + m_TypeName: UnityEngine.UI.MaskableGraphic+CullStateChangedEvent, UnityEngine.UI, + Version=1.0.0.0, Culture=neutral, PublicKeyToken=null + m_Sprite: {fileID: 10907, guid: 0000000000000000f000000000000000, type: 0} + m_Type: 1 + m_PreserveAspect: 0 + m_FillCenter: 1 + m_FillMethod: 4 + m_FillAmount: 1 + m_FillClockwise: 1 + m_FillOrigin: 0 +--- !u!222 &472879519 +CanvasRenderer: + m_ObjectHideFlags: 0 + m_PrefabParentObject: {fileID: 0} + m_PrefabInternal: {fileID: 0} + m_GameObject: {fileID: 472879515} --- !u!1 &475765631 GameObject: m_ObjectHideFlags: 0 @@ -1116,6 +1233,74 @@ CanvasRenderer: m_PrefabParentObject: {fileID: 0} m_PrefabInternal: {fileID: 0} m_GameObject: {fileID: 546976065} +--- !u!1 &557949553 +GameObject: + m_ObjectHideFlags: 0 + m_PrefabParentObject: {fileID: 0} + m_PrefabInternal: {fileID: 0} + serializedVersion: 5 + m_Component: + - component: {fileID: 557949554} + - component: {fileID: 557949555} + - component: {fileID: 557949556} + m_Layer: 5 + m_Name: Handle + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!224 &557949554 +RectTransform: + m_ObjectHideFlags: 0 + m_PrefabParentObject: {fileID: 0} + m_PrefabInternal: {fileID: 0} + m_GameObject: {fileID: 557949553} + m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} + m_LocalPosition: {x: 0, y: 0, z: 0} + m_LocalScale: {x: 1, y: 1, z: 1} + m_Children: [] + m_Father: {fileID: 1513549040} + m_RootOrder: 0 + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} + m_AnchorMin: {x: 0, y: 0} + m_AnchorMax: {x: 0, y: 0} + m_AnchoredPosition: {x: 0, y: 0} + m_SizeDelta: {x: 20, y: 20} + m_Pivot: {x: 0.5, y: 0.5} +--- !u!222 &557949555 +CanvasRenderer: + m_ObjectHideFlags: 0 + m_PrefabParentObject: {fileID: 0} + m_PrefabInternal: {fileID: 0} + m_GameObject: {fileID: 557949553} +--- !u!114 &557949556 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_PrefabParentObject: {fileID: 0} + m_PrefabInternal: {fileID: 0} + m_GameObject: {fileID: 557949553} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: -765806418, guid: f70555f144d8491a825f0804e09c671c, type: 3} + m_Name: + m_EditorClassIdentifier: + m_Material: {fileID: 0} + m_Color: {r: 1, g: 1, b: 1, a: 1} + m_RaycastTarget: 1 + m_OnCullStateChanged: + m_PersistentCalls: + m_Calls: [] + m_TypeName: UnityEngine.UI.MaskableGraphic+CullStateChangedEvent, UnityEngine.UI, + Version=1.0.0.0, Culture=neutral, PublicKeyToken=null + m_Sprite: {fileID: 10905, guid: 0000000000000000f000000000000000, type: 0} + m_Type: 1 + m_PreserveAspect: 0 + m_FillCenter: 1 + m_FillMethod: 4 + m_FillAmount: 1 + m_FillClockwise: 1 + m_FillOrigin: 0 --- !u!1 &571794653 GameObject: m_ObjectHideFlags: 0 @@ -1707,7 +1892,7 @@ RectTransform: m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} m_AnchorMin: {x: 1, y: 1} m_AnchorMax: {x: 1, y: 1} - m_AnchoredPosition: {x: -122.5, y: -176.5} + m_AnchoredPosition: {x: -113.9, y: -183.2} m_SizeDelta: {x: 150, y: 30} m_Pivot: {x: 0.5, y: 0.5} --- !u!114 &803284191 @@ -2525,7 +2710,7 @@ RectTransform: m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} m_AnchorMin: {x: 1, y: 1} m_AnchorMax: {x: 1, y: 1} - m_AnchoredPosition: {x: -335.5, y: -176.5} + m_AnchoredPosition: {x: -326.9, y: -183.2} m_SizeDelta: {x: 150, y: 30} m_Pivot: {x: 0.5, y: 0.5} --- !u!114 &1112321046 @@ -3612,6 +3797,40 @@ CanvasRenderer: m_PrefabParentObject: {fileID: 0} m_PrefabInternal: {fileID: 0} m_GameObject: {fileID: 1496412125} +--- !u!1 &1513549039 +GameObject: + m_ObjectHideFlags: 0 + m_PrefabParentObject: {fileID: 0} + m_PrefabInternal: {fileID: 0} + serializedVersion: 5 + m_Component: + - component: {fileID: 1513549040} + m_Layer: 5 + m_Name: Sliding Area + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!224 &1513549040 +RectTransform: + m_ObjectHideFlags: 0 + m_PrefabParentObject: {fileID: 0} + m_PrefabInternal: {fileID: 0} + m_GameObject: {fileID: 1513549039} + m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} + m_LocalPosition: {x: 0, y: 0, z: 0} + m_LocalScale: {x: 1, y: 1, z: 1} + m_Children: + - {fileID: 557949554} + m_Father: {fileID: 472879517} + m_RootOrder: 0 + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} + m_AnchorMin: {x: 0, y: 0} + m_AnchorMax: {x: 1, y: 1} + m_AnchoredPosition: {x: 0, y: 0} + m_SizeDelta: {x: -20, y: -20} + m_Pivot: {x: 0.5, y: 0.5} --- !u!1 &1520368422 GameObject: m_ObjectHideFlags: 0 diff --git a/Scripts/Layout/IScrollSnap.cs b/Scripts/Layout/IScrollSnap.cs new file mode 100644 index 0000000..50654c6 --- /dev/null +++ b/Scripts/Layout/IScrollSnap.cs @@ -0,0 +1,14 @@ +/// Credit SimonDarksideJ +/// Required for scrollbar support to work across ALL scroll snaps + + +namespace UnityEngine.UI.Extensions +{ + internal interface IScrollSnap + { + void ChangePage(int page); + void SetLerp(bool value); + int CurrentPage(); + void StartScreenChange(); + } +} diff --git a/Scripts/Layout/IScrollSnap.cs.meta b/Scripts/Layout/IScrollSnap.cs.meta new file mode 100644 index 0000000..d17eee4 --- /dev/null +++ b/Scripts/Layout/IScrollSnap.cs.meta @@ -0,0 +1,12 @@ +fileFormatVersion: 2 +guid: 08ba470d8c8dc384e9d5828136742452 +timeCreated: 1498921617 +licenseType: Free +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Scripts/Layout/ScrollSnap.cs b/Scripts/Layout/ScrollSnap.cs index 0bb3e9f..e064bf8 100644 --- a/Scripts/Layout/ScrollSnap.cs +++ b/Scripts/Layout/ScrollSnap.cs @@ -17,7 +17,7 @@ namespace UnityEngine.UI.Extensions [ExecuteInEditMode] [RequireComponent(typeof(ScrollRect))] [AddComponentMenu("UI/Extensions/Scroll Snap")] - public class ScrollSnap : MonoBehaviour, IBeginDragHandler, IEndDragHandler, IDragHandler + public class ScrollSnap : MonoBehaviour, IBeginDragHandler, IEndDragHandler, IDragHandler, IScrollSnap { // needed because of reversed behaviour of axis Y compared to X // (positions of children lower in children list in horizontal directions grows when in vertical it gets smaller) @@ -27,118 +27,123 @@ namespace UnityEngine.UI.Extensions Vertical } + private ScrollRect _scroll_rect; + + private RectTransform _scrollRectTransform; + + private Transform _listContainerTransform; + + private RectTransform _rectTransform; + + private int _pages; + + private int _startingPage = 0; + + // anchor points to lerp to to see child on certain indexes + private Vector3[] _pageAnchorPositions; + + private Vector3 _lerpTarget; + + private bool _lerp; + + // item list related + private float _listContainerMinPosition; + + private float _listContainerMaxPosition; + + private float _listContainerSize; + + private RectTransform _listContainerRectTransform; + + private Vector2 _listContainerCachedSize; + + private float _itemSize; + + private int _itemsCount = 0; + + // drag related + private bool _startDrag = true; + + private Vector3 _positionOnDragStart = new Vector3(); + + private int _pageOnDragStart; + + private bool _fastSwipeTimer = false; + + private int _fastSwipeCounter = 0; + + private int _fastSwipeTarget = 10; + + [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; + 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() + void Start() { - lerp = false; + _lerp = false; - scrollRect = gameObject.GetComponent(); - scrollRectTransform = gameObject.GetComponent(); - listContainerTransform = scrollRect.content; - listContainerRectTransform = listContainerTransform.GetComponent(); + _scroll_rect = gameObject.GetComponent(); + _scrollRectTransform = gameObject.GetComponent(); + _listContainerTransform = _scroll_rect.content; + _listContainerRectTransform = _listContainerTransform.GetComponent(); - rectTransform = listContainerTransform.gameObject.GetComponent(); + _rectTransform = _listContainerTransform.gameObject.GetComponent(); UpdateListItemsSize(); UpdateListItemPositions(); PageChanged(CurrentPage()); - if (nextButton) + if (NextButton) { - nextButton.GetComponent