Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
0.6.1 InputActionのコールバック購読をIDisposableで扱うための拡張メソッド
- Loading branch information
Showing
3 changed files
with
92 additions
and
1 deletion.
There are no files selected for viewing
80 changes: 80 additions & 0 deletions
80
Assets/Package/Runtime/InputSystem/InputActionExtension.cs
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using UnityEngine; | ||
using UnityEngine.InputSystem; | ||
|
||
namespace TSKT | ||
{ | ||
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; | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
Assets/Package/Runtime/InputSystem/InputActionExtension.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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