「Unity/UIScrollView」の版間の差分
提供: 初心者エンジニアの簡易メモ
(→縦移動をなくす) |
(→スクロールを指定のコンテンツに移動する) |
||
| 行63: | 行63: | ||
==スクロールを指定のコンテンツに移動する== | ==スクロールを指定のコンテンツに移動する== | ||
<pre> | <pre> | ||
| − | int | + | int itemKey = 2; |
GameObject content = GameObject.Find("Canvas/Panel/Scroll View/Viewport/Content"); | GameObject content = GameObject.Find("Canvas/Panel/Scroll View/Viewport/Content"); | ||
content.transform.localPosition = new Vector3(0, | content.transform.localPosition = new Vector3(0, | ||
| − | content.GetComponent<VerticalLayoutGroup>().spacing * | + | content.GetComponent<VerticalLayoutGroup>().spacing * itemKey, |
0); | 0); | ||
</pre> | </pre> | ||
2023年5月15日 (月) 10:06時点における版
目次
縦スクロールの中にtextを表示
- HierarchyにUI/ScrollViewで作成
- ScrollViewのViewportのContentの下で、左クリックから、UI/Textを新規追加
- ScrollViewのViewportのContentの下のTextを選択し、AddComponentでContentSizeFitterを追加
- ContentSizeFitterのVerticalfitをUnconstrainedからPreferredSizeにする
- ScrollViewのViewportのContentの縦サイズは、コンテンツの縦サイズにフィットさせ、windowからは、はみ出る感じにする。
c#から文字を追加する場合はこんな感じ
GameObject text = GameObject.Find("/Canvas/Scroll View/Viewport/Content/Text");
text.GetComponent<Text>().text = @"利用規約
この利用規約~";
スクロールの下に移動する
scroll.verticalNormalizedPosition = 0;を追加すればよいが、 スクロールが一番下の1つ上までしか、いかないので、 表示処理の0.1秒後ぐらいに、実行すると、一番下まで行く。
Invoke("ScrollDownGui", 0.1f);
void ScrollDownGui()
{
// スクロール下へ
GameObject content = GameObject.Find("/Canvas/Scroll View/Viewport/Content");
GameObject scrollObj = GameObject.Find("/Canvas/Scroll View/");
ScrollRect scroll = scrollObj.GetComponent<ScrollRect>();
scroll.verticalNormalizedPosition = 0;
content.GetComponent<ContentSizeFitter>().SetLayoutVertical();
}
参考:https://qiita.com/sonken625/items/adb6100f9f6d76dbdce4
UniRxを使ってスクロール下へ移動する
Contentの縦幅変更を、検知するとスクロール下へ移動するように
[SerializeField] RectTransform content; // ScrollView/Viewport/Content
[SeralizeField]] scrollRect; // ScrollView
content.ObserveEveryValueChanged(_ => _.sizeDelta.y)
.Subscribe(text => {
if (scrollRect != null)
{
scrollRect.verticalNormalizedPosition = 0;
}
})
.AddTo(gameObject);
スクロールView内にマテリアルを入れるとはみ出る
以下のような対応をすれば良いかも。
- 可能であれば、マテリアルを使わない。
- ScrollViewにaddComponents/RectMask2Dを設定
参考:https://tech.pjin.jp/blog/2017/02/22/unity_ugui_scrollbar/
参考:https://teratail.com/questions/98004
動的にリスト表示する時
unity/リスト表示 [ショートカット]
横スクロールバー削除
縦移動をなくす
ScrollViewのInspectorのScrollRectからVerticalのチェックを外す。
スクロールを指定のコンテンツに移動する
int itemKey = 2;
GameObject content = GameObject.Find("Canvas/Panel/Scroll View/Viewport/Content");
content.transform.localPosition = new Vector3(0,
content.GetComponent<VerticalLayoutGroup>().spacing * itemKey,
0);
