NGUI の ScrollView 内のアイテムをドラッグアンドドロップで並び替える
はじめに
- Unity + NGUI 環境で ScrollView 内のアイテムをドラッグアンドドロップで並び替えられるように
構成
ゲームオブジェクトの親子関係とアタッチするコンポーネント
- Scroll View (UISCrollView, UIDragDropRoot)
- Grid (UIGrid)
- Item (BoxCollider, UIDragDropItem, UIDragScrollView)
- Item
- Item
- Grid (UIGrid)
Scroll View オブジェクトの設定
- UIPanel の Size を任意で設定
- UIScrollView の Movement を任意で設定(今回は縦スクロールにするので Vertical に)
Grid の設定
- UIGrid の Arrangment を(今回は縦スクロールなので)Vertical に
- 同じく Cell Width と Cell Height を任意で設定
- Sorting を(縦スクロールの場合は)Vertical に
Item の設定
- UIDragDropItem の Restriction を Horizontal に
並び替え終了時にリストのアイテムを取得する
- UIDragDropItem を継承し OnDragDropEnd を override することで、ドラッグアンドドロップ終了時の処理を記述できる。
- 下記のスクリプトを UIDragDropItem の代わりに Item にアタッチすると、ドラッグアンドドロップ終了時にリストの並び順通りにオブジェクトを取得して名前を表示する。
using UnityEngine; using System.Collections; public class MyDragDropItem : UIDragDropItem { protected override void OnDragDropEnd () { var list = mGrid.GetChildList (); list.ForEach (child => Debug.Log (child.name)); base.OnDragDropEnd (); } }