Skip to content

Commit 1750c04

Browse files
committed
- BREAKING CHANGE: Replaced RequestPermission with RequestPermissionAsync (see yasirkula/UnityNativeGallery#343)
- BREAKING CHANGE: CheckPermission returns bool instead of Permission (see yasirkula/UnityAndroidRuntimePermissions#14) - Updated Unity version to 2021.3.41f1 - Upgraded Texts to TextMesh Pro (closed #82) - Configurable Enter Play Mode is supported (disabling Domain Reload works but disabling Scene Reload isn't tested) - FileBrowser.Instance is now public for convenience - Removed nested canvases from SimpleFileBrowserCanvas prefab to make VR integration simpler
1 parent 07398a2 commit 1750c04

49 files changed

Lines changed: 13340 additions & 9528 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.github/AAR Source (Android)/java/com/yasirkula/unity/FileBrowser.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -170,20 +170,14 @@ public static int CheckPermission( Context context )
170170
}
171171

172172
// Credit: https://github.com/Over17/UnityAndroidPermissions/blob/0dca33e40628f1f279decb67d901fd444b409cd7/src/UnityAndroidPermissions/src/main/java/com/unity3d/plugin/UnityAndroidPermissions.java
173-
public static void RequestPermission( Context context, final FileBrowserPermissionReceiver permissionReceiver, final int lastCheckResult )
173+
public static void RequestPermission( Context context, final FileBrowserPermissionReceiver permissionReceiver )
174174
{
175175
if( CheckPermission( context ) == 1 )
176176
{
177177
permissionReceiver.OnPermissionResult( 1 );
178178
return;
179179
}
180180

181-
if( lastCheckResult == 0 ) // If user clicked "Don't ask again" before, don't bother asking them again
182-
{
183-
permissionReceiver.OnPermissionResult( 0 );
184-
return;
185-
}
186-
187181
final Fragment request = new FileBrowserPermissionFragment( permissionReceiver );
188182
( (Activity) context ).getFragmentManager().beginTransaction().add( 0, request ).commitAllowingStateLoss();
189183
}

.github/README.md

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -168,25 +168,22 @@ File browser comes bundled with two premade skins in the *Skins* directory: *Lig
168168
- By changing *SimpleFileBrowserCanvas* prefab's *Skin* field
169169
- By changing the value of `FileBrowser.Skin` property from a C# script
170170

171-
On Android, file browser requires external storage access to function properly. You can use the following function to check if we have runtime permission to access the external storage:
171+
On Android, file browser requires external storage access to function properly. You can use the following functions to handle runtime permissions:
172172

173173
```csharp
174-
public static FileBrowser.Permission CheckPermission();
174+
public delegate void PermissionCallback( Permission permission );
175+
176+
public static bool CheckPermission();
177+
public static void RequestPermissionAsync( PermissionCallback callback );
175178
```
176179

177180
**FileBrowser.Permission** is an enum that can take 3 values:
178181

179182
- **Granted**: we have the permission to access the external storage
180-
- **ShouldAsk**: we don't have permission yet, but we can ask the user for permission via *RequestPermission* function (see below). As long as the user doesn't select "Don't ask again" while denying the permission, ShouldAsk is returned
183+
- **ShouldAsk**: we don't have permission yet but we can continue asking the user for permission. As long as the user doesn't select "Don't ask again" while denying the permission, ShouldAsk is returned
181184
- **Denied**: we don't have permission and we can't ask the user for permission. In this case, user has to give the permission from Settings. This happens when user selects "Don't ask again" while denying the permission or when user is not allowed to give that permission (parental controls etc.)
182185

183-
To request permission to access the external storage, use the following function:
184-
185-
```csharp
186-
public static FileBrowser.Permission RequestPermission();
187-
```
188-
189-
Note that FileBrowser automatically calls RequestPermission before opening a dialog. If you want, you can turn this feature off by setting **FileBrowser.AskPermissions** to *false*.
186+
Note that FileBrowser automatically calls RequestPermissionAsync before opening a dialog. If you want, you can turn this feature off by setting **FileBrowser.AskPermissions** to *false*.
190187

191188
The following file manipulation functions work on all platforms (including *Storage Access Framework (SAF)* on *Android 10+*). These functions should be called with the paths returned by the FileBrowser functions only:
192189

Plugins/SimpleFileBrowser/Android/FBCallbackHelper.cs

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,49 @@
11
#if UNITY_EDITOR || UNITY_ANDROID
2+
using System;
23
using UnityEngine;
34

45
namespace SimpleFileBrowser
56
{
67
public class FBCallbackHelper : MonoBehaviour
78
{
8-
private System.Action mainThreadAction = null;
9+
private bool autoDestroyWithCallback;
10+
private Action mainThreadAction = null;
911

10-
private void Awake()
12+
public static FBCallbackHelper Create( bool autoDestroyWithCallback )
1113
{
12-
DontDestroyOnLoad( gameObject );
14+
FBCallbackHelper result = new GameObject( "FBCallbackHelper" ).AddComponent<FBCallbackHelper>();
15+
result.autoDestroyWithCallback = autoDestroyWithCallback;
16+
DontDestroyOnLoad( result.gameObject );
17+
return result;
1318
}
1419

15-
private void Update()
20+
public void CallOnMainThread( Action function )
1621
{
17-
if( mainThreadAction != null )
22+
lock( this )
1823
{
19-
System.Action temp = mainThreadAction;
20-
mainThreadAction = null;
21-
temp();
24+
mainThreadAction += function;
2225
}
2326
}
2427

25-
public void CallOnMainThread( System.Action function )
28+
private void Update()
2629
{
27-
mainThreadAction = function;
30+
if( mainThreadAction != null )
31+
{
32+
try
33+
{
34+
lock( this )
35+
{
36+
Action temp = mainThreadAction;
37+
mainThreadAction = null;
38+
temp();
39+
}
40+
}
41+
finally
42+
{
43+
if( autoDestroyWithCallback )
44+
Destroy( gameObject );
45+
}
46+
}
2847
}
2948
}
3049
}

Plugins/SimpleFileBrowser/Android/FBDirectoryReceiveCallbackAndroid.cs

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,26 +11,13 @@ public class FBDirectoryReceiveCallbackAndroid : AndroidJavaProxy
1111
public FBDirectoryReceiveCallbackAndroid( FileBrowser.AndroidSAFDirectoryPickCallback callback ) : base( "com.yasirkula.unity.FileBrowserDirectoryReceiver" )
1212
{
1313
this.callback = callback;
14-
callbackHelper = new GameObject( "FBCallbackHelper" ).AddComponent<FBCallbackHelper>();
14+
callbackHelper = FBCallbackHelper.Create( true );
1515
}
1616

1717
[UnityEngine.Scripting.Preserve]
1818
public void OnDirectoryPicked( string rawUri, string name )
1919
{
20-
callbackHelper.CallOnMainThread( () => DirectoryPickedCallback( rawUri, name ) );
21-
}
22-
23-
private void DirectoryPickedCallback( string rawUri, string name )
24-
{
25-
try
26-
{
27-
if( callback != null )
28-
callback( rawUri, name );
29-
}
30-
finally
31-
{
32-
Object.Destroy( callbackHelper.gameObject );
33-
}
20+
callbackHelper.CallOnMainThread( () => callback( rawUri, name ) );
3421
}
3522
}
3623
}

