「Unity/リスト表示/使いまわし」の版間の差分
提供: 初心者エンジニアの簡易メモ
(→リスト作成) |
(→リスト作成) |
||
| 行12: | 行12: | ||
#SamplesをimportするとAssets/Samples/LoopScrollRect/1.1.2-p1以下にDemoが追加される | #SamplesをimportするとAssets/Samples/LoopScrollRect/1.1.2-p1以下にDemoが追加される | ||
| − | == | + | ==縦リスト作成== |
#Unityメインメニュー/GameObject/UI/LoopVerticalScrollRect | #Unityメインメニュー/GameObject/UI/LoopVerticalScrollRect | ||
#Assets/Canvas/LoopVerticalScrollRectが作成されることを確認 | #Assets/Canvas/LoopVerticalScrollRectが作成されることを確認 | ||
| − | # | + | #LoopVerticalScrollRectのInspectorで、AddComponentから、VerticalLayoutGroupを追加し、ControlChildSizeのWIdthとHeightにチェックと、ChildForceExpandのWidthにチェック。 |
| + | ProjectのAssetsの下にPrefabsのdirを作成しておく。 | ||
| + | #LoopVerticalScrollRect/Contentの下に、適当にImageやTextなどのコンテンツを作り、AddComponentから、LayoutElementを追加して、MinHeightにチェックを入れ200、FlexibleWidthにチェックを入れ1を入れ、Prefabsの下にドラッグ。 | ||
| + | #LoopVertialScrollRectに以下スクリプトを設定 | ||
| + | <pre> | ||
| + | using System.Collections.Generic; | ||
| + | using UnityEngine; | ||
| + | using UnityEngine.UI; | ||
| − | ( | + | [RequireComponent(typeof(LoopVerticalScrollRect))] |
| + | [DisallowMultipleComponent] | ||
| + | public class LoopScrollRectInit : MonoBehaviour, LoopScrollPrefabSource, LoopScrollDataSource | ||
| + | { | ||
| + | [SerializeField] private GameObject prefab; | ||
| + | [SerializeField] int totalCount = 10; | ||
| + | Stack<Transform> pool = new Stack<Transform>(); | ||
| + | private void Start() | ||
| + | { | ||
| + | var scrollRect = GetComponent<LoopVerticalScrollRect>(); ; | ||
| + | scrollRect.prefabSource = this; | ||
| + | scrollRect.dataSource = this; | ||
| + | scrollRect.totalCount = totalCount; | ||
| + | scrollRect.RefillCells(); | ||
| + | } | ||
| + | // Objectが新規表示の時 | ||
| + | GameObject LoopScrollPrefabSource.GetObject(int index) | ||
| + | { | ||
| + | if (pool.Count == 0) | ||
| + | { | ||
| + | return Instantiate(prefab); | ||
| + | } | ||
| + | Transform candidate = pool.Pop(); | ||
| + | candidate.gameObject.SetActive(true); | ||
| + | return candidate.gameObject; | ||
| + | } | ||
| + | // Objectが枠外になった時 | ||
| + | void LoopScrollPrefabSource.ReturnObject(Transform trans) | ||
| + | { | ||
| + | trans.gameObject.SetActive(false); | ||
| + | trans.SetParent(transform, false); | ||
| + | pool.Push(trans); | ||
| + | } | ||
| + | // 要素が表示される時 | ||
| + | void LoopScrollDataSource.ProvideData(Transform trans, int index) | ||
| + | { | ||
| + | trans.Find("Text").GetComponent<Text>().text = "文章" + index.ToString(); | ||
| + | } | ||
| + | } | ||
| + | </pre> | ||
| + | ==縦リストにスクロール追加== | ||
| + | #LoopVertialScrollRectの下にUI/Scrollbarを追加。 | ||
| + | #ScrollbarのInspectorのScrollbarのDirection をTopToBottomに変更 | ||
| + | #LoopVertialScrollRectのInspectorのVerticalScrollbarに、上で作ったScrollbarをドラッグ。 | ||
==スクロールが動かないとき== | ==スクロールが動かないとき== | ||
2023年3月9日 (木) 22:07時点における版
LoopScrollRectとは
無料で使える、使い回しができるリスト
インストール
- Unityメインメニュー/Window/PackageManager/左上の+/gitURLから以下を追加
- https://github.com/qiankanglai/LoopScrollRect.git
- Packages/LoopScrollRectが追加されてることを確認
サンプル追加
- Unityメインメニュー/Window/PackageManager/左上の+/gitURLから以下を追加
- https://github.com/qiankanglai/LoopScrollRect.git
- SamplesをimportするとAssets/Samples/LoopScrollRect/1.1.2-p1以下にDemoが追加される
縦リスト作成
- Unityメインメニュー/GameObject/UI/LoopVerticalScrollRect
- Assets/Canvas/LoopVerticalScrollRectが作成されることを確認
- LoopVerticalScrollRectのInspectorで、AddComponentから、VerticalLayoutGroupを追加し、ControlChildSizeのWIdthとHeightにチェックと、ChildForceExpandのWidthにチェック。
ProjectのAssetsの下にPrefabsのdirを作成しておく。
- LoopVerticalScrollRect/Contentの下に、適当にImageやTextなどのコンテンツを作り、AddComponentから、LayoutElementを追加して、MinHeightにチェックを入れ200、FlexibleWidthにチェックを入れ1を入れ、Prefabsの下にドラッグ。
- LoopVertialScrollRectに以下スクリプトを設定
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
[RequireComponent(typeof(LoopVerticalScrollRect))]
[DisallowMultipleComponent]
public class LoopScrollRectInit : MonoBehaviour, LoopScrollPrefabSource, LoopScrollDataSource
{
[SerializeField] private GameObject prefab;
[SerializeField] int totalCount = 10;
Stack<Transform> pool = new Stack<Transform>();
private void Start()
{
var scrollRect = GetComponent<LoopVerticalScrollRect>(); ;
scrollRect.prefabSource = this;
scrollRect.dataSource = this;
scrollRect.totalCount = totalCount;
scrollRect.RefillCells();
}
// Objectが新規表示の時
GameObject LoopScrollPrefabSource.GetObject(int index)
{
if (pool.Count == 0)
{
return Instantiate(prefab);
}
Transform candidate = pool.Pop();
candidate.gameObject.SetActive(true);
return candidate.gameObject;
}
// Objectが枠外になった時
void LoopScrollPrefabSource.ReturnObject(Transform trans)
{
trans.gameObject.SetActive(false);
trans.SetParent(transform, false);
pool.Push(trans);
}
// 要素が表示される時
void LoopScrollDataSource.ProvideData(Transform trans, int index)
{
trans.Find("Text").GetComponent<Text>().text = "文章" + index.ToString();
}
}
縦リストにスクロール追加
- LoopVertialScrollRectの下にUI/Scrollbarを追加。
- ScrollbarのInspectorのScrollbarのDirection をTopToBottomに変更
- LoopVertialScrollRectのInspectorのVerticalScrollbarに、上で作ったScrollbarをドラッグ。
スクロールが動かないとき
Canvasを作ったときに自動で作られる、EventSystemが、ヒエラルキーに作成されてるか確認して、なければ追加する。
参考
https://light11.hatenadiary.com/entry/2022/05/16/201949
https://github.com/qiankanglai/LoopScrollRect/blob/master/Samples~/Demo/Scripts/InitOnStart.cs
その他
TableScrollViewという別のプラグインもあり
