Last active
October 10, 2017 05:36
-
-
Save valryon/050436915a23db81aec8 to your computer and use it in GitHub Desktop.
InControl Tap zone for touch controls
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
namespace InControl | |
{ | |
public class TouchTapControl : TouchControl | |
{ | |
[Header("Dimensions")] | |
public Rect activeArea = new Rect(0.0f, 0.0f, 0.5f, 1.0f); | |
[Header("Options")] | |
[Tooltip("Taps to trigger the action")] | |
public int tapCount = 2; | |
[Tooltip("Time between two taps")] | |
public float tapCooldown = 0.5f; | |
public ButtonTarget target = ButtonTarget.Action1; | |
private Rect worldActiveArea; | |
private int currentTapCount; | |
private float cooldown; | |
public override void CreateControl() | |
{ | |
worldActiveArea = TouchManager.ViewToWorldRect(activeArea); | |
} | |
void Update() | |
{ | |
if (cooldown > 0) | |
{ | |
cooldown -= Time.deltaTime; | |
if (cooldown <= 0) | |
{ | |
currentTapCount = 0; | |
cooldown = 0; | |
} | |
} | |
} | |
public override void SubmitControlState(ulong updateTick) | |
{ | |
bool value = false; | |
if (currentTapCount >= tapCount) | |
{ | |
value = true; | |
currentTapCount = 0; | |
cooldown = 0f; | |
} | |
SubmitButtonState(target, value, updateTick); | |
} | |
public override void TouchBegan(Touch touch) | |
{ | |
} | |
public override void TouchEnded(Touch touch) | |
{ | |
var beganPosition = TouchManager.ScreenToWorldPoint(touch.position); | |
var insideActiveArea = worldActiveArea.Contains(beganPosition); | |
if (insideActiveArea) | |
{ | |
currentTapCount++; | |
cooldown = tapCooldown; | |
} | |
} | |
#region Unused methods | |
public override void DestroyControl() | |
{ | |
} | |
public override void ConfigureControl() | |
{ | |
} | |
public override void TouchMoved(Touch touch) | |
{ | |
} | |
#endregion | |
#region Debug | |
public override void DrawGizmos() | |
{ | |
Utility.DrawRectGizmo(worldActiveArea, Color.yellow); | |
} | |
#endregion | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment