체크박스 형태의 토글이 기본형이지만, 종종 버튼 스타일로 여러 개 버튼을 열거하고 하나의 버튼만 선택된 상태로 만들어야 할 때가 있습니다. 아래 정렬 스타일 버튼처럼.
GUILayout.Toggle() 함수를 활용하면 되는데, 적절한 메소드와 스타일을 넘겨줘야 합니다.
아래 두 가지 스타일의 코드 스니펫으로 활용할 수 있습니다.
Style A 는 문자열 상수로 GUIStyle 을 인자로 전달하고,
Style B 는 GUIStyle 을 명시적으로 생성해서 전달합니다.
// Style A
for (int i = 0; i < COUNT; i++)
{
bool isSelected = currentIndex == i;
bool newState = GUILayout.Toggle(isActive, $"Option {i}", "Button");
if (newState && !isSelected)
{
currentIndex = i;
// Do something
}
}
// Style B
for (int i = 0; i < COUNT; i++)
{
bool isSelected = currentIndex == i;
// 토글 버튼 스타일 적용
GUIStyle buttonStyle = new GUIStyle(GUI.skin.button);
if (isSelected)
{
buttonStyle.normal = buttonStyle.active; // 활성화된 상태 유지
}
// 토글 버튼 생성
if (GUILayout.Toggle(isSelected, $"Option {i}", buttonStyle))
{
currentIndex = i;
// Do something
}
}
이렇게 작성하면 체크박스처럼 동작하는 단일 선택 옵션 버튼을 생성할 수 있습니다.
https://docs.unity3d.com/6000.0/Documentation/ScriptReference/GUILayout.Toggle.html
Unity - Scripting API: GUILayout.Toggle
Declaration public static bool Toggle(bool value, Texture image, params GUILayoutOption[] options); Declaration public static bool Toggle(bool value, string text, params GUILayoutOption[] options); Declaration public static bool Toggle(bool value, GUIConte
docs.unity3d.com
728x90
반응형
'유니티 엔진 (Unity Engine)' 카테고리의 다른 글
[Unity Doc] Addressables: Planning and best practices 번역 (어드레서블 계획 및 모범 사례) (0) | 2025.03.16 |
---|---|
[Unity] Rigidbody, AddForce() 공식 문서 한글 번역 (2) | 2024.12.28 |
[Unity C#] 파일 경로 분리와 경로 구분자 변환: 두 가지 접근법 비교 (0) | 2024.12.06 |
유니티 엔진 라이트맵 베이커 Bakery의 Texels per unit 이란? (0) | 2024.06.03 |
[Unity] Timeline 을 구성하는 PlayableGraph의 Playable 구성 샘플 (0) | 2024.05.30 |
[Unity] ScriptableWizard 클래스로 간단하게 커스텀 툴, 에디터 창 만들기 (0) | 2024.05.15 |
[Unity] 후처리 효과 : 앰비언트 오클루젼 - Post Processing Effects: Ambient Occlusion (0) | 2024.05.14 |