UI をレンダリングし、ゲームビューでユーザーからの入力に反応するには、UI Document コンポーネントによって UXML ドキュメントを Panel Settings アセットに接続します。
すべての UI Document コンポーネントは、UI を定義する UXML Document と、それをレンダリングする Panel Settings を参照します。1 つの Panel Settings アセットに複数の UXML ドキュメントを接続することができます。
Panel Settings アセットは、シーン内のパネルを定義します。パネルは、UI 階層がアタッチされるルートビジュアル要素を提供し、ランタイムにシーンに UI を描画します。Panel Settings アセットをどのように設定するかによって、UI がどのようにレンダリングされるかが決まります。また、入力に対する UI の反応も決まります。例えば、視覚的に手前にあるパネルは、視覚的に奥にあるパネルよりも先にユーザーからのクリックをインターセプトします。
Panel Settings アセットを設定して、以下を行えます。
パネルを設定するには以下を行います。
パネルは、複数の UXML Document コンポーネントの UI を表示することができます。各 UI Document には、UXML ドキュメントのレンダリング順序を設定する Sort Order プロパティがあります。
複数の UXML Document をパネルに接続するには、以下を行います。
UI をホストするゲームオブジェクトを選択または作成します。
Component > UI Toolkit > UI Document を選択して、UI Documentコンポーネントを加えます。
UIDocument の Inspector ウィンドウで、以下を行います。
各 UXML Document に対してこのプロセスを繰り返します。
ノート: 同じ Panel Settings に複数の UXML Document コンポーネントがアタッチされている場合、これらのドキュメントはすべて共通のフォーカスナビゲーションコンテキストを持ちます。別々の Panel Settings を持つ場合、それらを横に並べても、ナビゲーションは一方から他方へ自動的にジャンプしません。ナビゲーションを一方から他方にジャンプさせるには、Panel Settings のfocusController
を、ジャンプ先の UI Document コンポーネントの FocusController
に設定する必要があります。
Unity は、コンポーネントの OnEnable()
メソッドを呼び出すと、UI Document コンポーネントのソース UXML ドキュメントをロードします。ビジュアルツリーが正しくロードされるようにするには、OnEnable()
メソッドの内部でコントロールと対話するロジックを追加します。つまり、スクリプトがOnEnable()
と OnDisable()
に応答して、UXML ドキュメントからビジュアル要素を安全に参照できるようにする必要があります。
UI Document コンポーネントは、OnEnable()
メソッドと OnDisable()
メソッドに応答すると、そのコンテンツを消去します。この方法は、UI Document がすぐに再利用しない UI 要素を削除し、関連リソースを効果的に消去します。さらに、UI Document コンポーネントに Panel Settings アセットが割り当てられていない場合、UI Document コンポーネントのコンテンツは自動的にクリアされます。
すぐに再利用される可能性が高い UI 要素や、初期化のペナルティを避けるために素早く表示する必要がある UI 要素を非表示にするには、UIDocument.rootVisualElement
の display
を none
に設定 します。これを使用して、より大きな UI 階層の一部である UI 要素のコンポーネントを非表示にすることもできます。
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.