버전:2021.3+
이 예제에서는 UnityEditor.PopupWindow
를 사용하여 팝업 창을 생성하는 방법을 보여 줍니다.
이 예제는 커스텀 에디터 창의 버튼을 통해 표시되는 팝업 창을 생성합니다.팝업 창에는 세 가지 토글이 있으며 초점이 맞지 않으면 팝업 창이 닫힙니다.
이 예제에서 생성한 완성된 파일은 GitHub 저장소에서 확인할 수 있습니다.
이 가이드는 Unity 에디터, UI 툴킷, 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.