このページでは、UI Toolkit でイベントシステムや入力システムを使用する際によくある質問を掲載しています。
これには 2 つの方法があります。
最初の方法
com.unity.uGUI
パッケージからゲームオブジェクトベースの UI コンテンツを作成するのと同じ方法で、シーンに Event System コンポーネントを加えます。
EventSystem.current.IsPointerOverGameObject
メソッドを使用し、ポインタがuGUIまたはUI ToolkitのUIコンテンツ上にある場合にtrueを
返します。
EventSystem.current.RaycastAll
メソッドを使用して、マウスの下にあるビジュアル要素を確認します。
交差した UI Toolkit パネルは、ゲームオブジェクトを通して Event System の環境に表示されます。
PanelRaycaster
と PanelEventHandler
があります。どちらのコンポーネントも、ターゲットとする IPanel
を返す panel
プロパティを持っています。ポインターの下にパネルを見つけたら、panel.Pick
メソッドを呼び出し、ポインターの位置にあるビジュアル要素を探します。
RuntimePanelUtils.ScreenToPanel
メソッドを使用して、ポインターのスクリーン座標をパネル座標に変換する必要があります。
uGUI のスクリーン座標系は左下の原点を使用しますが、UI Toolkit のスクリーン座標は左上から表現されます。2 つのシステム間で変換するには、yTopLeft = Screen.height - yBottomLeft
で Y 座標をミラーリングするか、その逆を行う必要があります。
第 2 の方法
UIDocument.rootVisualElement
プロパティを使用して、ポインターの下にあるすべてのランタイムパネルのリストを取得します。panel.Pick
を連続して呼び出します。基本的な UI アクションを再マップするには、以下を行います。
com.unity.uGUI
パッケージからゲームオブジェクトベースの UI コンテンツを作成するのと同じ方法で、シーンに Event System コンポーネントを加えます。ノート: Tab 入力と Shift+Tab 入力にマップされたアクションは、イベントシステムの入力モジュールを通して公開されていないため、再マップすることはできません。
方向ナビゲーションは、デフォルトのターゲット以外にも設定できます。
次のコード例では、要素 A が上下左右に移動するときに、それぞれ要素 U、D、L、R に移動することが可能です。
A.RegisterCallback <NavigationMoveEvent>(e =>
{
switch(e.direction)
{
case NavigationMoveEvent.Direction.Up: U.Focus(); break;
case NavigationMoveEvent.Direction.Down: D.Focus(); break;
case NavigationMoveEvent.Direction.Left: L.Focus(); break;
case NavigationMoveEvent.Direction.Right: R.Focus(); break;
}
e.PreventDefault();
});
はい、可能です。EventSystem の StandaloneInputModule
またはInputSystemUIInputModule
Inspector フィールドを使用して、どの入力が各アクションにマップされるかを制御できます。ただし、これらのアクションは uGUI 入力と共有されるので、uGUI コントロールも変更されます。
uGUI コントロールに影響を与えずに UI Toolkit の入力を再マップするには、UI Toolkit のランタイムイベント処理を無効にし、すべてのイベントを手動でパネルに送信します。
ゲームシーンを初めてロードするときなど、フォーカスが当たっている要素やパネルが存在しないことがあります。この場合、キーボードナビゲーションは予測可能な最初の要素から始まりません。これは、完全にマウスなしでプレイするゲームでは問題になることがあります。
最初から予測可能なナビゲーション動作ができるようにするには C# スクリプトを加えて、スクリプトを、最初のフォーカスを得るように選択した要素を処理する UIDocument と同じゲームオブジェクトにアタッチします。
スクリプトの名前を FirstFocus
とし、最初にフォーカスされる要素の名前をfirst-focused
とします。スクリプトの Start()
メソッドに、その要素をフォーカスするコードを以下のように追加します。
public class FirstFocus : MonoBehaviour
{
void Start()
{
FocusFirstElement();
}
public void FocusFirstElement()
{
GetComponent<UIDocument>().rootVisualElement.
Q<VisualElement>("first-focused").Focus();
}
}
ノート: UIDocument のゲームオブジェクトを無効にすると、その下にある階層はすべて最初から作り直されます。したがって、ゲームオブジェクトを再度有効にした後、カスタムの FocusFirstElement()
メソッドを再度実行する必要があります。
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.