Skip to content

Commit

Permalink
- Added SimpleInput.TrackUnityInput (see README)
Browse files Browse the repository at this point in the history
- Got rid of CS0649 warnings in the console
- Bugfix for an exception that might occur when scene changes
- Packed unused sprites into ExtraResources.unitypackage
  • Loading branch information
yasirkula committed Jul 7, 2019
1 parent 52a02e8 commit aee2642
Show file tree
Hide file tree
Showing 446 changed files with 330 additions and 18,296 deletions.
2 changes: 1 addition & 1 deletion Plugins/SimpleInput/Example/ExamplePlayerController.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using UnityEngine;

public class ExamplePlayerController : MonoBehaviour
public class ExamplePlayerController : MonoBehaviour
{
public Color materialColor;
private Rigidbody m_rigidbody;
Expand Down
Binary file added Plugins/SimpleInput/ExtraResources.unitypackage
Binary file not shown.

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

45 changes: 45 additions & 0 deletions Plugins/SimpleInput/README.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
= Simple Input =

Online documentation available at: https://github.com/yasirkula/UnitySimpleInput
E-mail: [email protected]

1. ABOUT
This tool is an improvement over Unity's standard Input system that allows you to use custom input providers like on-screen joysticks, UI buttons and d-pads. In other words, it lets you simulate e.g. Input.GetAxis when a button is pressed or a virtual joystick is dragged. It also supports using custom axes and buttons that don't necessarily exist in Edit-Project Settings-Input.

2. HOW TO
To use the SimpleInput system, simply replace Input with SimpleInput in your scripts. Note that there is no replacement for Input.GetKey(string) function. You have to convert the string to the corresponding KeyCode to benefit from SimpleInput.GetKey(KeyCode) function.

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). For more resources, open ExtraResources.unitypackage.

By default, SimpleInput receives input from Unity's Input system, as well. That's why your code keeps working as is after changing Input to SimpleInput. If you want, you can disable this behaviour so that SimpleInput receives input from custom input providers only. Simply calling "SimpleInput.TrackUnityInput = false;" will do the trick. It is possible to let a subset of the standard Unity inputs continue providing input to SimpleInput via the UnityInputProvider component.

SimpleInput works almost identically to standard Input system; only the lerping of Input.GetAxis might differ slightly. Lerp modifier can be configured via SimpleInput.AxisLerpModifier.

