コガネブログ

平日更新を目標に Unity や C#、Visual Studio、ReSharper などのゲーム開発アレコレを書いていきます

【Unity】スクリプトから UIElements の要素を横に並べる方法

概要

var root = new VisualElement();
root.style.flexDirection = FlexDirection.Row;

VisualElementstyle.flexDirectionFlexDirection.Row を指定する

これだけだと要素が左に表示されてしまう

var root = new VisualElement();
root.style.flexDirection = FlexDirection.Row;

prevButton.style.flexGrow = 1;
nextButton.style.flexGrow = 1;

root.Add( prevButton );
root.Add( nextButton );

子要素の style.flexGrow1 を指定すると横幅いっぱいに同じ幅で表示される