앱 개발/Unity, C#

[Unity][UI Toolkit] UI Toolkit 사용하면서 살짝 헤맨 부분들

마라턍 2024. 12. 31. 10:00

오래 헤매진 않았고, 그냥 당연히 이렇게 하면 되겠지~라고 했다가 안 돼서 검색한 부분을 살짝쿵 모아보았다.

 

커스텀 컨트롤에서 Content Container 설정

  • ui toolkit custom control content container add child in inner
  • custom c# visualElement child

나의 경우에는 커스텀한 Label Container을 만들 때와 grid View를 만들고 해당 뷰와 연결되는 리스트가 비어있을 때 보여줄 empty container을 만들기 위해 사용하였다.

간단하게 VisualElement의 virtual 프로퍼티인 contentContainer을 override하면 된다.

private VisualElement _contentContainer;
public override VisualElement contentContainer => _contentContainer;
// ...

  public Container()
  {
    // ...
    _contentContainer = new() { name = "container-content" };
    // ...
   }

 

다만 이 경우, 새로운 VisualElement를 추가할 때의 위치가 root인지 contentContainer인지 확실하게 하기 위해 Add() 대신 hierarchy.Add()를 사용하도록 하자.

 

 

Gradient Background

위 discussions에 있는 코드를 짜집기해서 사용

 

 

 

스크립트에서 VisualElement의 BackgroundImage를 RenderTexture로 설정

  • ui toolkit set background render texture in script

Texture2D나 Sprite 맹키로 바로 style의 backgroundImage에 할당할 수 없어서 살짝 당황했지만, Background.FromRenderTexture로 한번 감싸서 넣으면 잘 작동한다.

나는 static한 VisualElement.Extension에 아래 메서드를 추가하여 쉽고 편하게 VisualElement의 값을 바꿀 수 있도록 하였다.

public static T SetBackgroundImage<T>(this T e, Texture2D value) where T : VisualElement
{
    e.style.backgroundImage = value;
    return e;
}

public static T SetBackgroundImage<T>(this T e, Sprite value) where T : VisualElement
{
    e.style.backgroundImage = new StyleBackground(value);
    return e;
}

public static T SetBackgroundImage<T>(this T e, RenderTexture value) where T : VisualElement
{
    e.style.backgroundImage = new StyleBackground(Background.FromRenderTexture(value))
    return e;
}

 

 

스크립트에서 접근할 때 World Bound 및 width와 height 값이 0으로 찍힘

  • ui toolkit world bound 0 on display.flex

GeometryChangeEvent에 Listener을 추가하여, width 및 height가 설정된 이후에 관련 코드가 진행되도록 변경 필요

 

 

ASWD를 눌렀더니 UI의 포커스가 이동함

 

Remapping the default UI navigation keys

Not quite sure if this is an easy task. In my Widgets, I am able to move around the buttons using the keys on my keyboard (Up, down, left, right arrows), now, to press on a button, you can do so by pressing enter or spacebar. However, the spacebar is conne

forums.unrealengine.com

 

 

Z index 및 click event 처리