Skip to content

Commit

Permalink
To listen for pointer events, NonDrawingGraphic is now used instead o…
Browse files Browse the repository at this point in the history
…f an Image (results in slightly improved graphics performance)
  • Loading branch information
yasirkula committed Nov 2, 2019
1 parent aee2642 commit 94a3360
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 36 deletions.
29 changes: 10 additions & 19 deletions Plugins/SimpleInput/Scripts/AxisInputs/Joystick.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public enum MovementAxes { XandY, X, Y };
public SimpleInput.AxisInput yAxis = new SimpleInput.AxisInput( "Vertical" );

private RectTransform joystickTR;
private Image background;
private Graphic background;

public MovementAxes movementAxes = MovementAxes.XandY;
public float valueMultiplier = 1f;
Expand Down Expand Up @@ -48,10 +48,10 @@ private void Awake()
joystickTR = (RectTransform) transform;
thumbTR = thumb.rectTransform;

Image bgImage = GetComponent<Image>();
if( bgImage != null )
Graphic bgGraphic = GetComponent<Graphic>();
if( bgGraphic )
{
background = bgImage;
background = bgGraphic;
background.raycastTarget = false;
}

Expand All @@ -67,6 +67,8 @@ private void Awake()

_1OverMovementAreaRadius = 1f / movementAreaRadius;
movementAreaRadiusSqr = movementAreaRadius * movementAreaRadius;

thumbTR.localPosition = Vector3.zero;
}

private void Start()
Expand All @@ -76,28 +78,17 @@ private void Start()
eventReceiver = thumbTR.gameObject.AddComponent<SimpleInputDragListener>();
else
{
if( dynamicJoystickMovementArea == null )
if( !dynamicJoystickMovementArea )
{
Transform canvasTransform = thumb.canvas.transform;
dynamicJoystickMovementArea = new GameObject( "Dynamic Joystick Movement Area", typeof( RectTransform ), typeof( Image ) ).GetComponent<RectTransform>();

dynamicJoystickMovementArea.SetParent( canvasTransform, false );
dynamicJoystickMovementArea = new GameObject( "Dynamic Joystick Movement Area", typeof( RectTransform ) ).GetComponent<RectTransform>();
dynamicJoystickMovementArea.SetParent( thumb.canvas.transform, false );
dynamicJoystickMovementArea.SetAsFirstSibling();

dynamicJoystickMovementArea.anchorMin = Vector2.zero;
dynamicJoystickMovementArea.anchorMax = Vector2.one;
dynamicJoystickMovementArea.sizeDelta = Vector2.zero;
dynamicJoystickMovementArea.anchoredPosition = Vector2.zero;
}

Image dynamicJoystickMovementAreaRaycastTarget = dynamicJoystickMovementArea.GetComponent<Image>();
if( dynamicJoystickMovementAreaRaycastTarget == null )
dynamicJoystickMovementAreaRaycastTarget = dynamicJoystickMovementArea.gameObject.AddComponent<Image>();

dynamicJoystickMovementAreaRaycastTarget.sprite = thumb.sprite;
dynamicJoystickMovementAreaRaycastTarget.color = Color.clear;
dynamicJoystickMovementAreaRaycastTarget.raycastTarget = true;

eventReceiver = dynamicJoystickMovementArea.gameObject.AddComponent<SimpleInputDragListener>();
}

Expand Down Expand Up @@ -184,7 +175,7 @@ private void OnUpdate()
c.a = opacity;
thumb.color = c;

if( background != null )
if( background )
{
c = background.color;
c.a = opacity;
Expand Down
17 changes: 17 additions & 0 deletions Plugins/SimpleInput/Scripts/Core/NonDrawingGraphic.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using UnityEngine.UI;

namespace SimpleInputNamespace
{
// Credit: http://answers.unity.com/answers/1157876/view.html
public class NonDrawingGraphic : Graphic
{
public override void SetMaterialDirty() { return; }
public override void SetVerticesDirty() { return; }

protected override void OnPopulateMesh( VertexHelper vh )
{
vh.Clear();
return;
}
}
}
12 changes: 12 additions & 0 deletions Plugins/SimpleInput/Scripts/Core/NonDrawingGraphic.cs.meta

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

7 changes: 2 additions & 5 deletions Plugins/SimpleInput/Scripts/Core/SimpleInputDragListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,8 @@ public class SimpleInputDragListener : MonoBehaviour, IPointerDownHandler, IDrag
private void Awake()
{
Graphic graphic = GetComponent<Graphic>();
if( graphic == null )
{
graphic = gameObject.AddComponent<Image>();
graphic.color = Color.clear;
}
if( !graphic )
graphic = gameObject.AddComponent<NonDrawingGraphic>();

graphic.raycastTarget = true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,8 @@ public class SimpleInputMultiDragListener : MonoBehaviour, IPointerDownHandler,
private void Awake()
{
Graphic graphic = GetComponent<Graphic>();
if( graphic == null )
{
graphic = gameObject.AddComponent<Image>();
graphic.color = Color.clear;
}
if( !graphic )
graphic = gameObject.AddComponent<NonDrawingGraphic>();

graphic.raycastTarget = true;
}
Expand Down
10 changes: 3 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@

**WebGL Demo:** http://yasirkula.net/SimpleInputDemo/

## ABOUT

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

To use the SimpleInput system, simply replace Input with **SimpleInput** in your scripts; i.e:
## HOW TO

First, import [SimpleInput.unitypackage](https://github.com/yasirkula/UnitySimpleInput/releases) to your project. To use the SimpleInput system, simply replace Input with **SimpleInput** in your scripts; i.e:

- Input.GetAxis -> SimpleInput.GetAxis
- Input.GetAxisRaw -> SimpleInput.GetAxisRaw
Expand All @@ -31,10 +31,6 @@ By default, SimpleInput receives input from Unity's Input system, as well. That'

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

## UPGRADING FROM PREVIOUS VERSIONS

If you are upgrading from a very old version of SimpleInput, values of axes, buttons, mouse buttons and keys in SimpleInput components might get reset after the upgrade. So, you should write down these values somewhere before upgrading the plugin.

## BUILT-IN INPUT COMPONENTS

### SimpleInput.GetAxis Inputs
Expand Down
Binary file removed SimpleInput.unitypackage
Binary file not shown.

0 comments on commit 94a3360

Please sign in to comment.