Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
Tags
- Spring Boot
- Camera Movement
- --watch
- linux
- express
- java
- unity
- Digital Ocean
- react
- Camera Zoom
- critical rendering path
- spread 연산자
- OverTheWire
- SDK upgrade
- Google Refund
- Google Developer API
- Git
- server
- draganddrop
- nodejs
- docker
- springboot
- MySQL
- screencapture
- Unity Editor
- css framework
- Packet Network
- Unity IAP
- rpg server
- mongoDB
Archives
- Today
- Total
우당탕탕 개발일지
[Unity] 커스텀에디터 - ReorderableList 사용하기 본문
커스텀 에디터를 사용하다보면, 리스트를 생성할때가 굉장히 많다. 리스트를 제작할때마다 필요하다고 느낀 기능들이 있다. ReorderableList는 UnityEditorInternal 네임스페이스에 포함되어 있는 강력한 유틸리티로, 리스트를 깔끔하고 편리하게 표시할 수 있게 해준다.
- +, - 버튼으로 삭제/추가하기
- 드래그를 이용해 리스트 순서 변경하기
using UnityEditorInternal; // 필수
✅ 기본 사용법 예제
SerializedProperty listProperty;
ReorderableList reorderableList;
void OnEnable()
{
listProperty = serializedObject.FindProperty("myList");
reorderableList = new ReorderableList(serializedObject, listProperty, true, true, true, true);
reorderableList.drawHeaderCallback = DrawHeader;
reorderableList.drawElementCallback = DrawElement;
reorderableList.onAddCallback = OnAdd;
}
public override void OnInspectorGUI()
{
serializedObject.Update();
reorderableList.DoLayoutList();
serializedObject.ApplyModifiedProperties();
}
void DrawHeader(Rect rect)
{
EditorGUI.LabelField(rect, "My List");
}
void DrawElement(Rect rect, int index, bool isActive, bool isFocused)
{
var element = listProperty.GetArrayElementAtIndex(index);
EditorGUI.PropertyField(rect, element, GUIContent.none);
}
void OnAdd(ReorderableList list)
{
listProperty.arraySize++;
}
기본적으로는 한 요소당 한줄만 차지하는데, 요소가 많아질 경우 세로로 요소를 넣어야 하는 경우가 많다.
id, grade, upType, value 총 4개의 요소로 이루어져있을 경우 세로로 나열하는 방법은 다음과 같다.
요소가 한줄보다 길어질 경우, rect의 높이를 계산해서 지정해줘야한다.
예시 구조 (ID, Grade, UpType, Value)
void DrawElement(Rect rect, int index, bool isActive, bool isFocused)
{
var element = listProperty.GetArrayElementAtIndex(index);
rect.y += 4;
var boxRect = new Rect(rect.x, rect.y, rect.width, EditorGUIUtility.singleLineHeight * 5);
GUI.Box(boxRect, GUIContent.none);
rect.x += 10;
rect.y += 8;
rect.width -= 20;
// ID
EditorGUI.PropertyField(new Rect(rect.x, rect.y, rect.width, EditorGUIUtility.singleLineHeight),
element.FindPropertyRelative("id"), new GUIContent("ID"));
rect.y += EditorGUIUtility.singleLineHeight + 4;
// Grade
EditorGUI.PropertyField(new Rect(rect.x, rect.y, rect.width, EditorGUIUtility.singleLineHeight),
element.FindPropertyRelative("grade"), new GUIContent("Grade"));
rect.y += EditorGUIUtility.singleLineHeight + 4;
// UpType (드롭다운 예시)
DrawUpTypeDropdown(new Rect(rect.x, rect.y, rect.width, EditorGUIUtility.singleLineHeight),
element.FindPropertyRelative("upType"));
rect.y += EditorGUIUtility.singleLineHeight + 4;
// Value
EditorGUI.PropertyField(new Rect(rect.x, rect.y, rect.width, EditorGUIUtility.singleLineHeight),
element.FindPropertyRelative("value"), new GUIContent("Value"));
}
// 드롭다운 함수 예시
void DrawUpTypeDropdown(Rect rect, SerializedProperty property)
{
BattleUpgradeEnum[] enumValues = BattleUpgradeTypeDescriptions.GetAvailableTypes();
string[] descriptions = new string[enumValues.Length];
for (int i = 0; i < enumValues.Length; i++)
descriptions[i] = BattleUpgradeTypeDescriptions.GetDescription(enumValues[i]);
int currentIndex = System.Array.IndexOf(enumValues, (BattleUpgradeEnum)property.intValue);
int selectedIndex = EditorGUI.Popup(rect, "강화 타입", currentIndex, descriptions);
if (selectedIndex != currentIndex)
property.intValue = (int)enumValues[selectedIndex];
}
'Unity' 카테고리의 다른 글
[Unity] GPGS 2.0.0 로그인 에러 해결 (1) | 2025.06.13 |
---|---|
[Unity] 모바일 디바이스 노치 영역 처리하기 (1) | 2025.04.24 |
[Unity IOS] 빌드에러 Building Library/Bee/artifacts/iOS/ManagedStripped failed (0) | 2025.04.24 |
[커스텀 에디터] 파일 이름에서 ID추출 & 자동 ID부여하기 (0) | 2025.04.08 |
[Unity] CustomEditor - Dictionary 사용하기 (0) | 2025.03.17 |