Last active
June 6, 2021 08:40
-
-
Save enue/726212e3ef26d5e19d6faec0b6e895e0 to your computer and use it in GitHub Desktop.
subscribe InputAction callbacks
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 System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
using UnityEngine.InputSystem; | |
public static class InputActionExtension | |
{ | |
static public System.IDisposable SubscribePerform(this InputAction action, | |
System.Action<InputAction.CallbackContext> callback) | |
{ | |
return new SubscribeInputActionPerform(action, callback); | |
} | |
static public System.IDisposable SubscribeStart(this InputAction action, | |
System.Action<InputAction.CallbackContext> callback) | |
{ | |
return new SubscribeInputActionStart(action, callback); | |
} | |
static public System.IDisposable SubscribeCancel(this InputAction action, | |
System.Action<InputAction.CallbackContext> callback) | |
{ | |
return new SubscribeInputActionCancel(action, callback); | |
} | |
} | |
public class SubscribeInputActionPerform : System.IDisposable | |
{ | |
readonly InputAction action; | |
readonly System.Action<InputAction.CallbackContext> callback; | |
public SubscribeInputActionPerform(InputAction action, System.Action<InputAction.CallbackContext> callback) | |
{ | |
action.performed += callback; | |
this.action = action; | |
this.callback = callback; | |
} | |
public void Dispose() | |
{ | |
action.performed -= callback; | |
} | |
} | |
public class SubscribeInputActionStart : System.IDisposable | |
{ | |
readonly InputAction action; | |
readonly System.Action<InputAction.CallbackContext> callback; | |
public SubscribeInputActionStart(InputAction action, System.Action<InputAction.CallbackContext> callback) | |
{ | |
action.started += callback; | |
this.action = action; | |
this.callback = callback; | |
} | |
public void Dispose() | |
{ | |
action.started -= callback; | |
} | |
} | |
public class SubscribeInputActionCancel : System.IDisposable | |
{ | |
readonly InputAction action; | |
readonly System.Action<InputAction.CallbackContext> callback; | |
public SubscribeInputActionCancel(InputAction action, System.Action<InputAction.CallbackContext> callback) | |
{ | |
action.canceled += callback; | |
this.action = action; | |
this.callback = callback; | |
} | |
public void Dispose() | |
{ | |
action.canceled -= callback; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment