Unity에서 Handles.Label()을 활용한 씬 뷰 디버깅

📌 Handles.Label()란?

Handles.Label()씬(Scene) 뷰에서 특정 위치에 텍스트를 표시할 수 있는 함수입니다. 

Handles.Label()에디터 전용 기능입니다.


기본적인 Handles.Label() 사용법

using UnityEngine;
using UnityEditor; // Handles를 사용하기 위해 필요

public class HandlesLabelExample : MonoBehaviour
{
    void OnDrawGizmos()
    {
        Handles.Label(transform.position, "여기가 내 위치!");
    }
}📋

씬(Scene) 창에서 오브젝트의 위치에 "여기가 내 위치!"라는 텍스트가 표시됩니다.

public static void Label(Vector3 position, string text);
public static void Label(Vector3 position, string text, GUIStyle style);📋

 


GUI 스타일 적용 (글씨 크기 및 색상 조정)

void OnDrawGizmos()
{
    GUIStyle style = new GUIStyle();
    style.fontSize = 16; // 글씨 크기 설정
    style.normal.textColor = Color.white; // 글씨 색상 설정
    
    Handles.Label(transform.position + Vector3.up * 2, "오브젝트 정보", style);
}📋

위 코드를 실행하면 오브젝트 위 2m 지점에 "오브젝트 정보" 텍스트가 흰색으로 표시됩니다.


예시) 적 AI의 시야각과 플레이어 방향 표시

🎯 목표: 적이 플레이어를 감지할 때, 씬 뷰에서 각도를 텍스트로 표시하기

using UnityEngine;
using UnityEditor;

public class EnemyAI : MonoBehaviour
{
    public float detectionRange = 10f;
    public float fieldOfView = 60f;
    public Transform player;

    void OnDrawGizmos()
    {
        if (player == null) return;
        
        Vector3 directionToPlayer = player.position - transform.position;
        float angle = Vector3.Angle(transform.forward, directionToPlayer);
        
        // 씬 뷰에 플레이어와의 각도 표시
        #if UNITY_EDITOR
        GUIStyle style = new GUIStyle();
        style.fontSize = 16;
        style.normal.textColor = Color.yellow;
        Vector3 textPosition = transform.position + Vector3.up * 1; 
        Handles.Label(textPosition, $"각도: {angle:F2}°", style);
        #endif
    }
}📋

씬 창에서 AI가 플레이어를 감지할 때, 플레이어까지의 각도가 표시됩니다