Skip to content

Commit

Permalink
Added SimpleInputHelper to easily simulate a button/key click
Browse files Browse the repository at this point in the history
  • Loading branch information
yasirkula committed Sep 15, 2018
1 parent af63f7a commit 9dbdacd
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 14 deletions.
4 changes: 0 additions & 4 deletions Plugins/SimpleInput/README.txt

This file was deleted.

8 changes: 0 additions & 8 deletions Plugins/SimpleInput/README.txt.meta

This file was deleted.

73 changes: 73 additions & 0 deletions Plugins/SimpleInput/Scripts/SimpleInputHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
using UnityEngine;

public static class SimpleInputHelper
{
private class ButtonClickInput : SimpleInput.ButtonInput
{
public ButtonClickInput( string key ) : base( key ) { }

public void OnUpdate()
{
if( !value )
value = true;
else
{
StopTracking();
SimpleInput.OnUpdate -= OnUpdate;
}
}
}

private class MouseButtonClickInput : SimpleInput.MouseButtonInput
{
public MouseButtonClickInput( int key ) : base( key ) { }

public void OnUpdate()
{
if( !value )
value = true;
else
{
StopTracking();
SimpleInput.OnUpdate -= OnUpdate;
}
}
}

private class KeyClickInput : SimpleInput.KeyInput
{
public KeyClickInput( KeyCode key ) : base( key ) { }

public void OnUpdate()
{
if( !value )
value = true;
else
{
StopTracking();
SimpleInput.OnUpdate -= OnUpdate;
}
}
}

public static void TriggerButtonClick( string button )
{
ButtonClickInput buttonClick = new ButtonClickInput( button );
buttonClick.StartTracking();
SimpleInput.OnUpdate += buttonClick.OnUpdate;
}

public static void TriggerMouseButtonClick( int button )
{
MouseButtonClickInput mouseButtonClick = new MouseButtonClickInput( button );
mouseButtonClick.StartTracking();
SimpleInput.OnUpdate += mouseButtonClick.OnUpdate;
}

public static void TriggerKeyClick( KeyCode key )
{
KeyClickInput keyClick = new KeyClickInput( key );
keyClick.StartTracking();
SimpleInput.OnUpdate += keyClick.OnUpdate;
}
}
12 changes: 12 additions & 0 deletions Plugins/SimpleInput/Scripts/SimpleInputHelper.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
**WebGL Demo:** http://yasirkula.net/SimpleInputDemo/

## ABOUT

SimpleInput is a replacement for Unity's standard **Input** system that allows you to use your own input providers for axes, buttons, mouse buttons and keys. In other words, it lets you simulate e.g. Input.GetAxis when a button is pressed or a joystick is dragged. It also supports using custom axes and buttons that don't necessarily exist in Edit-Project Settings-Input.

To use the SimpleInput system, simply replace Input with **SimpleInput** in your scripts; i.e:
Expand All @@ -29,11 +30,13 @@ Note that there is no replacement for *Input.GetKey(string)* function. You have
SimpleInput works almost identically to standard Input system; only the lerping of Input.GetAxis might differ slightly.

## UPGRADING FROM PREVIOUS VERSIONS

Values of axes, buttons, mouse buttons and keys in SimpleInput components will be reset after the upgrade. So, you should write down these values somewhere before upgrading the plugin.

## BUILT-IN INPUT COMPONENTS

### SimpleInput.GetAxis Inputs

- **AxisInputKeyboard**: provides axis input while specified key is held down
- **AxisInputMouse**: redirects "Mouse X" and "Mouse Y" inputs to two other axes on standalone platforms. Normally, on mobile platforms, dragging your finger on touchscreen provides "Mouse X" and "Mouse Y" inputs. However, you may want to simulate these two axes only with certain input method(s) on mobile platforms, e.g. a joystick. In this case, use this component to redirect mouse input to some other custom axes (like "MouseNew X", "MouseNew Y") and use these axes with SimpleInput in your scripts. Other input method(s) e.g. joystick should also use these axes instead of "Mouse X" and "Mouse Y"
- **AxisInputUI**: provides axis input while attached UI Element (anything that extends *UnityEngine.UI.Graphic*) is held down
Expand All @@ -46,16 +49,19 @@ Values of axes, buttons, mouse buttons and keys in SimpleInput components will b
- **Touchpad**: provides axis input while a pointer is dragged on a RectTransform

### SimpleInput.GetButton Inputs

- **ButtonInputKeyboard**: provides button input while specified key is held down
- **ButtonInputUI**: provides button input while attached UI Element is held down
- **ButtonInputSwipeGesture**: provides button input while a pointer is swiped by a specified amount on a RectTransform

### SimpleInput.GetMouseButton Inputs

- **MouseButtonInputKeyboard**: provides mouse button input while specified key is held down
- **MouseButtonInputUI**: provides mouse button input while attached UI Element is held down
- **MouseButtonInputSwipeGesture**: provides mouse button input while a pointer is swiped by a specified amount on a RectTransform

### SimpleInput.GetKey Inputs

- **KeyInputKeyboard**: provides key input while specified real key is held down
- **KeyInputUI**: provides key input while attached UI Element is held down
- **KeyInputSwipeGesture**: provides key input while a pointer is swiped by a specified amount on a RectTransform
Expand All @@ -65,6 +71,7 @@ To send an input while a mouse button is held down, you can use the XInputKeyboa
**Prefabs** folder contains some plug 'n' play prefabs. Drag & drop them to your canvas and you are good to go! You can also customize them using the sprites provided in the **Sprites** folder (or using your own sprites, obviously).

## REBINDING INPUTS

It is possible to rebind the axes, buttons, mouse buttons and/or keys in your components during gameplay. For example, if you want to change the axes of your joystick from "Horizontal" and "Vertical" to "Horizontal2" and "Vertical2", use the following code:

```csharp
Expand All @@ -78,8 +85,9 @@ void ChangeBindingsOfJoystick( Joystick joystick )
Rebinding inputs from the Inspector is currently not possible during gameplay.

## WRITING CUSTOM INPUT PROVIDERS
Simply create a **SimpleInput.AxisInput**, **SimpleInput.ButtonInput**, **SimpleInput.MouseButtonInput** or **SimpleInput.KeyInput** object and call its **StartTracking()** function to start sending inputs to SimpleInput. Make sure to call the **StopTracking()** function before the object is destroyed or disabled. To change the value of the input, change its **value** field.

Simply create a **SimpleInput.AxisInput**, **SimpleInput.ButtonInput**, **SimpleInput.MouseButtonInput** or **SimpleInput.KeyInput** object and call its **StartTracking()** function to start sending inputs to SimpleInput. Make sure to call the **StopTracking()** function before the object is destroyed or disabled. To change the value of the input, change its **value** field. See *AxisInputKeyboard.cs* and *AxisInputUI.cs* for reference.

If you need to update your input's value in *Update* function, you can register to **SimpleInput.OnUpdate** event instead of using Unity's *Update* function as SimpleInput.OnUpdate is called before other Update functions (*Script Execution Order*).

See *AxisInputKeyboard.cs* and *AxisInputUI.cs* for reference.
To easily simulate a button click, mouse button click or key click from your scripts, you can use the **SimpleInputHelper.TriggerButtonClick**, **SimpleInputHelper.TriggerMouseButtonClick** and **SimpleInputHelper.TriggerKeyClick** functions.
Binary file modified SimpleInput.unitypackage
Binary file not shown.

0 comments on commit 9dbdacd

Please sign in to comment.