2.1. 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
- AxisInputPinchGesture: provides axis input while pinch gesture is performed with two pointers on a RectTransform
- AxisInputRotateGesture: provides axis input while rotate gesture is performed with two pointers on a RectTransform
- AxisInputSwipeGesture: provides axis input while a pointer is swiped by a specified amount on a RectTransform
- Dpad: provides -1, 0 or 1 as axis input for x and y axes while the Dpad is held down; works similar to joystick Dpads
- Joystick: a standard on-screen joystick input. If Is Dynamic Joystick is selected, joystick only appears while a pointer touches the screen. Dynamic Joystick Movement Area specifies the zone that the dynamic joystick can appear in (leave blank to use the whole canvas)
- SteeringWheel: provides axis input while the wheel is rotated (by far, the most fun input method to play with =') )
- Touchpad: provides axis input while a pointer is dragged on a RectTransform

2.2. 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

2.3. 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

2.4. 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

To send an input while a mouse button is held down, you can use the XInputKeyboard component and set the key to the desired mouse button: KeyCode.Mouse0, KeyCode.Mouse1, etc.
8 changes: 8 additions & 0 deletions Plugins/SimpleInput/README.txt.meta

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

2 changes: 2 additions & 0 deletions Plugins/SimpleInput/Scripts/AxisInputs/AxisInputKeyboard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ namespace SimpleInputNamespace
{
public class AxisInputKeyboard : MonoBehaviour
{
#pragma warning disable 0649
[SerializeField]
private KeyCode key;
#pragma warning restore 0649

public SimpleInput.AxisInput axis = new SimpleInput.AxisInput();
public float value = 1f;
Expand Down
4 changes: 2 additions & 2 deletions Plugins/SimpleInput/Scripts/AxisInputs/AxisInputMouse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public class AxisInputMouse : MonoBehaviour
public SimpleInput.AxisInput xAxis = new SimpleInput.AxisInput();
public SimpleInput.AxisInput yAxis = new SimpleInput.AxisInput();

//#if UNITY_EDITOR || UNITY_STANDALONE || UNITY_WEBGL || UNITY_FACEBOOK || UNITY_WSA || UNITY_WSA_10_0
//#if UNITY_EDITOR || UNITY_STANDALONE || UNITY_WEBGL || UNITY_FACEBOOK || UNITY_WSA || UNITY_WSA_10_0
private void OnEnable()
{
xAxis.StartTracking();
Expand Down Expand Up @@ -37,6 +37,6 @@ private void OnUpdate()
yAxis.value = 0f;
}
}
//#endif
//#endif
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,8 @@ public bool OnUpdate( List<PointerEventData> mousePointers, List<PointerEventDat

if( touchPointers.Count < 2 )
{
#pragma warning disable 0252
if( activeListener == this && touchPointers.Count == 1 )
if( ReferenceEquals( activeListener, this ) && touchPointers.Count == 1 )
touchPointers[0].pressPosition = touchPointers[0].position;
#pragma warning restore 0252

return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class AxisInputRotateGesture : MonoBehaviour, ISimpleInputDraggableMultiT
public bool clockwise = true;

private SimpleInputMultiDragListener eventReceiver;

public int Priority { get { return 2; } }

private void Awake()
Expand Down Expand Up @@ -44,10 +44,8 @@ public bool OnUpdate( List<PointerEventData> mousePointers, List<PointerEventDat

if( touchPointers.Count < 2 )
{
#pragma warning disable 0252
if( activeListener == this && touchPointers.Count == 1 )
if( ReferenceEquals( activeListener, this ) && touchPointers.Count == 1 )
touchPointers[0].pressPosition = touchPointers[0].position;
#pragma warning restore 0252

return false;
}
Expand All @@ -57,7 +55,7 @@ public bool OnUpdate( List<PointerEventData> mousePointers, List<PointerEventDat

Vector2 deltaPosition = touch2.position - touch1.position;
Vector2 prevDeltaPosition = deltaPosition - ( touch2.delta - touch1.delta );

float deltaAngle = ( Mathf.Atan2( prevDeltaPosition.y, prevDeltaPosition.x ) - Mathf.Atan2( deltaPosition.y, deltaPosition.x ) ) * MULTIPLIER;
if( deltaAngle > 180f )
deltaAngle -= 360f;
Expand Down
2 changes: 0 additions & 2 deletions Plugins/SimpleInput/Scripts/AxisInputs/Dpad.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

namespace SimpleInputNamespace
{
Expand All @@ -12,7 +11,6 @@ public class Dpad : MonoBehaviour, ISimpleInputDraggable
public float valueMultiplier = 1f;

private RectTransform rectTransform;
private Graphic background;

private Vector2 m_value = Vector2.zero;
public Vector2 Value { get { return m_value; } }
Expand Down
8 changes: 5 additions & 3 deletions Plugins/SimpleInput/Scripts/AxisInputs/Joystick.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@ public enum MovementAxes { XandY, X, Y };
private RectTransform joystickTR;
private Image background;

public MovementAxes movementAxes = MovementAxes.XandY;
public float valueMultiplier = 1f;

#pragma warning disable 0649
[SerializeField]
private Image thumb;
private RectTransform thumbTR;

public MovementAxes movementAxes = MovementAxes.XandY;
public float valueMultiplier = 1f;

[SerializeField]
private float movementAreaRadius = 75f;

Expand All @@ -29,6 +30,7 @@ public enum MovementAxes { XandY, X, Y };

[SerializeField]
private RectTransform dynamicJoystickMovementArea;
#pragma warning restore 0649

private bool joystickHeld = false;
private Vector2 pointerInitialPos;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ namespace SimpleInputNamespace
{
public class ButtonInputKeyboard : MonoBehaviour
{
#pragma warning disable 0649
[SerializeField]
private KeyCode key;
#pragma warning restore 0649

public SimpleInput.ButtonInput button = new SimpleInput.ButtonInput();

Expand Down
4 changes: 3 additions & 1 deletion Plugins/SimpleInput/Scripts/Core/BaseInput.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public interface IBaseInput

public abstract class BaseInput<K, V> : IBaseInput
{
#pragma warning disable 0649
[SerializeField]
private K m_key;
public K Key
Expand All @@ -30,6 +31,7 @@ public K Key
}
}
}
#pragma warning restore 0649

public V value;
private bool isTracking = false;
Expand Down Expand Up @@ -71,7 +73,7 @@ public void ResetValue()
protected abstract void RegisterInput();
protected abstract void UnregisterInput();
protected abstract bool KeysEqual( K key1, K key2 );

public virtual bool IsKeyValid()
{
return true;
Expand Down
6 changes: 2 additions & 4 deletions Plugins/SimpleInput/Scripts/Core/SwipeGestureBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace SimpleInputNamespace
public abstract class SwipeGestureBase<K, V> : SelectivePointerInput, ISimpleInputDraggableMultiTouch
{
public Vector2 swipeAmount = new Vector2( 0f, 25f );

private SimpleInputMultiDragListener eventReceiver;

protected abstract BaseInput<K, V> Input { get; }
Expand Down Expand Up @@ -44,10 +44,8 @@ public bool OnUpdate( List<PointerEventData> mousePointers, List<PointerEventDat
if( pointer == null )
return false;

#pragma warning disable 0252
if( !IsSwipeSatisfied( pointer ) )
return activeListener == this;
#pragma warning restore 0252
return ReferenceEquals( activeListener, this );

Input.value = Value;
return true;
Expand Down
Loading

0 comments on commit aee2642

Please sign in to comment.