A simple tool to make unity scrollview to be snapped. It includes some default resources and provides various options as well as writing custom transition effects.
- Go Window - Package Manager - Add package from git URL...
- Paste this url : https://github.com/emrecelik95/LightScrollSnap.git
- Install via manifest.json file
- Navigate to Packages/manifest.json file.
- Add this into
"dependencies": {
block
"com.emrecelik95.lightscrollsnap": "https://github.com/emrecelik95/LightScrollSnap.git"
If you don't want to use git, you can download and unzip the repo and move it to Packages folder.
- From Sample
- Import the sample from Package Manager -> LightScrollSnap -> Basic Sample.
- Change the items in Content object(Canvas/ScrollSnap/Viewport/Content).
- Make any configuration using ScrollSnap component(Canvas/ScrollSnap).
- From Scratch
- Create a canvas with screen based Canvas Scaler.
- Create horizontal ScrollView.
- Add Horizontal Layout Group and Content Size Fitter to Content object;
- Bring the first item to center using Padding in Horizontal Layout Group.
- Add ScrollSnap component to ScrollRect object.
- Drag ScrollBarHorizontal to the Scrollbar field on ScrollSnap component.
- Drag any ScrollSnap effects to the Effects field.
- Transition efffects are based on Scriptable Objects and inherited from BaseScrollSnapEffect.
- There are two default effect on this package, to use them, Right Click under Assets, Create -> ScrollSnapEffect.
- To write custom transition effect, just inherit BaseScrollSnapEffect.
- OnItemSelected , OnItemDeselected, OnItemClicked.
- Register and unregister to events;
scrollSnap.OnItemSelected.AddListener(OnSelectedItemChanged);
scrollSnap.OnItemSelected.RemoveListener(OnSelectedItemChanged);
private void OnSelectedItemChanged(RectTransform go, int index){}
- ScrollTo(float ratio), SmoothScrollTo(float ratio, float duration) // ratio is between 0 and 1
- ScrollToItem(int itemIndex), SmoothScrollToItem(int itemIndex, float duration)
scrollSnap.SmoothScrollToItem(5, 0.5f);
- AddItemClickListener(int itemIndex, Action clickListener)
- RemoveItemClickListener(int itemIndex, Action clickListener
scrollSnap.AddItemClickListener(0, () => { Debug.Log("First item clicked!"); });