facebook twitter hatena line email

「Unity/UIScrollView」の版間の差分

提供: 初心者エンジニアの簡易メモ
移動: 案内検索
(スクロールを指定のコンテンツに移動する)
(スクロールを指定のコンテンツに移動する)
行68: 行68:
 
content.transform.localPosition = new Vector3(0,
 
content.transform.localPosition = new Vector3(0,
 
content.GetComponent<VerticalLayoutGroup>().spacing * itemNum - 1,
 
content.GetComponent<VerticalLayoutGroup>().spacing * itemNum - 1,
 +
0);
 +
</pre>
 +
 +
コンテンツを中断に出す場合Scroll Viewの幅の半分を、コンテンツ座標のyから引く
 +
<pre>
 +
int itemNum = 3;
 +
GameObject content = GameObject.Find("Canvas/Panel/Scroll View/Viewport/Content");
 +
content.transform.localPosition = new Vector3(0,
 +
content.GetComponent<VerticalLayoutGroup>().spacing * itemNum - 1 - scroll.transform.parent.transform.parent.GetComponent<RectTransform>().sizeDelta.y,
 
0);
 
0);
 
</pre>
 
</pre>

2023年5月22日 (月) 11:01時点における版

縦スクロールの中にtextを表示

  1. HierarchyにUI/ScrollViewで作成
  2. ScrollViewのViewportのContentの下で、左クリックから、UI/Textを新規追加
  3. ScrollViewのViewportのContentの下のTextを選択し、AddComponentでContentSizeFitterを追加
  4. ContentSizeFitterのVerticalfitをUnconstrainedからPreferredSizeにする
  5. 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/リスト表示 [ショートカット]

横スクロールバー削除

参考:https://www.chuken-engineer.com/entry/2019/11/12/160221#%E6%A8%AA%E3%82%B9%E3%82%AF%E3%83%AD%E3%83%BC%E3%83%AB%E3%83%90%E3%83%BC%E3%82%92%E7%84%A1%E5%8A%B9%E3%81%AB%E3%81%99%E3%82%8B

縦移動をなくす

ScrollViewのInspectorのScrollRectからVerticalのチェックを外す。

スクロールを指定のコンテンツに移動する

3番目のコンテンツに移動

int itemNum = 3;
GameObject content = GameObject.Find("Canvas/Panel/Scroll View/Viewport/Content");
content.transform.localPosition = new Vector3(0,
content.GetComponent<VerticalLayoutGroup>().spacing * itemNum - 1,
0);

コンテンツを中断に出す場合Scroll Viewの幅の半分を、コンテンツ座標のyから引く

int itemNum = 3;
GameObject content = GameObject.Find("Canvas/Panel/Scroll View/Viewport/Content");
content.transform.localPosition = new Vector3(0,
content.GetComponent<VerticalLayoutGroup>().spacing * itemNum - 1 - scroll.transform.parent.transform.parent.GetComponent<RectTransform>().sizeDelta.y,
0);

ScrollBar

https://futabazemi.net/unity/scroll_bar/