Application.logMessageReceived에 메서드를 추가하면 로그가 발생할 경우 callback 함수를 불러올 수 있다.
매번 로그에 추가 작업을 하지 않아도 되기 때문에 디버깅을 할 때 도움이 된다.
Unity - Scripting API: Application.logMessageReceived
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
토스트 메시지를 만든다고 가정하자.
↑ 링크의 토스트 메시지를 사용하기 위해서는 매번 ToastMsg.Instance를 불러와야 했다.
public void OnMouseUp()
{
string message = "click " + ++clickCount;
ToastMsg.Instrance.showMessage(message, 1.0f);
}
하지만 모든 로그에 대해 항상 토스트 메시지를 사용하고 싶다면 토스트 메시지 이벤트를 등록하면 된다.
예를 들어 오브젝트에 있는 ToastClick.cs가 아래와 같다고 하자.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ToastClick : MonoBehaviour
{
int clickCount;
public void OnMouseUp()
{
string message = "click " + ++clickCount;
ToastMsg.Instance.showMessage(message, 1.0f);
}
}
handleLog 함수를 추가하여 OnEnable에 등록하고, OnDisable에서는 삭제하도록 코드를 수정한다.
handleLog 내부에서 ToastMsg.Instance.showMessage를 추가하면 된다.
그리고 기존의 ToastMsg.Instance.showMessage는 Debug.Log로 대체한다.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ToastClick : MonoBehaviour
{
string output = "";
string stack = "";
void OnEnable()
{
Application.logMessageReceived += handleLog;
}
void OnDisable()
{
Application.logMessageReceived -= handleLog;
}
void handleLog(string logString, string stackTrace, LogType type)
{
output = logString;
stack = stackTrace;
//Debug.Log("output : " + output);
//Debug.Log("stackTrace : " + stackTrace);
//Debug.Log("LogType : " + type);
ToastMsg.Instance.showMessage(output, 1.0f);
}
int clickCount;
public void OnMouseUp()
{
string message = "click " + ++clickCount;
Debug.Log(message);
//ToastMsg.Instance.showMessage(message, 1.0f);
}
}
handleLog로 넘어오는 parameter로 logString, stackTrace, type이 있다.
참고로 LogType은 아래와 같이 5개로 구분된다.
Unity Plus:
Easy 2D, 3D, VR, & AR software for cross-platform development of games and mobile apps. - Unity Store
Have a 2D, 3D, VR, or AR project that needs cross-platform functionality? We can help. Take a look at the easy-to-use Unity Plus real-time dev platform!
store.unity.com
Unity Pro:
Unity Pro
The complete solutions for professionals to create and operate.
unity.com
Unity 프리미엄 학습:
Unity Learn
Advance your Unity skills with live sessions and over 750 hours of on-demand learning content designed for creators at every skill level.
unity.com
'개발 > Unity' 카테고리의 다른 글
유니티 - 윈도우 빌드 해상도, 창모드 설정하기 (0) | 2022.08.24 |
---|---|
유니티 UI - PlayerPrefs로 드롭다운 목록 관리하기 (Control TextMeshPro Dropdown with PlayerPrefs) (0) | 2022.08.10 |
유니티 - Additive Scene으로 여러 개의 씬 편집하기 (0) | 2022.08.09 |
유니티 - Editor.log에서 빌드에 포함된 리소스 확인하기 (0) | 2022.08.06 |
유니티 C# - 문자열 합치기 (0) | 2022.08.01 |
댓글