Plugins/SimpleFileBrowser/Android/FBPermissionCallbackAndroid.cs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,23 @@
11
#if UNITY_EDITOR || UNITY_ANDROID
2-
using System.Threading;
32
using UnityEngine;
43

54
namespace SimpleFileBrowser
65
{
76
public class FBPermissionCallbackAndroid : AndroidJavaProxy
87
{
9-
private object threadLock;
10-
public int Result { get; private set; }
8+
private readonly FileBrowser.PermissionCallback callback;
9+
private readonly FBCallbackHelper callbackHelper;
1110

12-
public FBPermissionCallbackAndroid( object threadLock ) : base( "com.yasirkula.unity.FileBrowserPermissionReceiver" )
11+
public FBPermissionCallbackAndroid( FileBrowser.PermissionCallback callback ) : base( "com.yasirkula.unity.FileBrowserPermissionReceiver" )
1312
{
14-
Result = -1;
15-
this.threadLock = threadLock;
13+
this.callback = callback;
14+
callbackHelper = FBCallbackHelper.Create( true );
1615
}
1716

1817
[UnityEngine.Scripting.Preserve]
1918
public void OnPermissionResult( int result )
2019
{
21-
Result = result;
22-
23-
lock( threadLock )
24-
{
25-
Monitor.Pulse( threadLock );
26-
}
20+
callbackHelper.CallOnMainThread( () => callback( (FileBrowser.Permission) result ) );
2721
}
2822
}
2923
}
-44 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)