NGMediaReceiveCallbackiOS.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. #if UNITY_EDITOR || UNITY_IOS
  2. using UnityEngine;
  3. namespace NativeGalleryNamespace
  4. {
  5. public class NGMediaReceiveCallbackiOS : MonoBehaviour
  6. {
  7. private static NGMediaReceiveCallbackiOS instance;
  8. private NativeGallery.MediaPickCallback callback;
  9. private NativeGallery.MediaPickMultipleCallback callbackMultiple;
  10. private float nextBusyCheckTime;
  11. public static bool IsBusy { get; private set; }
  12. [System.Runtime.InteropServices.DllImport( "__Internal" )]
  13. private static extern int _NativeGallery_IsMediaPickerBusy();
  14. public static void Initialize( NativeGallery.MediaPickCallback callback, NativeGallery.MediaPickMultipleCallback callbackMultiple )
  15. {
  16. if( IsBusy )
  17. return;
  18. if( instance == null )
  19. {
  20. instance = new GameObject( "NGMediaReceiveCallbackiOS" ).AddComponent<NGMediaReceiveCallbackiOS>();
  21. DontDestroyOnLoad( instance.gameObject );
  22. }
  23. instance.callback = callback;
  24. instance.callbackMultiple = callbackMultiple;
  25. instance.nextBusyCheckTime = Time.realtimeSinceStartup + 1f;
  26. IsBusy = true;
  27. }
  28. private void Update()
  29. {
  30. if( IsBusy )
  31. {
  32. if( Time.realtimeSinceStartup >= nextBusyCheckTime )
  33. {
  34. nextBusyCheckTime = Time.realtimeSinceStartup + 1f;
  35. if( _NativeGallery_IsMediaPickerBusy() == 0 )
  36. {
  37. IsBusy = false;
  38. NativeGallery.MediaPickCallback _callback = callback;
  39. callback = null;
  40. NativeGallery.MediaPickMultipleCallback _callbackMultiple = callbackMultiple;
  41. callbackMultiple = null;
  42. if( _callback != null )
  43. _callback( null );
  44. if( _callbackMultiple != null )
  45. _callbackMultiple( null );
  46. }
  47. }
  48. }
  49. }
  50. public void OnMediaReceived( string path )
  51. {
  52. IsBusy = false;
  53. if( string.IsNullOrEmpty( path ) )
  54. path = null;
  55. NativeGallery.MediaPickCallback _callback = callback;
  56. callback = null;
  57. if( _callback != null )
  58. _callback( path );
  59. }
  60. public void OnMultipleMediaReceived( string paths )
  61. {
  62. IsBusy = false;
  63. string[] _paths = SplitPaths( paths );
  64. if( _paths != null && _paths.Length == 0 )
  65. _paths = null;
  66. NativeGallery.MediaPickMultipleCallback _callbackMultiple = callbackMultiple;
  67. callbackMultiple = null;
  68. if( _callbackMultiple != null )
  69. _callbackMultiple( _paths );
  70. }
  71. private string[] SplitPaths( string paths )
  72. {
  73. string[] result = null;
  74. if( !string.IsNullOrEmpty( paths ) )
  75. {
  76. string[] pathsSplit = paths.Split( '>' );
  77. int validPathCount = 0;
  78. for( int i = 0; i < pathsSplit.Length; i++ )
  79. {
  80. if( !string.IsNullOrEmpty( pathsSplit[i] ) )
  81. validPathCount++;
  82. }
  83. if( validPathCount == 0 )
  84. pathsSplit = new string[0];
  85. else if( validPathCount != pathsSplit.Length )
  86. {
  87. string[] validPaths = new string[validPathCount];
  88. for( int i = 0, j = 0; i < pathsSplit.Length; i++ )
  89. {
  90. if( !string.IsNullOrEmpty( pathsSplit[i] ) )
  91. validPaths[j++] = pathsSplit[i];
  92. }
  93. pathsSplit = validPaths;
  94. }
  95. result = pathsSplit;
  96. }
  97. return result;
  98. }
  99. }
  100. }
  101. #endif