Debug 클래스를 사용하면 프로젝트가 실행되는 동안 어떤 일이 일어나고 있는지 파악하거나 조사하는 데 도움이 되는 정보를 에디터에서 시각화할 수 있습니다. 예를 들어 콘솔 창에 메시지를 출력하고, 씬 뷰 및 게임 뷰에서 시각화 라인을 그리고, 스크립트에서 에디터의 플레이 모드를 일시 중지하는 데 사용할 수 있습니다.
이 페이지는 Debug 클래스의 개요, 그리고 이 클래스를 사용하는 스크립팅의 일반적인 용도에 대해 설명합니다. Debug 클래스의 모든 멤버에 대한 전체 레퍼런스는 디버그 스크립트 레퍼런스를 참조하십시오.
Unity 자체는 때때로 오류, 경고 및 메시지를 콘솔 창에 로깅합니다. Debug 클래스는 아래와 같이 자체 코드에서 동일한 작업을 수행할 수 있는 기능을 제공합니다.
Debug.Log("This is a log message.");
Debug.LogWarning("This is a warning message!");
Debug.LogError("This is an error message!");
세 가지 타입(오류, 경고, 메시지)은 각각 콘솔 창에서 고유한 아이콘 타입을 사용합니다.
Unity 또는 자체 코드를 통해 콘솔 창에 작성되는 모든 요소는 로그 파일에도 작성됩니다.
콘솔에서 Error Pause를 활성화하면 Debug 클래스를 통해 콘솔에 작성하는 오류로 인해 Unity의 플레이 모드가 일시 중지됩니다.
다음과 같이 이러한 로그 메서드에 두 번째 파라미터를 선택적으로 제공하여 메시지가 특정 게임 오브젝트와 연결되었음을 나타낼 수도 있습니다.
using UnityEngine;
public class DebugExample : MonoBehaviour
{ void Start()
{
Debug.LogWarning("I come in peace!", this.gameObject);
}
}
이렇게 하면 콘솔에서 메시지를 클릭할 때 해당 메시지와 연결된 게임 오브젝트가 계층 구조에서 강조 표시되므로, 메시지가 관련된 게임 오브젝트를 쉽게 식별할 수 있습니다. 아래 이미지에서 “I come in peace!” 경고 메시지를 클릭하면 “Alien (8)” 게임 오브젝트가 강조 표시됩니다.
또한 Debug 클래스는 씬 뷰와 게임 뷰에서 라인을 그릴 수 있는 두 가지 메서드, 즉 DrawLine과 DrawRay를 제공합니다.
이 예시에서는 씬의 모든 구체 게임 오브젝트에 스크립트가 추가되었으며, Debug.DrawLine을 사용하여 Y가 0인 평면으로부터의 수직 거리를 나타냅니다. 이 예시의 마지막 파라미터는 라인이 에디터에 표시되는 시간(초)입니다.
using UnityEngine;
public class DebugLineExample : MonoBehaviour
{
// 시작은 첫 번째 프레임 업데이트 전에 호출됨
void Start()
{
float height = transform.position.y;
Debug.DrawLine(transform.position, transform.position - Vector3.up * height, Color.magenta, 4);
}
}
씬 뷰에서 다음과 같은 결과물을 확인할 수 있습니다.
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.