-
[Unity][UI Toolkit] 버튼 누르는 동안 동작 실행앱 개발/Unity, C# 2024. 2. 18. 00:25
역시 아래 네이버 블로그와 내용은 동일하다.
[Unity][UI Toolkit] 버튼 누르는 동안 동작 실행
아 블로그 업로드에 부담을 느껴서 계속 안하게 되는데, 그냥 편하게 메모하는 식으로 가자. 오늘은 Visua...
blog.naver.com
기존 VisualElement Button은 처음 눌렀을 때(clicked) 설정된 동작을 한 번 실행한다.
따라서 버튼을 누르는 동안 오브젝트를 이동시킨다거나 회전시키는 등의 기능은 작성하기 어렵다.
물론 boolean 속성(isPressingButton)을 하나 만들어 버튼을 눌렀을 때 true로 설정하고,
Update 함수에서 이 속성의 값에 따라 수행하는 작업을 다르게 작성할 수도 있다.
하지만 이것은 우리가 찾는 방법이 아닐 것이다. 심지어 해당 작업이 자주 쓰이지 않는 기능이라면 더더욱 말이다.
열심히 구글링한 결과, 유니티 UI 툴킷에서 이미 우리가 생각하는 방식으로 동작하는 VisualElement인 RepeatButton을 제공하고 있다는 것을 알 수 있었다. 아래는 내가 검색한 키워드...
- How can I make button that work repeatedly while it's pressed
- unity ui toolkit button while clicking
- unity ui toolkit button active event
- unity ui toolkit while button pressed
그리고 발견한 공식 문서!
Unity - Scripting API: RepeatButton
Success! Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable. Close
docs.unity3d.com
Class RepeatButton | UI Toolkit | 1.0.0-preview.18
Class RepeatButton A button that executes an action repeatedly while it is pressed. Inherited Members Assembly : solution.dll Syntax public class RepeatButton : TextElement, IEventHandler, ITransform, ITransitionAnimations, IExperimentalFeatures, IVisualEl
docs.unity3d.com
정리하면 간단하다.
요약
1. 추가를 원하는 UXML 파일에 아래의 코드를 붙여 넣거나 Button을 RepeatButton으로 변경한다.
<ui:RepeatButton text="Text" display-tooltip-when-elided="true" name="RepeatBtn" />
2. 해당 버튼에 실행할 동작을 설정해준다.
RepeatButton repeatBtn = rootElement.Q<RepeatButton>("RepeatBtn"); repeatBtn.SetAction(OnPressRepeatBtn, 0, 1); private void OnPressRepeatBtn() { Debug.Log("Pressing Repeat Button"); }
SetAction의 매개변수는 아래와 같다.
clickEvent버튼이 눌렸을 때 실행시킬 액션delay버튼을 처음 눌렀을 때, 동작하기 전까지의 시간interval각 동작 사이의 시간 (동작 주기)이때 interval에 0을 넣으면 한번만 실행된다. 그래서 나는 1을 넣어주었다.
값을 바꿔가면서 적당한 동작 주기를 설정하면 될 것 같다.
끗~
'앱 개발 > Unity, C#' 카테고리의 다른 글
[Unity][C#] Event must be of a delegate type 에러 해결 (0) 2024.06.17 [Unity] 커서 모양 바꾸기 + 커서 회전하기 [How to rotate custom cursor texture] (2) 2024.02.18 [Unity][ClickEvent] 버튼 클릭 이벤트를 처리하는 n가지 방법 (0) 2024.02.18 [Unity][UI Toolkit] UI Document를 생성하고 Visual Tree Asset(uxml 파일) 적용시키기 (0) 2022.09.06 [Unity][UI Toolkit] vector 형식(svg 형식)으로 아이콘을 불러오는 방법 (0) 2022.09.04