본문 바로가기

Unity

UI Element

UI Element는 유니티의 에디터를 확장하거나 바꿀 수 있는 기능이다.

예시)

UI Element 생성

Create - UI ToolKit - Editor Window 

UXML 체크 해제 후 Confirm을 누른다.

그러면 Editor 폴더에 파일이 추가되고 창 하나가 생기는 것을 볼 수 있다.

 

UI Element 편집

UIElement를 사용하기 위해서는 두가지 네임스페이스가 있어야 한다. 생성시에 자동으로 추가된다. 

using UnityEngine.UIElements;
using UnityEditor.UIElements;

 

VisualElement.Add

VisualElement.Insert       

 

    public void CreateGUI()
    {
        VisualElement container = new VisualElement();
        rootVisualElement.Add(container);

        Label title = new Label("Color Picker");
        ColorField colorField = new ColorField();

        container.Add(title);
        container.Add(colorField);

        VisualElement buttonsContainer = new VisualElement();
        rootVisualElement.Add(buttonsContainer);

        Button randomColorButton = (Button)CreateButton("Random Color");
        Button resetColorButton = (Button)CreateButton("Reset Color");
        Button copyColorButton = (Button)CreateButton("Copy Color");
        Button pasteColorButton = (Button)CreateButton("Paste Color");

        buttonsContainer.Add(randomColorButton);
        buttonsContainer.Add(resetColorButton);
        buttonsContainer.Add(copyColorButton);
        buttonsContainer.Add(pasteColorButton);

        container.Add(buttonsContainer);
    }
    
    public VisualElement CreateButton(string text)
    {
        return new Button() { text = text };
    }