Ejemplo n.º 1
0
    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);
    }
    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.º 3
0
    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
                string destination = UnityEditor.EditorUtility.OpenFolderPanel("Select destination", Path.GetDirectoryName(filePaths[0]), "");
                if (string.IsNullOrEmpty(destination))
                {
                    if (callback != null)
                    {
                        callback(false);
                    }
                }
                else
                {
                    try
                    {
                        for (int i = 0; i < filePaths.Length; i++)
                        {
                            File.Copy(filePaths[i], Path.Combine(destination, Path.GetFileName(filePaths[i])), true);
                        }

                        if (callback != null)
                        {
                            callback(true);
                        }
                    }
                    catch (Exception e)
                    {
                        Debug.LogException(e);

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

        return(result);
    }
Ejemplo n.º 4
0
    public static Permission ExportFile(string filePath, FilesExportedCallback callback = null)
    {
        if (string.IsNullOrEmpty(filePath))
        {
            throw new ArgumentException("Parameter 'filePath' is null or empty!");
        }

        Permission result = RequestPermission(false);

        if (result == Permission.Granted && !IsFilePickerBusy())
        {
            if (CanExportFiles())
            {
#if UNITY_EDITOR
                string extension = Path.GetExtension(filePath);
                if (extension == null)
                {
                    extension = "";
                }
                else if (extension.IndexOf('.') == 0)
                {
                    extension = extension.Substring(1);
                }

                string destination = UnityEditor.EditorUtility.SaveFilePanel("Select destination", Path.GetDirectoryName(filePath), Path.GetFileName(filePath), extension);
                if (string.IsNullOrEmpty(destination))
                {
                    if (callback != null)
                    {
                        callback(false);
                    }
                }
                else
                {
                    try
                    {
                        File.Copy(filePath, destination, true);

                        if (callback != null)
                        {
                            callback(true);
                        }
                    }
                    catch (Exception e)
                    {
                        Debug.LogException(e);

                        if (callback != null)
                        {
                            callback(false);
                        }
                    }
                }
#elif UNITY_ANDROID
                AJC.CallStatic("ExportFiles", Context, new FPResultCallbackAndroid(null, null, callback), new string[1] {
                    filePath
                }, 1);
#elif UNITY_IOS
                FPResultCallbackiOS.Initialize(null, null, callback);
                _NativeFilePicker_ExportFiles(new string[1] {
                    filePath
                }, 1);
#endif
            }
            else if (callback != null)
            {
                callback(false);
            }
        }

        return(result);
    }
Ejemplo n.º 5
0
    public static Permission PickFile(FilePickedCallback 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 UNITY_EDITOR
            // Accept Android and iOS UTIs when possible, for user's convenience
            string[] editorFilters = new string[allowedFileTypes.Length * 2];
            for (int i = 0; i < allowedFileTypes.Length; i++)
            {
                if (allowedFileTypes[i].IndexOf('*') >= 0)
                {
                    if (allowedFileTypes[i] == "image/*")
                    {
                        editorFilters[i * 2]     = "Image files";
                        editorFilters[i * 2 + 1] = "png,jpg,jpeg";
                    }
                    else if (allowedFileTypes[i] == "video/*")
                    {
                        editorFilters[i * 2]     = "Video files";
                        editorFilters[i * 2 + 1] = "mp4,mov,wav,avi";
                    }
                    else if (allowedFileTypes[i] == "audio/*")
                    {
                        editorFilters[i * 2]     = "Audio files";
                        editorFilters[i * 2 + 1] = "mp3,aac,flac";
                    }
                    else
                    {
                        editorFilters[i * 2]     = "All files";
                        editorFilters[i * 2 + 1] = "*";
                    }
                }
                else
                {
                    editorFilters[i * 2] = allowedFileTypes[i];

                    if (allowedFileTypes[i].IndexOf('/') >= 0)                        // Android UTIs like 'image/png'
                    {
                        editorFilters[i * 2 + 1] = allowedFileTypes[i].Substring(allowedFileTypes[i].IndexOf('/') + 1);
                    }
                    else if (allowedFileTypes[i].StartsWith("public."))                        // iOS UTIs like 'public.png'
                    {
                        editorFilters[i * 2 + 1] = allowedFileTypes[i].Substring(7);
                    }
                    else if (allowedFileTypes[i].IndexOf('.') == 0)                        // Extensions starting with period like '.png'
                    {
                        editorFilters[i * 2 + 1] = allowedFileTypes[i].Substring(1);
                    }
                    else
                    {
                        editorFilters[i * 2 + 1] = allowedFileTypes[i];
                    }
                }
            }

            string pickedFile = UnityEditor.EditorUtility.OpenFilePanelWithFilters("Select file", "", editorFilters);

            if (callback != null)
            {
                callback(pickedFile != "" ? pickedFile : null);
            }
#elif UNITY_ANDROID
            AJC.CallStatic("PickFiles", Context, new FPResultCallbackAndroid(callback, null, null), false, SelectedFilePath, allowedFileTypes, "");
#elif UNITY_IOS
            FPResultCallbackiOS.Initialize(callback, null, null);
            _NativeFilePicker_PickFile(allowedFileTypes, allowedFileTypes.Length);
#else
            if (callback != null)
            {
                callback(null);
            }
#endif
        }

        return(result);
    }