DebugLogLogcatListener.cs 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #if !UNITY_EDITOR && UNITY_ANDROID
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. // Credit: https://stackoverflow.com/a/41018028/2373034
  5. namespace IngameDebugConsole
  6. {
  7. public class DebugLogLogcatListener : AndroidJavaProxy
  8. {
  9. private Queue<string> queuedLogs;
  10. private AndroidJavaObject nativeObject;
  11. public DebugLogLogcatListener() : base( "com.yasirkula.unity.DebugConsoleLogcatLogReceiver" )
  12. {
  13. queuedLogs = new Queue<string>( 16 );
  14. }
  15. ~DebugLogLogcatListener()
  16. {
  17. Stop();
  18. if( nativeObject != null )
  19. nativeObject.Dispose();
  20. }
  21. public void Start( string arguments )
  22. {
  23. if( nativeObject == null )
  24. nativeObject = new AndroidJavaObject( "com.yasirkula.unity.DebugConsoleLogcatLogger" );
  25. nativeObject.Call( "Start", this, arguments );
  26. }
  27. public void Stop()
  28. {
  29. if( nativeObject != null )
  30. nativeObject.Call( "Stop" );
  31. }
  32. public void OnLogReceived( string log )
  33. {
  34. queuedLogs.Enqueue( log );
  35. }
  36. public string GetLog()
  37. {
  38. if( queuedLogs.Count > 0 )
  39. return queuedLogs.Dequeue();
  40. return null;
  41. }
  42. }
  43. }
  44. #endif