Skip to content

Instantly share code, notes, and snippets.

@dyguests
Created March 5, 2020 14:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dyguests/ffc8ecef9346c45a080a74c01f7412f7 to your computer and use it in GitHub Desktop.
Save dyguests/ffc8ecef9346c45a080a74c01f7412f7 to your computer and use it in GitHub Desktop.
Unity Inputer2DHelper
using System;
using UnityEngine;
namespace Lina.Scripts
{
public struct Inputer2DHelper
{
private Vector2 move;
private bool fire1;
private bool fire1Down;
private bool fire1Up;
private bool fire2;
private bool fire2Down;
private bool fire2Up;
private bool fire3;
private bool fire3Down;
private bool fire3Up;
private bool jump;
private bool jumpDown;
private bool jumpUp;
/// <summary>
/// 接收输入
/// </summary>
public void Refresh()
{
float horizontal = Input.GetAxis("Horizontal");
float vertical = Input.GetAxis("Vertical");
float fire1Axis = Input.GetAxis("Fire1");
float fire2Axis = Input.GetAxis("Fire2");
float fire3Axis = Input.GetAxis("Fire3");
float jumpAxis = Input.GetAxis("Jump");
move.x = horizontal;
move.y = vertical;
var newFire1 = Math.Abs(fire1Axis - 1f) < 0.01f;
fire1Down = !fire1 && newFire1;
fire1Up = fire1 && !newFire1;
fire1 = newFire1;
var newFire2 = Math.Abs(fire2Axis - 1f) < 0.01f;
fire2Down = !fire2 && newFire2;
fire2Up = fire2 && !newFire2;
fire2 = newFire2;
var newFire3 = Math.Abs(fire3Axis - 1f) < 0.01f;
fire3Down = !fire3 && newFire3;
fire3Up = fire3 && !newFire3;
fire3 = newFire3;
var newJump = Math.Abs(jumpAxis - 1f) < 0.01f;
jumpDown = !jump && newJump;
jumpUp = jump && !newJump;
jump = newJump;
}
public Vector2 Move => move;
public bool Fire1 => fire1;
public bool Fire1Down => fire1Down;
public bool Fire1Up => fire1Up;
public bool Fire2 => fire2;
public bool Fire2Down => fire2Down;
public bool Fire2Up => fire2Up;
public bool Fire3 => fire3;
public bool Fire3Down => fire3Down;
public bool Fire3Up => fire3Up;
public bool Jump => jump;
public bool JumpDown => jumpDown;
public bool JumpUp => jumpUp;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment