LoginSignup
4
2

More than 3 years have passed since last update.

Unity : ScrollRectを拡張したい!

Last updated at Posted at 2017-09-06

ScrollRectを拡張したい!

 UnityのScrollViewは便利なんですが、無限ループとか指定したアイテムの位置に移動させるとかできないとか。
そういったなんか製品だとできるようなUIの動きを実装していきたいと思います。

実装とかやってられっか!って人は AssetStoreに Picker for uGui(有料) をつかうといいかも

Picker for uGUI
https://www.assetstore.unity3d.com/jp/#!/content/31412

無限ループさせるのは無料の模様

NGUI Infinite Pickers
https://www.assetstore.unity3d.com/jp/#!/content/10024

無限ループ

まず最初にスクロールを無限ループさせることを考えます。

一番単純なモデルで、表示されているのは単一のオブジェクトのみ、それがスクロールさせた際に無限に続くように作ってみます。(広告バナーのスライドのイメージ)

ループする広告バナーを作る

下準備として、 ScrollView以下のContentには GridLayoutGroupを付けておきましょう。
単一サイズのものを複数並べる際に有効です。
unity=gridlayoutgroup.PNG

初期の表示はこんな感じ。
Initial.PNG

真ん中に広告とかがあって、左右に前後に動かすボタンがあります。

実際にループさせてみます
ド.gif

ループできてますね!(アニメーションとかがないのでアレですが。。。)

コードはこんな感じ

simpleLoopScrollRect.cs
using UnityEngine.UI;

public class CustomScrollRect : ScrollRect
{
    private int contentCount;

    protected override void Awake()
    {
        base.Awake();

        contentCount = content.childCount;
    }

    public void Next()
    {
        content.GetChild(0).SetAsLastSibling();
    }

    public void Prev()
    {
        content.GetChild(contentCount - 1).SetAsFirstSibling();
    }
}

ひとまず今回はここまで~
次回は、変更時をアニメーション?させたく思います

4
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
2