Ejemplo n.º 1
0
    public static void OpenWifiSettings()
    {
#if !UNITY_EDITOR && UNITY_ANDROID
        AJC.CallStatic("OpenWifiSettings", Context);
#elif !UNITY_EDITOR && UNITY_IOS
#endif
    }
    public static Permission PickMultipleFiles(MultipleFilesPickedCallback callback, string[] allowedFileTypes)
    {
        if (allowedFileTypes == null || allowedFileTypes.Length == 0)
        {
            throw new ArgumentException("Parameter 'allowedFileTypes' is null or empty!");
        }

        Permission result = RequestPermission(true);

        if (result == Permission.Granted && !IsFilePickerBusy())
        {
            if (CanPickMultipleFiles())
            {
#if !UNITY_EDITOR && UNITY_ANDROID
                AJC.CallStatic("PickFiles", Context, new FPResultCallbackAndroid(null, callback, null), true, SelectedFilePath, allowedFileTypes, "");
#elif !UNITY_EDITOR && UNITY_IOS
                FPResultCallbackiOS.Initialize(null, callback, null);
                _NativeFilePicker_PickMultipleFiles(allowedFileTypes, allowedFileTypes.Length);
#endif
            }
            else if (callback != null)
            {
                callback(null);
            }
        }

        return(result);
    }
Ejemplo n.º 3
0
    public static Permission TakePicture(CameraCallback callback, int maxSize = -1, bool saveAsJPEG = true, PreferredCamera preferredCamera = PreferredCamera.Default)
    {
        Permission result = RequestPermission();

        if (result == Permission.Granted && !IsCameraBusy())
        {
#if UNITY_EDITOR
            string pickedFile = UnityEditor.EditorUtility.OpenFilePanelWithFilters("Select image", "", new string[] { "Image files", "png,jpg,jpeg", "All files", "*" });

            if (callback != null)
            {
                callback(pickedFile != "" ? pickedFile : null);
            }
#elif UNITY_ANDROID
            AJC.CallStatic("TakePicture", Context, new NCCameraCallbackAndroid(callback), (int)preferredCamera);
#elif UNITY_IOS
            if (maxSize <= 0)
            {
                maxSize = SystemInfo.maxTextureSize;
            }

            NCCameraCallbackiOS.Initialize(callback);
            _NativeCamera_TakePicture(IOSSelectedImagePath + (saveAsJPEG ? ".jpeg" : ".png"), maxSize, (int)preferredCamera);
#else
            if (callback != null)
            {
                callback(null);
            }
#endif
        }

        return(result);
    }
Ejemplo n.º 4
0
    public static Permission RecordVideo(CameraCallback callback, Quality quality = Quality.Default, int maxDuration = 0, long maxSizeBytes = 0L, PreferredCamera preferredCamera = PreferredCamera.Default)
    {
        Permission result = RequestPermission();

        if (result == Permission.Granted && !IsCameraBusy())
        {
#if UNITY_EDITOR
            string pickedFile = UnityEditor.EditorUtility.OpenFilePanelWithFilters("Select video", "", new string[] { "Video files", "mp4,mov,wav,avi", "All files", "*" });

            if (callback != null)
            {
                callback(pickedFile != "" ? pickedFile : null);
            }
#elif UNITY_ANDROID
            AJC.CallStatic("RecordVideo", Context, new NCCameraCallbackAndroid(callback), (int)preferredCamera, (int)quality, maxDuration, maxSizeBytes);
#elif UNITY_IOS
            NCCameraCallbackiOS.Initialize(callback);
            _NativeCamera_RecordVideo((int)quality, maxDuration, (int)preferredCamera);
#else
            if (callback != null)
            {
                callback(null);
            }
#endif
        }

        return(result);
    }
