バージョン: 2021.3 以降
この例は UnityEditor.PopupWindow
を使ってポップアップウィンドウを作成する方法を紹介します。
この例では、カスタムエディターウィンドウのボタンで表示されるポップアップウィンドウを作成します。ポップアップウィンドウには 3 つのトグルがあり、フォーカスがないときは閉じます。
この例で作成するすべてのファイルは、GitHub リポジトリ にあります。
このガイドは、Unity エディター、UI Toolkit、および C# スクリプトに精通している開発者を対象としています。始める前に、以下をよく理解してください。
ボタン付きのカスタムエディターウィンドウを作成します。UI ドキュメント (UXMLファイル) でボタンを定義し、USS ファイルでスタイルを設定します。最後に C# スクリプトでボタンロジックを定義すると、ボタンをクリックするとポップアップウィンドウが表示されます。
任意のテンプレートで Unity プロジェクトを作成します。
Project ウィンドウで右クリックし、Create > UI Toolkit > Editor Window を選択します。
UI Toolkit Editor Window Creator ウィンドウの C# ボックスに、PopupExample
と入力します。
Confirm を選択します。これで 3 つのファイル PopupExample.cs
、PopupExample.uxml
、PopupExample.uss
が作成されます。
PopupExample.uxml
を次のコンテンツに置き換えてください。
<ui:UXML xmlns:ui="UnityEngine.UIElements" xmlns:uie="UnityEditor.UIElements" xsi="http://www.w3.org/2001/XMLSchema-instance" engine="UnityEngine.UIElements" editor="UnityEditor.UIElements" noNamespaceSchemaLocation="../../UIElementsSchema/UIElements.xsd" editor-extension-mode="True">
<Style src="PopupExample.uss" />
<ui:Button text="Popup Options" display-tooltip-when-elided="true" class="popup-button" />
</ui:UXML>
PopupExample.cs
を次のコンテンツに置き換えてください。
using UnityEditor;
using UnityEngine.UIElements;
using PopupWindow = UnityEditor.PopupWindow;
public class PopupExample : EditorWindow
{
// Add menu item
[MenuItem("Example/Popup Example")]
static void Init()
{
EditorWindow window = EditorWindow.CreateInstance<PopupExample>();
window.Show();
}
private void CreateGUI()
{
var visualTreeAsset = AssetDatabase.LoadAssetAtPath<VisualTreeAsset>("Assets/Editor/PopupExample.uxml");
visualTreeAsset.CloneTree(rootVisualElement);
var button = rootVisualElement.Q<Button>();
button.clicked += () => PopupWindow.Show(button.worldBound, new PopupContentExample());
}
}
PopupExample.uss
を次のコンテンツに置き換えてください。
.popup-button {
width: 200px;
}
ポップアップウィンドウのコンテンツを定義する UI ドキュメント (UXMLファイル) を作成します。ウィンドウサイズを設定し、ウィンドウを表示する C# クラスを作成します。
Editor
フォルダーに、以下のコンテンツを含む PopupWindowContent.uxml
という UI ドキュメントを作成します。
<ui:UXML xmlns:ui="UnityEngine.UIElements" xmlns:uie="UnityEditor.UIElements" xsi="http://www.w3.org/2001/XMLSchema-instance" engine="UnityEngine.UIElements" editor="UnityEditor.UIElements" noNamespaceSchemaLocation="../../../UIElementsSchema/UIElements.xsd" editor-extension-mode="True">
<ui:Toggle label="Toggle 1" name="Toggle1"/>
<ui:Toggle label="Toggle 2" />
<ui:Toggle label="Toggle 3" />
</ui:UXML>
Editor
フォルダーに、以下のコンテンツを含む PopupContentExample.cs
という C# ファイルを作成します。
using UnityEditor;
using UnityEngine;
using UnityEngine.UIElements;
public class PopupContentExample : PopupWindowContent
{
//Set the window size
public override Vector2 GetWindowSize()
{
return new Vector2(200, 100);
}
public override void OnGUI(Rect rect)
{
// Intentionally left empty
}
public override void OnOpen()
{
Debug.Log("Popup opened: " + this);
var visualTreeAsset = AssetDatabase.LoadAssetAtPath<VisualTreeAsset>("Assets/Editor/PopupWindowContent.uxml");
visualTreeAsset.CloneTree(editorWindow.rootVisualElement);
}
public override void OnClose()
{
Debug.Log("Popup closed: " + this);
}
}
ポップアップウィンドウをテストするには、メニューから Example > Popup Example > Popup Options を選択します。
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.