Skip to content

Commit

Permalink
Added new Input System support (requires manual modifications, see do…
Browse files Browse the repository at this point in the history
…cumentation)
  • Loading branch information
yasirkula committed Apr 12, 2021
1 parent f377168 commit d581661
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 20 deletions.
9 changes: 9 additions & 0 deletions .github/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,15 @@ There are 5 ways to install this plugin:
- *(via [OpenUPM](https://openupm.com))* after installing [openupm-cli](https://github.com/openupm/openupm-cli), run the following command:
- `openupm add com.yasirkula.simplefilebrowser`

### NEW INPUT SYSTEM SUPPORT

This plugin supports Unity's new Input System but it requires some manual modifications (if both the legacy and the new input systems are active at the same time, no changes are needed):

- the plugin mustn't be installed as a package, i.e. it must reside inside the *Assets* folder and not the *Packages* folder (it can reside inside a subfolder of Assets like *Assets/Plugins*)
- if Unity 2019.2.5 or earlier is used, add `ENABLE_INPUT_SYSTEM` compiler directive to **Player Settings/Scripting Define Symbols** (these symbols are platform specific, so if you change the active platform later, you'll have to add the compiler directive again)
- add `Unity.InputSystem` assembly to **SimpleFileBrowser.Runtime** Assembly Definition File's *Assembly Definition References* list
- open *SimpleFileBrowserCanvas* prefab, select *EventSystem* child object and replace *StandaloneInputModule* component with *InputSystemUIInputModule* component (or, if your scene(s) already have EventSystem objects, you can delete SimpleFileBrowserCanvas prefab's EventSystem child object)

## FAQ

- **Android build fails, it says "error: attribute android:requestLegacyExternalStorage not found" in Console**
Expand Down
17 changes: 13 additions & 4 deletions Plugins/SimpleFileBrowser/README.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,24 @@
Online documentation & example code available at: https://github.com/yasirkula/UnitySimpleFileBrowser
E-mail: [email protected]

1. ABOUT
### ABOUT
This plugin helps you show save/load dialogs during gameplay with its uGUI based file browser.


2. HOW TO
### HOW TO
The file browser can be shown either as a save dialog or a load dialog. In load mode, the returned path(s) always lead to existing files or folders. In save mode, the returned path(s) can point to non-existing files, as well.


3. FAQ
### NEW INPUT SYSTEM SUPPORT
This plugin supports Unity's new Input System but it requires some manual modifications (if both the legacy and the new input systems are active at the same time, no changes are needed):

- the plugin mustn't be installed as a package, i.e. it must reside inside the Assets folder and not the Packages folder (it can reside inside a subfolder of Assets like Assets/Plugins)
- if Unity 2019.2.5 or earlier is used, add ENABLE_INPUT_SYSTEM compiler directive to "Player Settings/Scripting Define Symbols" (these symbols are platform specific, so if you change the active platform later, you'll have to add the compiler directive again)
- add "Unity.InputSystem" assembly to "SimpleFileBrowser.Runtime" Assembly Definition File's "Assembly Definition References" list
- open SimpleFileBrowserCanvas prefab, select EventSystem child object and replace StandaloneInputModule component with InputSystemUIInputModule component (or, if your scene(s) already have EventSystem objects, you can delete SimpleFileBrowserCanvas prefab's EventSystem child object)


### FAQ
- Android build fails, it says "error: attribute android:requestLegacyExternalStorage not found" in Console
"android:requestLegacyExternalStorage" attribute in AndroidManifest.xml grants full access to device's storage on Android 10 but requires you to update your Android SDK to at least SDK 29. If this isn't possible for you, you should open SimpleFileBrowser.aar with WinRAR or 7-Zip and then remove the "<application ... />" tag from AndroidManifest.xml.

Expand All @@ -26,7 +35,7 @@ Declare the WRITE_EXTERNAL_STORAGE permission manually in your Plugins/Android/A
You'll need to add the following attribute to the '<manifest ...>' element: xmlns:tools="http://schemas.android.com/tools"


4. SCRIPTING API
### SCRIPTING API
Please see the online documentation for a more in-depth documentation of the Scripting API: https://github.com/yasirkula/UnitySimpleFileBrowser

NOTE: On Android Q (10) or later, it is impossible to work with File APIs. On these devices, SimpleFileBrowser uses Storage Access Framework (SAF) to browse the files. However, paths returned by SAF are not File API compatible. To simulate the behaviour of the File API on all devices (including SAF), you can check out the FileBrowserHelpers functions. For reference, here is an example SAF path: content://com.android.externalstorage.documents/tree/primary%3A/document/primary%3APictures
Expand Down
51 changes: 43 additions & 8 deletions Plugins/SimpleFileBrowser/Scripts/FileBrowser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
using System.Collections;
using System.Collections.Generic;
using System.Text;
#if ENABLE_INPUT_SYSTEM && !ENABLE_LEGACY_INPUT_MANAGER
using UnityEngine.InputSystem;
#endif

namespace SimpleFileBrowser
{
Expand Down Expand Up @@ -730,6 +733,13 @@ private void Awake()

if( !showResizeCursor )
Destroy( resizeCursorHandler );

#if ENABLE_INPUT_SYSTEM && !ENABLE_LEGACY_INPUT_MANAGER
// On new Input System, scroll sensitivity is much higher than legacy Input system
filesScrollRect.scrollSensitivity *= 0.25f;
quickLinksContainer.GetComponentInParent<ScrollRect>().scrollSensitivity *= 0.25f;
filtersDropdownContainer.GetComponent<ScrollRect>().scrollSensitivity *= 0.25f;
#endif
}

private void OnRectTransformDimensionsChange()
Expand Down Expand Up @@ -758,14 +768,31 @@ private void LateUpdate()
// Handle keyboard shortcuts
if( !EventSystem.current.currentSelectedGameObject )
{
if( Input.GetKeyDown( KeyCode.Delete ) )
DeleteSelectedFiles();
#if ENABLE_INPUT_SYSTEM && !ENABLE_LEGACY_INPUT_MANAGER
if( Keyboard.current != null )
#endif
{
#if ENABLE_INPUT_SYSTEM && !ENABLE_LEGACY_INPUT_MANAGER
if( Keyboard.current[Key.Delete].wasPressedThisFrame )
#else
if( Input.GetKeyDown( KeyCode.Delete ) )
#endif
DeleteSelectedFiles();

if( Input.GetKeyDown( KeyCode.F2 ) )
RenameSelectedFile();
#if ENABLE_INPUT_SYSTEM && !ENABLE_LEGACY_INPUT_MANAGER
if( Keyboard.current[Key.F2].wasPressedThisFrame )
#else
if( Input.GetKeyDown( KeyCode.F2 ) )
#endif
RenameSelectedFile();

if( Input.GetKeyDown( KeyCode.A ) && ( Input.GetKey( KeyCode.LeftControl ) || Input.GetKey( KeyCode.LeftCommand ) ) )
SelectAllFiles();
#if ENABLE_INPUT_SYSTEM && !ENABLE_LEGACY_INPUT_MANAGER
if( Keyboard.current[Key.A].wasPressedThisFrame && Keyboard.current.ctrlKey.isPressed )
#else
if( Input.GetKeyDown( KeyCode.A ) && ( Input.GetKey( KeyCode.LeftControl ) || Input.GetKey( KeyCode.LeftCommand ) ) )
#endif
SelectAllFiles();
}
}
#endif

Expand Down Expand Up @@ -1091,12 +1118,12 @@ public void OnMoreOptionsButtonClicked()
ShowContextMenuAt( rectTransform.InverseTransformPoint( moreOptionsContextMenuPosition.position ), true );
}

internal void OnContextMenuTriggered()
internal void OnContextMenuTriggered( Vector2 pointerPos )
{
filesScrollRect.velocity = Vector2.zero;

Vector2 position;
RectTransformUtility.ScreenPointToLocalPointInRectangle( rectTransform, Input.mousePosition, canvas.worldCamera, out position );
RectTransformUtility.ScreenPointToLocalPointInRectangle( rectTransform, pointerPos, canvas.worldCamera, out position );

ShowContextMenuAt( position, false );
}
Expand Down Expand Up @@ -1396,7 +1423,11 @@ public void OnItemSelected( FileBrowserItem item, bool isDoubleClick )
{
#if UNITY_EDITOR || UNITY_STANDALONE || UNITY_WEBGL || UNITY_WSA || UNITY_WSA_10_0
// When Shift key is held, all items from the pivot item to the clicked item will be selected
#if ENABLE_INPUT_SYSTEM && !ENABLE_LEGACY_INPUT_MANAGER
if( Keyboard.current != null && Keyboard.current.shiftKey.isPressed )
#else
if( Input.GetKey( KeyCode.LeftShift ) || Input.GetKey( KeyCode.RightShift ) )
#endif
{
multiSelectionPivotFileEntry = Mathf.Clamp( multiSelectionPivotFileEntry, 0, validFileEntries.Count - 1 );

Expand All @@ -1416,7 +1447,11 @@ public void OnItemSelected( FileBrowserItem item, bool isDoubleClick )

// When in toggle selection mode or Control key is held, individual items can be multi-selected
#if UNITY_EDITOR || UNITY_STANDALONE || UNITY_WEBGL || UNITY_WSA || UNITY_WSA_10_0
#if ENABLE_INPUT_SYSTEM && !ENABLE_LEGACY_INPUT_MANAGER
if( m_multiSelectionToggleSelectionMode || ( Keyboard.current != null && Keyboard.current.ctrlKey.isPressed ) )
#else
if( m_multiSelectionToggleSelectionMode || Input.GetKey( KeyCode.LeftControl ) || Input.GetKey( KeyCode.RightControl ) )
#endif
#else
if( m_multiSelectionToggleSelectionMode )
#endif
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
#if ENABLE_INPUT_SYSTEM && !ENABLE_LEGACY_INPUT_MANAGER
using UnityEngine.InputSystem;
#endif

namespace SimpleFileBrowser
{
Expand Down Expand Up @@ -80,12 +83,25 @@ internal void OnCanvasDimensionsChanged( Vector2 size )
#if UNITY_EDITOR || UNITY_STANDALONE || UNITY_WSA || UNITY_WSA_10_0
private void LateUpdate()
{
// Handle keyboard shortcuts
if( Input.GetKeyDown( KeyCode.Return ) || Input.GetKeyDown( KeyCode.KeypadEnter ) )
YesButtonClicked();
#if ENABLE_INPUT_SYSTEM && !ENABLE_LEGACY_INPUT_MANAGER
if( Keyboard.current != null )
#endif
{
// Handle keyboard shortcuts
#if ENABLE_INPUT_SYSTEM && !ENABLE_LEGACY_INPUT_MANAGER
if( Keyboard.current[Key.Enter].wasPressedThisFrame || Keyboard.current[Key.NumpadEnter].wasPressedThisFrame )
#else
if( Input.GetKeyDown( KeyCode.Return ) || Input.GetKeyDown( KeyCode.KeypadEnter ) )
#endif
YesButtonClicked();

if( Input.GetKeyDown( KeyCode.Escape ) )
NoButtonClicked();
#if ENABLE_INPUT_SYSTEM && !ENABLE_LEGACY_INPUT_MANAGER
if( Keyboard.current[Key.Escape].wasPressedThisFrame )
#else
if( Input.GetKeyDown( KeyCode.Escape ) )
#endif
NoButtonClicked();
}
}
#endif

Expand Down
4 changes: 3 additions & 1 deletion Plugins/SimpleFileBrowser/Scripts/FileBrowserItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ public class FileBrowserItem : ListItem, IPointerClickHandler, IPointerDownHandl
private Text nameText;
#pragma warning restore 0649

#pragma warning disable 0414
private bool isSelected;
#pragma warning restore 0414

private float pressTime = Mathf.Infinity;
private float prevClickTime;
Expand Down Expand Up @@ -98,7 +100,7 @@ public void OnPointerClick( PointerEventData eventData )
}

// Then, show the context menu
fileBrowser.OnContextMenuTriggered();
fileBrowser.OnContextMenuTriggered( eventData.position );
return;
}
#endif
Expand Down
7 changes: 7 additions & 0 deletions Plugins/SimpleFileBrowser/Scripts/FileBrowserRenamedItem.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
#if ENABLE_INPUT_SYSTEM && !ENABLE_LEGACY_INPUT_MANAGER
using UnityEngine.InputSystem;
#endif

namespace SimpleFileBrowser
{
Expand Down Expand Up @@ -38,7 +41,11 @@ public void Show( string initialFilename, Color backgroundColor, Sprite icon, On
private void LateUpdate()
{
// Don't allow scrolling with mouse wheel while renaming a file or creating a folder
#if ENABLE_INPUT_SYSTEM && !ENABLE_LEGACY_INPUT_MANAGER
if( Mouse.current != null && Mouse.current.scroll.ReadValue().y != 0f )
#else
if( Input.mouseScrollDelta.y != 0f )
#endif
nameInputField.DeactivateInputField();
}
#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ public void OnPointerClick( PointerEventData eventData )
if( eventData.button == PointerEventData.InputButton.Left )
fileBrowser.DeselectAllFiles();
else if( eventData.button == PointerEventData.InputButton.Right )
fileBrowser.OnContextMenuTriggered();
fileBrowser.OnContextMenuTriggered( eventData.position );
}
#endif
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "com.yasirkula.simplefilebrowser",
"displayName": "Simple File Browser",
"version": "1.4.3",
"version": "1.4.4",
"documentationUrl": "https://github.com/yasirkula/UnitySimpleFileBrowser",
"changelogUrl": "https://github.com/yasirkula/UnitySimpleFileBrowser/releases",
"licensesUrl": "https://github.com/yasirkula/UnitySimpleFileBrowser/blob/master/LICENSE.txt",
Expand Down

0 comments on commit d581661

Please sign in to comment.