Skip to content

Commit

Permalink
0.6.1 InputActionのコールバック購読をIDisposableで扱うための拡張メソッド
Browse files Browse the repository at this point in the history
  • Loading branch information
enue committed Jun 6, 2021
1 parent e405db2 commit 0c6f61b
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 1 deletion.
80 changes: 80 additions & 0 deletions Assets/Package/Runtime/InputSystem/InputActionExtension.cs
@@ -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 Assets/Package/Runtime/InputSystem/InputActionExtension.cs.meta

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

2 changes: 1 addition & 1 deletion Assets/Package/package.json
@@ -1,7 +1,7 @@
{
"name": "com.github.enue.tskt_input",
"displayName": "TSKT Input",
"version": "0.6.0",
"version": "0.6.1",
"unity": "2021.1",
"description": "",
"keywords": [],
Expand Down

0 comments on commit 0c6f61b

Please sign in to comment.