Ejemplo n.º 5
0
    public static ImageProperties GetImageProperties(string imagePath)
    {
        if (!File.Exists(imagePath))
        {
            throw new FileNotFoundException("File not found at " + imagePath);
        }

        string value = null;

#if !UNITY_EDITOR && UNITY_ANDROID
        value = AJC.CallStatic <string>("GetImageProperties", imagePath);
#elif !UNITY_EDITOR && UNITY_IOS
        value = _NativeGallery_GetImageProperties(imagePath);
#endif

        ImageProperties result = new ImageProperties();
        if (!string.IsNullOrEmpty(value))
        {
            string[] properties = value.Split('>');
            if (properties != null && properties.Length >= 3)
            {
                int width, height;
                if (int.TryParse(properties[0], out width))
                {
                    result.width = width;
                }
                if (int.TryParse(properties[1], out height))
                {
                    result.height = height;
                }

                if (!string.IsNullOrEmpty(properties[2]))
                {
                    result.mimeType = properties[2];
                }
                else
                {
                    String extension = Path.GetExtension(imagePath);
                    if (extension == ".png")
                    {
                        result.mimeType = "image/png";
                    }
                    else if (extension == ".jpg" || extension == ".jpeg")
                    {
                        result.mimeType = "image/jpeg";
                    }
                    else if (extension == ".gif")
                    {
                        result.mimeType = "image/gif";
                    }
                    else if (extension == ".bmp")
                    {
                        result.mimeType = "image/bmp";
                    }
                }
            }
        }

        return(result);
    }
