Unityの新しいUI実装のツール.
昔はUIElementという名前でEditor拡張用のイメージがあったけど、今は普通のゲーム実装でも推奨になっている。ただしuGUIのすべての機能をサポートしていない
![](https://cdn-ak.f.st-hatena.com/images/fotolife/b/bibinbaleo/20240412/20240412142155.png)
docs.unity3d.com
World Space用にもまだ使えない
![](https://cdn-ak.f.st-hatena.com/images/fotolife/b/bibinbaleo/20240412/20240412142504.png)
Html/CSS的な書き方らしい。最近React学んだから相性いいかと思ったけど、jQuery的なものを使うらしくReactとは違うらしい
![](https://cdn-ak.f.st-hatena.com/images/fotolife/b/bibinbaleo/20240412/20240412142742.png)
UI Toolkitメモ - フレームシンセシス
あんまり話聞かないから使われてるのかな?Realityでは導入しているって!
note.com
Twitter検索した感じまだガッツリ使ってる人はいないのかな。足りていない機能もあるし
UIBuilderはこれのパクリ?
gluonhq.com
とりあえず使う
記事を参考にしながらとりあえず触る
qiita.com
UIBuilder
UIBuilderというUIをドラッグアンドドロップで配置できるツールがある。(ホームページビルダーみたい)
![](https://cdn-ak.f.st-hatena.com/images/fotolife/b/bibinbaleo/20240412/20240412143048.png)
左下の要素を置いていく
![](https://cdn-ak.f.st-hatena.com/images/fotolife/b/bibinbaleo/20240412/20240412143619.png)
適当に面白そうなUI全部置いた
![](https://cdn-ak.f.st-hatena.com/images/fotolife/b/bibinbaleo/20240412/20240412144355.png)
保存するとuxmlファイルができる
![](https://cdn-ak.f.st-hatena.com/images/fotolife/b/bibinbaleo/20240412/20240412143752.png)
シーンに置く
ヒエラルキーでUI Toolkit>UI Document
![](https://cdn-ak.f.st-hatena.com/images/fotolife/b/bibinbaleo/20240412/20240412144634.png)
SourceAssetsにさっきのuxmlをアタッチ
![](https://cdn-ak.f.st-hatena.com/images/fotolife/b/bibinbaleo/20240412/20240412144732.png)
するとゲームビューに表示された!シーンビューには何も映らない。
![](https://cdn-ak.f.st-hatena.com/images/fotolife/b/bibinbaleo/20240412/20240412144623.png)
デザインやレイアウト変更
CSS的なのでもできるけど、Builder上の右側の設定でもいろいろできる
![](https://cdn-ak.f.st-hatena.com/images/fotolife/b/bibinbaleo/20240412/20240412151101.png)
保存したらすぐGameビューのほうに反映されるし楽しい。
![](https://cdn-ak.f.st-hatena.com/images/fotolife/b/bibinbaleo/20240412/20240412151452.png)
カーソルまで設定できて、設定したエリア上にマウスを当てたらそのカーソルになるw
![](https://cdn-ak.f.st-hatena.com/images/fotolife/b/bibinbaleo/20240412/20240412151614.png)
角丸も簡単にできる
![](https://cdn-ak.f.st-hatena.com/images/fotolife/b/bibinbaleo/20240412/20240412154221.png)
スクリプトから操作
UIDocumentごとプレハブ化できる
![](https://cdn-ak.f.st-hatena.com/images/fotolife/b/bibinbaleo/20240412/20240412154320.png)
スクリプトでプレハブ化したUIDocumentを呼び出すと、表示される
![](https://cdn-ak.f.st-hatena.com/images/fotolife/b/bibinbaleo/20240412/20240412154540.png)
![](https://cdn-ak.f.st-hatena.com/images/fotolife/b/bibinbaleo/20240412/20240412154523.png)
ボタンにアクセスするには、名前で参照
![](https://cdn-ak.f.st-hatena.com/images/fotolife/b/bibinbaleo/20240412/20240412162448.png)
![](https://cdn-ak.f.st-hatena.com/images/fotolife/b/bibinbaleo/20240412/20240412162444.png)
参考記事はそれ以外にもリストとして子のカードを並べて表示するとか、スクリプトでの操作がいろいろ紹介されていた。
こちらにもInputFieldの値の取り方とか書いてある
tech.framesynthesis.co.jp
デザインやりたいこと
Builderでできるのかな?
日本語フォント
フォントをセットしても反映されない~と思ったら、下のFontAssetsにデフォルトで入っているものを消したら反映された
![](https://cdn-ak.f.st-hatena.com/images/fotolife/b/bibinbaleo/20240412/20240412164622.png)
![](https://cdn-ak.f.st-hatena.com/images/fotolife/b/bibinbaleo/20240412/20240412164717.png)
簡単に使える~と思ったけどこれだとビルドしたとき表示されないらしい
https://amabie-labo.com/dev_article/article01
TextMeshProと同じくFontAssets化しないといけないって。そして下のFontAssetのところにセットする
テキスト省略...
...できるボタンあるけどならない。。。
![](https://cdn-ak.f.st-hatena.com/images/fotolife/b/bibinbaleo/20240412/20240412163939.png)
Unityフォーラムに答えがあった
forum.unity.com
displayのOverflowもhudden(右のやつ)にしないといけなかった。わかるか~
![](https://cdn-ak.f.st-hatena.com/images/fotolife/b/bibinbaleo/20240412/20240412171129.png)
画像を入れる
UIBuilderの中に画像コンポーネントがない;;
ファーラムの投稿にもあったが「ない」らしい。
forum.unity.com
自分で以下のコードを書いてAsstsのどこかに置く。
using UnityEngine.UIElements;
public class MyImage : Image
{
public new class UxmlFactory : UxmlFactory<MyImage, Image.UxmlTraits>{}
public MyImage()
{
}
}
そしたらUIBuilderのLibraryのProjectに入る
![](https://cdn-ak.f.st-hatena.com/images/fotolife/b/bibinbaleo/20240412/20240412173458.png)
これでサイズやBackgroundの画像を設定したら表示できた。
![](https://cdn-ak.f.st-hatena.com/images/fotolife/b/bibinbaleo/20240412/20240412173451.png)
影を付ける
テキストには付けれた。ついでにワードごとの間の値も設定できた
![](https://cdn-ak.f.st-hatena.com/images/fotolife/b/bibinbaleo/20240415/20240415134306.png)
画像とかボタンには付けれないのかな?ないっぽい
これもフォーラムですごい会話されてる。つい最近まで。やっぱりみんなソフトシャドウほしいよな。
forum.unity.com
現時点ではないけどロードマップにあって2024年(今年!)追加されるっぽい。今やりたかったら画像用意するか、コード書く。(有志がコード提供している)
forum.unity.com
デフォルトコンポーネントの細かいUI編集
スクロールバーとかInputFieldとかデフォルトのコンポーネントのデザインは編集できない
![](https://cdn-ak.f.st-hatena.com/images/fotolife/b/bibinbaleo/20240415/20240415135856.png)
どうやるんだろう?
教えてもらった
cssを作るときにそのパーツの名前(#unity-text-input)とかで書く。そして右側のインスペクタでかえると変わる。
![](https://cdn-ak.f.st-hatena.com/images/fotolife/b/bibinbaleo/20240426/20240426141504.png)
同じパーツ名だが、別々のデザインにしたいときは親オブジェクトの名前を変えて#TextField_Name > #unity-text-input
この記号で親の名前を指定したら別々になる。
highlighの色など(?)
ボタンなどをホバーしたときとかの色の変化も設定するとこなさそう。一応実行すると大きさ変わったりちょっと色変化するのでどこかに設定されてそう。USSかな?
![](https://cdn-ak.f.st-hatena.com/images/fotolife/b/bibinbaleo/20240415/20240415135710.png)
コードでのuxml,uss編集
ここのタブ上に上げれた。
![](https://cdn-ak.f.st-hatena.com/images/fotolife/b/bibinbaleo/20240426/20240426135354.png)
uxml
UIBuilderで作ったuxmlをコードとして開くにはどうするんだ?普通にクリックしたらBuilderが表示される。
uibuilderの画面の一番下のアイコンから見れた
![](https://cdn-ak.f.st-hatena.com/images/fotolife/b/bibinbaleo/20240415/20240415140302.png)
ここからしかできないのかな?
![](https://cdn-ak.f.st-hatena.com/images/fotolife/b/bibinbaleo/20240415/20240415140304.png)
さっきのshadowも付けてみた
![](https://cdn-ak.f.st-hatena.com/images/fotolife/b/bibinbaleo/20240415/20240415140935.png)
でか・・・
![](https://cdn-ak.f.st-hatena.com/images/fotolife/b/bibinbaleo/20240415/20240415140923.png)
USSで調整するらしい
USS(?)
デフォルトではunityが定義したtss?
![](https://cdn-ak.f.st-hatena.com/images/fotolife/b/bibinbaleo/20240415/20240415143749.png)
新しく作るにはuibuilderの左上の+から追加できる
![](https://cdn-ak.f.st-hatena.com/images/fotolife/b/bibinbaleo/20240415/20240415141848.png)
公式ドキュメント読んだけどそこからどうするのか全く分からない。。。
docs.unity3d.com
+ボタンの右のAdd new selector...のところに何か自分で入力するっぽい。
![](https://cdn-ak.f.st-hatena.com/images/fotolife/b/bibinbaleo/20240415/20240415142457.png)
そしてUI選んでスタイルシートの欄に名前入力するといいのか???
![](https://cdn-ak.f.st-hatena.com/images/fotolife/b/bibinbaleo/20240415/20240415142545.png)
さっきとのuxmlと同じように画面下にコード開くボタンが出る
![](https://cdn-ak.f.st-hatena.com/images/fotolife/b/bibinbaleo/20240415/20240415143509.png)
shadowのcss書いてみた
![](https://cdn-ak.f.st-hatena.com/images/fotolife/b/bibinbaleo/20240415/20240415143627.png)
何も変わらなかった。フォーラムの下のほう見たらUSSでは動かずインスペクタで調整って書いてた。
TextのColor変えたら変わったけど、何か間違ってる・・・まあこれは主題じゃないからいいや
![](https://cdn-ak.f.st-hatena.com/images/fotolife/b/bibinbaleo/20240415/20240415144327.png)
公式サンプルアセット
もうよく分からないのでサンプルを見る
Unity公式から二つサンプルアセットが出てる。QuizUとDragon Crashers
gamemakers.jp
QuizU
assetstore.unity.com
ここから起動する
![](https://cdn-ak.f.st-hatena.com/images/fotolife/b/bibinbaleo/20240415/20240415150511.png)
クイズと設定画面とUIToolKitデモと各種リンクページの4つのコンテンツがある
![](https://cdn-ak.f.st-hatena.com/images/fotolife/b/bibinbaleo/20240415/20240415150545.png)
クイズはUnityのAudioとかUIToolKitとかいろいろ分かれてる。クイズの内容は本題じゃなくてあくまでUIToolKitでのUIを見せるサンプルだと思うけど結構クイズの量多かった。
![](https://cdn-ak.f.st-hatena.com/images/fotolife/b/bibinbaleo/20240415/20240415150649.png)
![](https://cdn-ak.f.st-hatena.com/images/fotolife/b/bibinbaleo/20240415/20240415150652.png)
起動したときから感じてるけど、結構UI崩れる。
![](https://cdn-ak.f.st-hatena.com/images/fotolife/b/bibinbaleo/20240415/20240415150819.png)
UIToolKitはマルチ解像度のUIを作れることも目的だった気がするんだけどな~uGUIでもちゃんとマルチ解像度のUI作れるのに何でデモでマルチ解像度対応しないんだろう?
できないのかやってないのか分からない・・・
テキストの大きさとか折り返し設定とかも気持ち悪いし。
DEMOのページではUXMLとかUCCとかの説明とデモ
![](https://cdn-ak.f.st-hatena.com/images/fotolife/b/bibinbaleo/20240415/20240415150716.png)
![](https://cdn-ak.f.st-hatena.com/images/fotolife/b/bibinbaleo/20240415/20240415150703.png)
ボタンのhoverとかの話もあった。
![](https://cdn-ak.f.st-hatena.com/images/fotolife/b/bibinbaleo/20240415/20240415150706.png)
![](https://cdn-ak.f.st-hatena.com/images/fotolife/b/bibinbaleo/20240415/20240415150711.png)
では実際に中身を見てみよう~ここにある
![](https://cdn-ak.f.st-hatena.com/images/fotolife/b/bibinbaleo/20240415/20240415151125.png)
私はデフォルトだと崩れている
![](https://cdn-ak.f.st-hatena.com/images/fotolife/b/bibinbaleo/20240415/20240415151132.png)
一番上の部品を選んでWidthとHeightを変えた
![](https://cdn-ak.f.st-hatena.com/images/fotolife/b/bibinbaleo/20240415/20240415151205.png)
気になっていた、〇ボタンの中にアイコンは、丸い形ボタンの下にvisual elementsで画像置いてた
![](https://cdn-ak.f.st-hatena.com/images/fotolife/b/bibinbaleo/20240415/20240415151842.png)
画面全体の解像度変えたときに、丸のサイズとアイコンのサイズが別の比率で変わって気持ち悪かった
![](https://cdn-ak.f.st-hatena.com/images/fotolife/b/bibinbaleo/20240415/20240415151720.png)
USSは普通にダブルクリックしたら開くということが分かった。
![](https://cdn-ak.f.st-hatena.com/images/fotolife/b/bibinbaleo/20240415/20240415153612.png)
そしてさっき分からなかったUIBuilderでのUSSのselectorのことも少しわかったような。
作りたい名前のselectorを入力したらUSSにこれが追加されるから、この中はスクリプトで書くのかな?
![](https://cdn-ak.f.st-hatena.com/images/fotolife/b/bibinbaleo/20240415/20240415154218.png)
そしてこれを適用したいコンポーネントにselectorを適用する。
![](https://cdn-ak.f.st-hatena.com/images/fotolife/b/bibinbaleo/20240415/20240415154220.png)
u-queryはサンプルシーン見てもどこに使われてるかよくわからなかった。まず元のjqueryが何なのか知らない
Dragon Crashers
※このアセットにプロジェクト全体が乗っ取られるので新しいプロジェクトに入れたほうがいい。自分が作ったシーンを再生してもこのゲームのシーンが再生される
assetstore.unity.com
![](https://cdn-ak.f.st-hatena.com/images/fotolife/b/bibinbaleo/20240415/20240415165830.png)
QuizUとは共存しようとしたらQuizU側でエラーでる。
![](https://cdn-ak.f.st-hatena.com/images/fotolife/b/bibinbaleo/20240415/20240415165841.png)
git管理もしてないし、assetpackageからもremoveできないし詰んだか?と思ったが1個スクリプト消して、もう一つコメントアウトしたらエラー消えた。QuizUにどんな影響が出たかは分からない
![](https://cdn-ak.f.st-hatena.com/images/fotolife/b/bibinbaleo/20240415/20240415165807.png)
MainMenuからプレイできる。
![](https://cdn-ak.f.st-hatena.com/images/fotolife/b/bibinbaleo/20240415/20240415171813.png)
UIのサンプルなのでゲームとしてすべての機能は入ってない
![](https://cdn-ak.f.st-hatena.com/images/fotolife/b/bibinbaleo/20240415/20240415170020.png)
いろいろUI使われている
![](https://cdn-ak.f.st-hatena.com/images/fotolife/b/bibinbaleo/20240415/20240415165914.png)
QuizUよりはきれいなUI。でもマルチ解像度対応完ぺきではない。
![](https://cdn-ak.f.st-hatena.com/images/fotolife/b/bibinbaleo/20240415/20240415165916.png)
UIBuilderで見るとそれぞれのパーツが別のuxmlになっていて組み合わさっている。これはReactのコンポーネント的概念ではないのか
![](https://cdn-ak.f.st-hatena.com/images/fotolife/b/bibinbaleo/20240415/20240415171806.png)
公式ドキュメント
いい加減公式ドキュメントを読もう
docs.unity3d.com
UIBuilder以外にもツールがあった
![](https://cdn-ak.f.st-hatena.com/images/fotolife/b/bibinbaleo/20240415/20240415155838.png)
SamplesはUnityEditor用なのかな?ドキュメントには特にそんなこと書いてないけど
![](https://cdn-ak.f.st-hatena.com/images/fotolife/b/bibinbaleo/20240415/20240415155758.png)
![](https://cdn-ak.f.st-hatena.com/images/fotolife/b/bibinbaleo/20240415/20240415155756.png)
UI Toolkit Debugger
docs.unity3d.com
インスペクターの要素見れた
![](https://cdn-ak.f.st-hatena.com/images/fotolife/b/bibinbaleo/20240415/20240415160605.png)
自分で作ったUIの情報も見れるはず。
UIBuilderの右上のPreviewを押した状態で、DebuggerのPickElementを押して要素を選ぶ。もしくはゲームビューで実行中も同じことができる
![](https://cdn-ak.f.st-hatena.com/images/fotolife/b/bibinbaleo/20240415/20240415161300.png)
Figmaの開発モードでもこの多重長方形見るけどこれはWebでの定番なのか?あんまりなじみがない・・・
![](https://cdn-ak.f.st-hatena.com/images/fotolife/b/bibinbaleo/20240415/20240415161528.png)
最後に
もっと踏み込んでいくにはhtml/cssの知識が必要そう。
ui toolkitのudemyの講座は英語しかない><
www.udemy.com