LoginSignup
0
0

More than 3 years have passed since last update.

Unity 2D #4 ScrollViewにおけるlayout

Posted at

参考URL:https://tech.griphone.co.jp/2018/12/18/advent-calendar-20181218/

LayoutGroup

Child Controls Size : 子のLayout要素によって算出された幅によって、子自身の幅を変える設定。
Child Force Expand : 親の幅に対して子に余白がある場合、子を引き伸ばす設定。

Layout Element

Min ~~ : 最低でもこのサイズは確保してほしい、という値。これ以上小さくならない。
Preffered ~~ : 余っているサイズがあればこのサイズまで描画する。
Flexible ~~ : 余っているサイズの中で、全てのFlexible間で占める割合

●グリッド状に並べるオブジェクトの数をスクリプトで変更する方法
ScrollViewのContentにGridLayoutGroupを、オブジェクトにLayout Elementをアタッチする。

// あらかじめアタッチしておく
public GameObject ScrollContent;


// オブジェクトを並べる空間の横幅
double content_width = Screen.width - 20;
// いくつのオブジェクトを並べるか(ここでは、横幅50のオブジェクトを10感覚で並べようとしている)
int constraint_count = (int)(content_width/60);

// スクロールビューの横に並べるオブジェクト数の変更
ScrollContent.GetComponent<GridLayoutGroup>().constraintCount = constraint_count;
// オブジェクト同士の隙間を計算
double content_space = (content_width - constraint_count*50)/(constraint_count-1);
// オブジェクト同士の隙間をスクロールに反映
ScrollContent.GetComponent<GridLayoutGroup>().spacing = new Vector2((float)content_space, 50);
0
0
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
0
0