Ejemplo n.º 6
0
    public static Texture2D LoadImageAtPath(string imagePath, int maxSize = -1, bool markTextureNonReadable = true,
                                            bool generateMipmaps          = true, bool linearColorSpace = false)
    {
        if (string.IsNullOrEmpty(imagePath))
        {
            throw new ArgumentException("Parameter 'imagePath' is null or empty!");
        }

        if (!File.Exists(imagePath))
        {
            throw new FileNotFoundException("File not found at " + imagePath);
        }

        if (maxSize <= 0)
        {
            maxSize = SystemInfo.maxTextureSize;
        }

#if !UNITY_EDITOR && UNITY_ANDROID
        string loadPath = AJC.CallStatic <string>("LoadImageAtPath", Context, imagePath, TemporaryImagePath, maxSize);
#elif !UNITY_EDITOR && UNITY_IOS
        string loadPath = _NativeGallery_LoadImageAtPath(imagePath, TemporaryImagePath, maxSize);
#else
        string loadPath = imagePath;
#endif

        String        extension = Path.GetExtension(imagePath).ToLowerInvariant();
        TextureFormat format    = (extension == ".jpg" || extension == ".jpeg") ? TextureFormat.RGB24 : TextureFormat.RGBA32;

        Texture2D result = new Texture2D(default, default, format, generateMipmaps, linearColorSpace);
Ejemplo n.º 7
0
    public static Permission TakePicture(CameraCallback callback, int maxSize = -1, bool saveAsJPEG = true, PreferredCamera preferredCamera = PreferredCamera.Default)
    {
        Permission result = RequestPermission();

        if (result == Permission.Granted && !IsCameraBusy())
        {
#if !UNITY_EDITOR && UNITY_ANDROID
            AJC.CallStatic("TakePicture", Context, new NCCameraCallbackAndroid(callback), (int)preferredCamera);
#elif !UNITY_EDITOR && UNITY_IOS
            if (maxSize <= 0)
            {
                maxSize = SystemInfo.maxTextureSize;
            }

            NCCameraCallbackiOS.Initialize(callback);
            _NativeCamera_TakePicture(IOSSelectedImagePath + (saveAsJPEG ? ".jpeg" : ".png"), maxSize, (int)preferredCamera);
#else
            if (callback != null)
            {
                callback(null);
            }
#endif
        }

        return(result);
    }
Ejemplo n.º 8
0
    public static Permission TakePicture(CameraCallback callback, int maxSize = -1)
    {
        Permission result = RequestPermission();

        if (result == Permission.Granted && !IsCameraBusy())
        {
#if !UNITY_EDITOR && UNITY_ANDROID
            AJC.CallStatic("TakePicture", Context, new NCCameraCallbackAndroid(callback));
#elif !UNITY_EDITOR && UNITY_IOS
            if (maxSize <= 0)
            {
                maxSize = SystemInfo.maxTextureSize;
            }

            NCCameraCallbackiOS.Initialize(callback);
            _NativeCamera_TakePicture(IOSSelectedImagePath, maxSize);
#else
            if (callback != null)
            {
                callback(null);
            }
#endif
        }

        return(result);
    }
Ejemplo n.º 9
0
    public static bool FindTarget(out string androidPackageName, out string androidClassName, string packageNameRegex, string classNameRegex = null)
    {
        androidPackageName = null;
        androidClassName   = null;
        if (string.IsNullOrEmpty(packageNameRegex))
        {
            return(false);
        }
        if (classNameRegex == null)
        {
            classNameRegex = string.Empty;
        }
        string text = AJC.CallStatic <string>("FindMatchingTarget", new object[3]
        {
            Context,
            packageNameRegex,
            classNameRegex
        });

        if (string.IsNullOrEmpty(text))
        {
            return(false);
        }
        int num = text.IndexOf('>');

        if (num <= 0 || num >= text.Length - 1)
        {
            return(false);
        }
        androidPackageName = text.Substring(0, num);
        androidClassName   = text.Substring(num + 1);
        return(true);
    }
Ejemplo n.º 10
0
    public void Share()
    {
        if (files.Count == 0 && subject.Length == 0 && text.Length == 0 && url.Length == 0)
        {
            Debug.LogWarning("Share Error: attempting to share nothing!");
            return;
        }

#if UNITY_EDITOR
        Debug.Log("Shared!");

        if (callback != null)
        {
            callback(ShareResult.Shared, null);
        }
#elif UNITY_ANDROID
        AJC.CallStatic("Share", Context, new NSShareResultCallbackAndroid(callback), targetPackages.ToArray(), targetClasses.ToArray(), files.ToArray(), mimes.ToArray(), subject, CombineURLWithText(), title);
#elif UNITY_IOS
        NSShareResultCallbackiOS.Initialize(callback);
        if (files.Count == 0)
        {
            _NativeShare_Share(new string[0], 0, subject, text, GetURLWithScheme());
        }
        else
        {
            // While sharing both a URL and a file, some apps either don't show up in share sheet or omit the file
            // If we append URL to text, the issue is resolved for at least some of these apps
            _NativeShare_Share(files.ToArray(), files.Count, subject, CombineURLWithText(), "");
        }
#else
        Debug.LogWarning("NativeShare is not supported on this platform!");
#endif
    }
    public static Permission[] CheckPermissions(params string[] permissions)
    {
        ValidateArgument(permissions);

#if IS_ANDROID_PLATFORM
        string resultRaw = AJC.CallStatic <string>("CheckPermission", permissions, Context);
        if (resultRaw.Length != permissions.Length)
        {
            Debug.LogError("CheckPermissions: something went wrong");
            return(null);
        }

        Permission[] result = new Permission[permissions.Length];
        for (int i = 0; i < result.Length; i++)
        {
            Permission _permission = resultRaw[i].ToPermission();
            if (_permission == Permission.Denied && GetCachedPermission(permissions[i], Permission.ShouldAsk) != Permission.Denied)
            {
                _permission = Permission.ShouldAsk;
            }

            result[i] = _permission;
        }

        return(result);
#else
        return(GetDummyResult(permissions));
#endif
    }
    public static Permission ExportMultipleFiles(string[] filePaths, FilesExportedCallback callback = null)
    {
        if (filePaths == null || filePaths.Length == 0)
        {
            throw new ArgumentException("Parameter 'filePaths' is null or empty!");
        }

        Permission result = RequestPermission(false);

        if (result == Permission.Granted && !IsFilePickerBusy())
        {
            if (CanExportMultipleFiles())
            {
#if !UNITY_EDITOR && UNITY_ANDROID
                AJC.CallStatic("ExportFiles", Context, new FPResultCallbackAndroid(null, null, callback), filePaths, filePaths.Length);
#elif !UNITY_EDITOR && UNITY_IOS
                FPResultCallbackiOS.Initialize(null, null, callback);
                _NativeFilePicker_ExportFiles(filePaths, filePaths.Length);
#endif
            }
            else if (callback != null)
            {
                callback(false);
            }
        }

        return(result);
    }
Ejemplo n.º 13
0
    private static Permission GetMediaFromGallery(MediaPickCallback callback, bool imageMode, string mime, string title, int maxSize)
    {
        Permission result = RequestPermission(true);

        if (result == Permission.Granted && !IsMediaPickerBusy())
        {
#if !UNITY_EDITOR && UNITY_ANDROID
            AJC.CallStatic("PickMedia", Context, new NGMediaReceiveCallbackAndroid(callback, null), imageMode, false, mime, title);
#elif !UNITY_EDITOR && UNITY_IOS
            NGMediaReceiveCallbackiOS.Initialize(callback);
            if (imageMode)
            {
                if (maxSize <= 0)
                {
                    maxSize = SystemInfo.maxTextureSize;
                }

                _NativeGallery_PickImage(IOSSelectedImagePath, maxSize);
            }
            else
            {
                _NativeGallery_PickVideo();
            }
#else
            if (callback != null)
            {
                callback(null);
            }
#endif
        }

        return(result);
    }
Ejemplo n.º 14
0
    private void StartSharing()
    {
        // AddFile(filepath);
        //SetSubject("Particle Jump!!");
        //SetText("Jump Jump Shoot!\nCan you beat this score?");

        //Debug.Log("Coroutine Complete");

        //  Debug.Log(files.Count);
        //  Debug.Log(subject.Length);
        //  Debug.Log(text.Length);

        if (files.Count == 0 && subject.Length == 0 && text.Length == 0)
        {
            Debug.LogWarning("Share Error: attempting to share nothing!");
            Debug.Log("Attempting to share nothing");
            return;
        }

        //Debug.Log("Passed here");

#if UNITY_EDITOR
        Debug.Log("Shared!");
#elif UNITY_ANDROID
        AJC.CallStatic("Share", Context, targetPackage, targetClass, files.ToArray(), mimes.ToArray(), subject, text, title);
#elif UNITY_IOS
        _NativeShare_Share(files.ToArray(), files.Count, subject, text);
#else
        Debug.Log("No sharing set up for this platform.");
#endif
    }
Ejemplo n.º 15
0
    public static Texture2D LoadImageAtPath(string imagePath, int maxSize = -1, bool markTextureNonReadable = true,
                                            bool generateMipmaps          = true, bool linearColorSpace = false)
    {
        if (string.IsNullOrEmpty(imagePath))
        {
            throw new ArgumentException("Parameter 'imagePath' is null or empty!");
        }

        if (!File.Exists(imagePath))
        {
            throw new FileNotFoundException("File not found at " + imagePath);
        }

        if (maxSize <= 0)
        {
            maxSize = SystemInfo.maxTextureSize;
        }

#if !UNITY_EDITOR && UNITY_ANDROID
        string loadPath = AJC.CallStatic <string>("LoadImageAtPath", Context, imagePath, TemporaryImagePath, maxSize);
#elif !UNITY_EDITOR && UNITY_IOS
        string loadPath = _NativeGallery_LoadImageAtPath(imagePath, TemporaryImagePath, maxSize);
#else
        string loadPath = imagePath;
#endif

        String        extension = Path.GetExtension(imagePath).ToLowerInvariant();
        TextureFormat format    = (extension == ".jpg" || extension == ".jpeg") ? TextureFormat.RGB24 : TextureFormat.RGBA32;

        Texture2D result = new Texture2D(2, 2, format, generateMipmaps, linearColorSpace);

        try
        {
            if (!result.LoadImage(File.ReadAllBytes(loadPath), markTextureNonReadable))
            {
                Object.DestroyImmediate(result);
                return(null);
            }
        }
        catch (Exception e)
        {
            Debug.LogException(e);

            Object.DestroyImmediate(result);
            return(null);
        }
        finally
        {
            if (loadPath != imagePath)
            {
                try
                {
                    File.Delete(loadPath);
                }
                catch { }
            }
        }

        return(result);
    }
Ejemplo n.º 16
0
    public static Permission RequestPermission(PermissionType permissionType)
    {
#if !UNITY_EDITOR && UNITY_ANDROID
        object threadLock = new object();
        lock ( threadLock )
        {
            NGPermissionCallbackAndroid nativeCallback = new NGPermissionCallbackAndroid(threadLock);

            AJC.CallStatic("RequestPermission", Context, nativeCallback, permissionType == PermissionType.Read, PlayerPrefs.GetInt("NativeGalleryPermission", (int)Permission.ShouldAsk));

            if (nativeCallback.Result == -1)
            {
                System.Threading.Monitor.Wait(threadLock);
            }

            if ((Permission)nativeCallback.Result != Permission.ShouldAsk && PlayerPrefs.GetInt("NativeGalleryPermission", -1) != nativeCallback.Result)
            {
                PlayerPrefs.SetInt("NativeGalleryPermission", nativeCallback.Result);
                PlayerPrefs.Save();
            }

            return((Permission)nativeCallback.Result);
        }
#elif !UNITY_EDITOR && UNITY_IOS
        // result == 3: LimitedAccess permission on iOS, no need to handle it when PermissionFreeMode is set to true
        int result = _NativeGallery_RequestPermission(permissionType == PermissionType.Read ? 1 : 0, PermissionFreeMode ? 1 : 0);
        return(result == 3 ? Permission.Granted : (Permission)result);
#else
        return(Permission.Granted);
#endif
    }
Ejemplo n.º 17
0
    private static Permission GetMultipleMediaFromGallery(MediaPickMultipleCallback callback, MediaType mediaType, string mime, string title)
    {
        Permission result = RequestPermission(true);

        if (result == Permission.Granted && !IsMediaPickerBusy())
        {
            if (CanSelectMultipleFilesFromGallery())
            {
#if !UNITY_EDITOR && UNITY_ANDROID
                AJC.CallStatic("PickMedia", Context, new NGMediaReceiveCallbackAndroid(null, callback), (int)mediaType, true, SelectedMediaPath, mime, title);
#elif !UNITY_EDITOR && UNITY_IOS
                Debug.LogError("Picking multiple media is not supported on iOS");

                if (callback != null)
                {
                    callback(null);
                }
#else
                if (callback != null)
                {
                    callback(null);
                }
#endif
            }
            else if (callback != null)
            {
                callback(null);
            }
        }

        return(result);
    }
Ejemplo n.º 18
0
    public static Permission RequestPermission(bool readPermissionOnly = false)
    {
#if !UNITY_EDITOR && UNITY_ANDROID
        object threadLock = new object();
        lock ( threadLock )
        {
            NGPermissionCallbackAndroid nativeCallback = new NGPermissionCallbackAndroid(threadLock);

            AJC.CallStatic("RequestPermission", Context, nativeCallback, readPermissionOnly, PlayerPrefs.GetInt("NativeGalleryPermission", (int)Permission.ShouldAsk));

            if (nativeCallback.Result == -1)
            {
                System.Threading.Monitor.Wait(threadLock);
            }

            if ((Permission)nativeCallback.Result != Permission.ShouldAsk && PlayerPrefs.GetInt("NativeGalleryPermission", -1) != nativeCallback.Result)
            {
                PlayerPrefs.SetInt("NativeGalleryPermission", nativeCallback.Result);
                PlayerPrefs.Save();
            }

            return((Permission)nativeCallback.Result);
        }
#elif !UNITY_EDITOR && UNITY_IOS
        return((Permission)_NativeGallery_RequestPermission());
#else
        return(Permission.Granted);
#endif
    }
Ejemplo n.º 19
0
    private static Permission GetMediaFromGallery(MediaPickCallback callback, MediaType mediaType, string mime, string title)
    {
        Permission result = RequestPermission(true);

        if (result == Permission.Granted && !IsMediaPickerBusy())
        {
#if !UNITY_EDITOR && UNITY_ANDROID
            AJC.CallStatic("PickMedia", Context, new NGMediaReceiveCallbackAndroid(callback, null), (int)mediaType, false, SelectedMediaPath, mime, title);
#elif !UNITY_EDITOR && UNITY_IOS
            if (mediaType == MediaType.Audio)
            {
                Debug.LogError("Picking audio files is not supported on iOS");

                if (callback != null)                  // Selecting audio files is not supported on iOS
                {
                    callback(null);
                }
            }
            else
            {
                NGMediaReceiveCallbackiOS.Initialize(callback);
                _NativeGallery_PickMedia(SelectedMediaPath, (int)(mediaType & ~MediaType.Audio));
            }
#else
            if (callback != null)
            {
                callback(null);
            }
#endif
        }

        return(result);
    }
Ejemplo n.º 20
0
    private static void SaveToGalleryInternal(string path, string album, bool isImage, MediaSaveCallback callback)
    {
#if !UNITY_EDITOR && UNITY_ANDROID
        AJC.CallStatic("MediaScanFile", Context, path);

        if (callback != null)
        {
            callback(null);
        }

        Debug.Log("Saving to gallery: " + path);
#elif !UNITY_EDITOR && UNITY_IOS
        NGMediaSaveCallbackiOS.Initialize(callback);
        if (isImage)
        {
            _NativeGallery_ImageWriteToAlbum(path, album);
        }
        else
        {
            _NativeGallery_VideoWriteToAlbum(path, album);
        }

        Debug.Log("Saving to Pictures: " + Path.GetFileName(path));
#else
        if (callback != null)
        {
            callback(null);
        }
#endif
    }
Ejemplo n.º 21
0
    private static Permission GetMultipleMediaFromGallery(MediaPickMultipleCallback callback, bool imageMode, string mime, string title, int maxSize)
    {
        Permission result = RequestPermission(true);

        if (result == Permission.Granted && !IsMediaPickerBusy())
        {
            if (CanSelectMultipleFilesFromGallery())
            {
#if !UNITY_EDITOR && UNITY_ANDROID
                AJC.CallStatic("PickMedia", Context, new NGMediaReceiveCallbackAndroid(null, callback), imageMode, true, mime, title);
#else
                if (callback != null)
                {
                    callback(null);
                }
#endif
            }
            else if (callback != null)
            {
                callback(null);
            }
        }

        return(result);
    }
Ejemplo n.º 22
0
    public static bool CanSelectMultipleFilesFromGallery()
    {
#if !UNITY_EDITOR && UNITY_ANDROID
        return(AJC.CallStatic <bool>("CanSelectMultipleMedia"));
#else
        return(false);
#endif
    }
Ejemplo n.º 23
0
    public static void OpenSettings()
    {
#if !UNITY_EDITOR && UNITY_ANDROID
        AJC.CallStatic("OpenSettings", Context);
#elif !UNITY_EDITOR && UNITY_IOS
        _NativeGallery_OpenSettings();
#endif
    }
    public static void OpenSettings()
    {
#if IS_ANDROID_PLATFORM
        AJC.CallStatic("OpenSettings", Context);
#else
        Debug.Log("Opening settings...");
#endif
    }
Ejemplo n.º 25
0
    public static bool CanPickMultipleFiles()
    {
#if !UNITY_EDITOR && UNITY_ANDROID
        return(AJC.CallStatic <bool>("CanPickMultipleFiles"));
#elif !UNITY_EDITOR && UNITY_IOS
        return(_NativeFilePicker_CanPickMultipleFiles() == 1);
#else
        return(false);
#endif
    }
Ejemplo n.º 26
0
        public static bool FileExists(string path)
        {
#if !UNITY_EDITOR && UNITY_ANDROID
            if (ShouldUseSAF)
            {
                return(AJC.CallStatic <bool>("SAFEntryExists", Context, path, false));
            }
#endif
            return(File.Exists(path));
        }
Ejemplo n.º 27
0
        public static long GetFilesize(string path)
        {
#if !UNITY_EDITOR && UNITY_ANDROID
            if (ShouldUseSAF)
            {
                return(AJC.CallStatic <long>("SAFEntrySize", Context, path));
            }
#endif
            return(new FileInfo(path).Length);
        }
Ejemplo n.º 28
0
        public static string GetFilename(string path)
        {
#if !UNITY_EDITOR && UNITY_ANDROID
            if (ShouldUseSAF)
            {
                return(AJC.CallStatic <string>("SAFEntryName", Context, path));
            }
#endif
            return(Path.GetFileName(path));
        }
Ejemplo n.º 29
0
        public static string GetDirectoryName(string path)
        {
#if !UNITY_EDITOR && UNITY_ANDROID
            if (ShouldUseSAF)
            {
                return(AJC.CallStatic <string>("GetParentDirectory", Context, path));
            }
#endif
            return(Path.GetDirectoryName(path));
        }
Ejemplo n.º 30
0
    public static bool DeviceHasCamera()
    {
#if !UNITY_EDITOR && UNITY_ANDROID
        return(AJC.CallStatic <bool>("HasCamera", Context));
#elif !UNITY_EDITOR && UNITY_IOS
        return(_NativeCamera_HasCamera() == 1);
#else
        return(true);
#endif
    }