概要
using System; using UnityEngine; public class Example : MonoBehaviour { public T Hoge<T>( string json ) { var isError = false; void OnReceive( string condition, string trace, LogType type ) { if ( type != LogType.Error && type != LogType.Assert && type != LogType.Exception ) return; isError = true; Application.logMessageReceivedThreaded -= OnReceive; } Application.logMessageReceivedThreaded += OnReceive; var result = JsonUtility.FromJson<T>( json ); Application.logMessageReceivedThreaded -= OnReceive; if ( isError ) { throw new Exception(); } return result; } }
- JsonUtility で変換失敗時に発生する「Unexpected node type.」は
例外ではなくエラーログのため try~catch できない - 例外として扱いたい場合は上記のコードのように
Application.logMessageReceivedThreaded を使用する