다음 섹션에서는 플레이어 UI에 위젯을 추가하거나 제거하는 방법을 알아봅니다.
이 예시에서는 텍스트 블록 위젯을 사용하지만 어떤 위젯이든 사용할 수 있습니다. 사용 가능한 모든 위젯 타입에 대한 자세한 내용은 위젯 타입을 참고하세요.
위젯 추가하기
이 예시에서는 플레이어가 레벨에서 버튼 장치와 상호작용할 때 위젯을 추가하는 방법을 보여줍니다.
템플릿에서 새 Verse 장치를 생성하고 레벨에 추가합니다. Verse에서 새로운 장치를 만드는 방법을 알아보려면 Verse를 사용하여 나만의 장치 만들기를 참고하세요.
Verse 익스플로러를 사용하여 Visual Script Code에서 현재 Verse 파일을 엽니다.
편집 가능 버튼 장치 프로퍼티를 Verse 장치에 추가하고 레벨 내 버튼 장치에 대한 레퍼런스를 구성합니다. 편집 가능 프로퍼티를 생성하는 방법에 대한 자세한 내용은 장치 프로퍼티 커스터마이징하기를 참고하세요.
# 에디터에서 버튼 장치가 레벨 내 장치를 참조하도록 설정합니다. @editable MyButton : button_device = button_device{}Verse 파일 상단에 다음 줄을 추가하여 Verse UI API를 사용합니다.
using { /UnrealEngine.com/Temporary/UI } using { /Fortnite.com/UI } using { /UnrealEngine.com/Temporary/SpatialMath}이 UI는 플레이어별로만 작동하므로 플레이어의 UI에 액세스하려면 해당 플레이어에 대한 레퍼런스가 필요합니다. 이 예시에서는 플레이어가 버튼과 인터랙션할 때 커스텀 UI를 표시하는 방법을 보여줍니다. 장치에 대한 이벤트는 장치와 인터랙션한 플레이어에 대한 레퍼런스를 포함하기 때문입니다. 이벤트 구독을 구성하는 방법에 대한 자세한 내용은 장치 상호작용 코딩하기를 참고하세요.
# 실행 중인 게임에서 장치가 시작되면 실행됩니다. OnBegin<override>()<suspends>:void= MyButton.InteractedWithEvent.Subscribe(HandleButtonInteraction) # 커스텀 UI는 특정 플레이어와 연결될 수만 있으며 해당 플레이어만 볼 수 있습니다. HandleButtonInteraction(Agent : agent) : void = # UI 표시이제 실패 가능 함수
GetPlayerUI[]를 if 표현식에서 호출하여 플레이어 UI에 대한 레퍼런스를 가져올 수 있습니다.Verse# A custom UI can only be associated with a specific player, and only that player can see it HandleButtonInteraction(Agent : agent) : void = # Agents can be a player or AI, but you can only get the UI of a player # so you must cast the Agent, who interacted with the Button device, to the player type if (InPlayer := player[Agent], PlayerUI := GetPlayerUI[InPlayer]): # Display UI플레이어 UI에 대한 레퍼런스가 있으면
AddWidget()을 호출하여 위젯을 추가할 수 있습니다.localizes지정자를 갖춘 TextForMyUI 메시지를 생성하고"Hello, world!"스트링으로 초기화합니다.hello_world_device := class(creative_device): # 에디터에서 버튼 장치가 레벨 내 장치를 참조하도록 설정합니다. @editable MyButton : button_device = button_device{} # UI에서 텍스트로 표시할 현지화 가능 메시지 TextForMyUI<localizes> : message = "Hello, world!"
message타입의 경우 텍스트를 현지화할 수 있으며,message변수를 초기화하는 데 사용하는 스트링으로 message의 디폴트 텍스트 및 언어가 설정됩니다.message타입에 대한 자세한 내용은 Verse 언어 퀵 레퍼런스를 참고하세요.그런 다음
TextForMyUI가 디폴트 텍스트인 텍스트 위젯을 생성하고 플레이어 UI에 추가합니다.Verse# A custom UI can only be associated with a specific player, and only that player can see it HandleButtonInteraction(Agent : agent) : void = # Agents can be a player or AI, but you can only get the UI of a player # so you must cast the Agent, who interacted with the Button device, to the player type if (InPlayer := player[Agent], PlayerUI := GetPlayerUI[InPlayer]): MyUI : text_block = text_block{DefaultText := TextFOrMyUI} PlayerUI.AddWidget(MyUI)
완성된 코드는 다음과 같습니다.
Verseusing { /Fortnite.com/Devices } using { /Verse.org/Simulation } using { /UnrealEngine.com/Temporary/UI } using { /Fortnite.com/UI } using { /UnrealEngine.com/Temporary/SpatialMath} hello_world_device := class(creative_device): # Set the Button device in the Editor to reference the device in the level @editableVerse 파일을 저장하고 UEFN 메인 메뉴에서 Verse > Verse 코드 빌드(Build Verse Code)를 선택하여 Verse 장치를 업데이트합니다.
이 Verse 장치로 레벨을 플레이테스트하면 플레이어가 버튼 장치와 상호작용할 때 화면 왼쪽 상단에 'Hello, world!' 텍스트가 표시됩니다.
위젯 제거하기
이 예시에서는 플레이어가 버튼 장치와 다시 상호작용할 때 플레이어 화면에서 UI를 제거하는 방법을 살펴봅니다.
위젯 추가하기에서 생성한 Verse 파일을 엽니다.
나중에 플레이어가 버튼과 다시 상호작용할 때 위젯을 제거할 수 있게 하려면 나중에 위젯을 참조할 수 있도록 변수에 저장해야 합니다. 위젯은 플레이어별로만 표시되므로 위젯을 저장하고 표시할 플레이어와 연결해야 올바른 플레이어에게 표시할 수 있습니다.
맵에서
player를 키로 사용하고 선택 사항 텍스트 블록을 위젯 값으로 사용하는 맵 변수MaybeMyUIPerPlayer를 생성합니다. 이 예시에서는 텍스트 블록 위젯을 사용하지만 어떤 위젯이든 사용할 수 있습니다.var MaybeMyUIPerPlayer : [player]?text_block = map{}위젯을 플레이어 UI에 추가할 때 나중에 화면에서 제거할 수 있도록
MaybeMyUIPerPlayer변수에 저장합니다. 맵에서 요소에 액세스하는 것은 실패 가능 표현식이 되어야 하므로, 플레이어와 관련된 위젯 값을 설정하는 것은if표현식과 같은 실패 가능 컨텍스트에서 호출해야 합니다.Verse# A custom UI can only be associated with a specific player, and only that player can see it HandleButtonInteraction(Agent : agent) : void = # Agents can be a player or AI, but you can only get the UI of a player # so you must cast the Agent, who interacted with the Button device, to the player type if (InPlayer := player[Agent], PlayerUI := GetPlayerUI[InPlayer]): NewUI := text_block{DefaultText := TextForMyUI} PlayerUI.AddWidget(NewUI) if (set MaybeMyUIPerPlayer[InPlayer] = option{NewUI}) {}이제 함수를 업데이트하여 플레이어가
MaybeMyUIPerPlayer에 표시할 위젯을 이미 가지고 있는지 확인합니다. 가지고 있다면 위젯을 제거하고, 가지고 있지 않다면 표시할 새 위젯을 생성합니다.Verse# A custom UI can only be associated with a specific player, and only that player can see it HandleButtonInteraction(Agent : agent) : void = # Agents can be a player or AI, but you can only get the UI of a player # so you must cast the Agent, who interacted with the Button device, to the player type if (InPlayer := player[Agent], PlayerUI := GetPlayerUI[InPlayer]): if (MyUI := MaybeMyUIPerPlayer[InPlayer]?): PlayerUI.RemoveWidget(MyUI) if (set MaybeMyUIPerPlayer[InPlayer] = false) {} else: NewUI := text_block{DefaultText := TextForMyUI}완성된 코드는 다음과 같습니다.
Verseusing { /Fortnite.com/Devices } using { /Verse.org/Simulation } using { /UnrealEngine.com/Temporary/UI } using { /Fortnite.com/UI } using { /UnrealEngine.com/Temporary/SpatialMath} hello_world_device := class(creative_device): # Set the Button device in the Editor to reference the device in the level @editableVerse 파일을 저장하고 UEFN 메인 메뉴에서 Verse > Verse 코드 빌드를 선택하여 Verse 장치를 업데이트합니다.
이 Verse 장치로 레벨을 플레이테스트하면 플레이어가 버튼 장치와 처음 인터랙션할 때 화면 왼쪽 상단에 'Hello, world!' 텍스트가 표시됩니다. 플레이어가 버튼 장치와 다시 상호작용하면 텍스트가 사라집니다.