日本語ドキュメント(Japanese Documents Available)
Carousel View for Unity uGUI using Fancy Scroll View.
- You can create the Carousel View in a few simple steps.
- High performance because cells that are not needed for display are reused.
- Supports vertical and horizontal scrolling.
- The movement of the carousel and each parameters can be customized in detail.
- Clone this repository.
- Open and play the following scene.
Unity 2019.4 or higher.
The Fancy Carousel View uses the Fancy Scroll View as a low layer implementation.
So you need to install both of them.
- Open the Package Manager from Window > Package Manager
- "+" button > Add package from git URL
- Enter the following to install Fancy Scroll View
- Enter the following to install Fancy Carousel View
Or, open Packages/manifest.json and add the following to the dependencies block.
{
"dependencies": {
"jp.setchi.fancyscrollview": "https://github.com/setchi/FancyScrollView.git#upm",
"com.harumak.fancycarouselview": "https://github.com/Haruma-K/FancyCarouselView.git?path=/Assets/FancyCarouselView"
}
}
If you want to set the target version, specify it like follow.
First, create data for each of the cells that are elements of the carousel view.
In the following example, defines the key to load cell background texture and the string that is displayed in the cell.
public class DemoData
{
public string SpriteResourceKey { get; }
public string Text { get; }
public DemoData(string spriteResourceKey, string text)
{
SpriteResourceKey = spriteResourceKey;
Text = text;
}
}
Next, create the view of the cell.
As the following, inherit from the CarouselCell
class and write the process of updating the view in the Refresh
method.
using FancyCarouselView.Runtime.Scripts;
using UnityEngine;
using UnityEngine.UI;
public class DemoCarouselCell : CarouselCell<DemoData, DemoCarouselCell>
{
[SerializeField] private Image _image = default;
[SerializeField] private Text _text = default;
protected override void Refresh(DemoData data)
{
_image.sprite = Resources.Load<Sprite>(data.SpriteResourceKey);
_text.text = data.Text;
}
}
Attach this script to a GameObject and create a prefab of the cell.
Next, create the carousel view.
Create a class that inherits from the CarouselView
class with the data and the cell class described above as the generic.
using FancyCarouselView.Runtime.Scripts;
public class DemoCarouselView : CarouselView<DemoData, DemoCarouselCell>
{
}
Attach it to a GameObject under Canvas.
Adjust the size of the carousel view with the size of RectTransform, and adjust the size per cell with Cell Size
property of CarouselView
.
Also, assign the prefab created in the previous section to the Cell Prefab
property.
Finally, pass the data using the CarouselView.Setup
method, and the carousel view will be displayed.
using System.Linq;
using UnityEngine;
namespace Demo.Scripts
{
public class Demo : MonoBehaviour
{
[SerializeField] private DemoCarouselView _carouselView = default;
[SerializeField, Range(1, 3)] private int _bannerCount = 3;
private void Start()
{
var items = Enumerable.Range(0, _bannerCount)
.Select(i =>
{
var spriteResourceKey = $"tex_demo_banner_{i:D2}";
var text = $"Demo Banner {i:D2}";
return new DemoData(spriteResourceKey, text);
})
.ToArray();
_carouselView.Setup(items);
}
}
}
The description of each property in the Carousel View inspector is as follows.
Property Name | Description | ||
Cell Container | RectTransform representing the area of the carousel view. Cells outside this area will be hidden and be reused. | ||
Cell Prefab | Prefab of the cell. | ||
Cell Size | Size of the cell. | ||
Cell Spacing | Spacing between cells. | ||
Snap Animation Durataion | Seconds of the snap animation. | ||
Snap Animation Type | Easing type of the snap animation. | ||
Auto Scrolling | If true, scroll automatically at regular intervals. | ||
Interval | Auto scrolling interval in seconds。 | ||
Inverse Direction | If true, reverse the direction of the auto scrolling. | ||
Scroll Direction | Direction of the scrolling. | ||
Loop | If true, loop when reaches the end. | ||
Draggable | Dragable or not. | ||
Progress View | View that represents the progress. See below for details. | ||
Clickable | If true, you can click progress view to scroll to the element at the clicked index. |
Fancy Carousel View supports the progress view representing the carousel progress.
You can use the simple dot progress view by the following steps.
- Instantiate
pfb_default_carousel_progress_view.prefab
in a scene. - Assign 1. to the Progress View property of the Carousel View.
DotCarouselProgressView
derived from CarouselProgressView
is attached to the Prefab above.
If you implement your own class that derived from CarouselProgressView
or ClickableCarouselProgressView
, you can create a progress view that behaves as you wish.
If you want to change only the color or size of the dot while using DotCarouselProgressView
, you can do so by replacing only the Progress Element Prefab in the Inspector of DotCarouselProgressView
.
You can override CarouselCell.OnPositionUpdated
to implement your own cell movement.
The following is an example of a custom cell movements.
protected override void OnPositionUpdated(float position)
{
base.OnPositionUpdated(position);
var trans = transform;
var pos = position * 2.0f - 1.0f;
var absPos = Mathf.Abs(pos);
var cellPosZ = Mathf.Lerp(0.0f, 120.0f, absPos);
trans.localPosition = new Vector3(trans.localPosition.x, trans.localPosition.y, cellPosZ);
trans.rotation = Quaternion.AngleAxis(pos * -20.0f, Vector3.up);
}
When you use the carousel view as the content of the scroll view, the carousel view blocks the dragging of the scroll view according to the Unity specification.
In other words, dragging the carousel view will not scroll the scroll view.
In such a case, attach the Scroll Event Propagator
component to the Carousel View GameObject.
This component will propagate drag events to the parent ScrollRect
properly.
As a result, the scroll view and carousel view will work properly as shown below.
This software is released under the MIT License.
You are free to use it within the scope of the license.
However, the following copyright and license notices are required for use.
And this software is implemented on the assumption that the following software is installed (not included).
See Third Party Notices.md for details on the license of this software.