Skip to content

Commit

Permalink
Feat: Input System Test
Browse files Browse the repository at this point in the history
  • Loading branch information
fkdl0048 committed Oct 14, 2023
1 parent 6dbd0af commit 021a799
Show file tree
Hide file tree
Showing 6 changed files with 286 additions and 26 deletions.
33 changes: 17 additions & 16 deletions .idea/.idea.GameOver/.idea/workspace.xml

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

3 changes: 2 additions & 1 deletion Assets/InputSystem.inputsettings.asset
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: c46f07b5ed07e4e92aa78254188d3d10, type: 3}
m_Name: InputSystem.inputsettings
m_EditorClassIdentifier:
m_SupportedDevices: []
m_SupportedDevices:
- Keyboard
m_UpdateMode: 1
m_MaxEventBytesPerUpdate: 5242880
m_MaxQueuedEventsPerUpdate: 1000
Expand Down
128 changes: 127 additions & 1 deletion Assets/New Controls.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,85 @@ public @NewControls()
{
asset = InputActionAsset.FromJson(@"{
""name"": ""New Controls"",
""maps"": [],
""maps"": [
{
""name"": ""Player"",
""id"": ""dd99f131-9b99-4bda-830e-e7b186eedf41"",
""actions"": [
{
""name"": ""Move"",
""type"": ""Value"",
""id"": ""4b02cb89-94ea-4d58-859a-34300ddb5b09"",
""expectedControlType"": ""Vector2"",
""processors"": """",
""interactions"": """",
""initialStateCheck"": true
}
],
""bindings"": [
{
""name"": ""2D Vector"",
""id"": ""be7b3ab5-89a9-4470-88eb-cfe77b258e98"",
""path"": ""2DVector"",
""interactions"": """",
""processors"": """",
""groups"": """",
""action"": ""Move"",
""isComposite"": true,
""isPartOfComposite"": false
},
{
""name"": ""up"",
""id"": ""cc8cce14-e67c-4f3a-bd36-7d81b200e962"",
""path"": ""<Keyboard>/w"",
""interactions"": """",
""processors"": """",
""groups"": """",
""action"": ""Move"",
""isComposite"": false,
""isPartOfComposite"": true
},
{
""name"": ""down"",
""id"": ""9382b306-bb74-4e3a-a7b2-a9c6e48e8dc7"",
""path"": ""<Keyboard>/s"",
""interactions"": """",
""processors"": """",
""groups"": """",
""action"": ""Move"",
""isComposite"": false,
""isPartOfComposite"": true
},
{
""name"": ""left"",
""id"": ""0802d62f-0a3e-4546-9437-d24265331894"",
""path"": ""<Keyboard>/a"",
""interactions"": """",
""processors"": """",
""groups"": """",
""action"": ""Move"",
""isComposite"": false,
""isPartOfComposite"": true
},
{
""name"": ""right"",
""id"": ""3f36ea06-cbd6-4a87-a538-ed89c4218e73"",
""path"": ""<Keyboard>/d"",
""interactions"": """",
""processors"": """",
""groups"": """",
""action"": ""Move"",
""isComposite"": false,
""isPartOfComposite"": true
}
]
}
],
""controlSchemes"": []
}");
// Player
m_Player = asset.FindActionMap("Player", throwIfNotFound: true);
m_Player_Move = m_Player.FindAction("Move", throwIfNotFound: true);
}

public void Dispose()
Expand Down Expand Up @@ -82,4 +158,54 @@ public int FindBinding(InputBinding bindingMask, out InputAction action)
{
return asset.FindBinding(bindingMask, out action);
}

// Player
private readonly InputActionMap m_Player;
private List<IPlayerActions> m_PlayerActionsCallbackInterfaces = new List<IPlayerActions>();
private readonly InputAction m_Player_Move;
public struct PlayerActions
{
private @NewControls m_Wrapper;
public PlayerActions(@NewControls wrapper) { m_Wrapper = wrapper; }
public InputAction @Move => m_Wrapper.m_Player_Move;
public InputActionMap Get() { return m_Wrapper.m_Player; }
public void Enable() { Get().Enable(); }
public void Disable() { Get().Disable(); }
public bool enabled => Get().enabled;
public static implicit operator InputActionMap(PlayerActions set) { return set.Get(); }
public void AddCallbacks(IPlayerActions instance)
{
if (instance == null || m_Wrapper.m_PlayerActionsCallbackInterfaces.Contains(instance)) return;
m_Wrapper.m_PlayerActionsCallbackInterfaces.Add(instance);
@Move.started += instance.OnMove;
@Move.performed += instance.OnMove;
@Move.canceled += instance.OnMove;
}

private void UnregisterCallbacks(IPlayerActions instance)
{
@Move.started -= instance.OnMove;
@Move.performed -= instance.OnMove;
@Move.canceled -= instance.OnMove;
}

public void RemoveCallbacks(IPlayerActions instance)
{
if (m_Wrapper.m_PlayerActionsCallbackInterfaces.Remove(instance))
UnregisterCallbacks(instance);
}

public void SetCallbacks(IPlayerActions instance)
{
foreach (var item in m_Wrapper.m_PlayerActionsCallbackInterfaces)
UnregisterCallbacks(item);
m_Wrapper.m_PlayerActionsCallbackInterfaces.Clear();
AddCallbacks(instance);
}
}
public PlayerActions @Player => new PlayerActions(this);
public interface IPlayerActions
{
void OnMove(InputAction.CallbackContext context);
}
}
79 changes: 78 additions & 1 deletion Assets/New Controls.inputactions
Original file line number Diff line number Diff line change
@@ -1 +1,78 @@
{}
{
"name": "New Controls",
"maps": [
{
"name": "Player",
"id": "dd99f131-9b99-4bda-830e-e7b186eedf41",
"actions": [
{
"name": "Move",
"type": "Value",
"id": "4b02cb89-94ea-4d58-859a-34300ddb5b09",
"expectedControlType": "Vector2",
"processors": "",
"interactions": "",
"initialStateCheck": true
}
],
"bindings": [
{
"name": "2D Vector",
"id": "be7b3ab5-89a9-4470-88eb-cfe77b258e98",
"path": "2DVector",
"interactions": "",
"processors": "",
"groups": "",
"action": "Move",
"isComposite": true,
"isPartOfComposite": false
},
{
"name": "up",
"id": "cc8cce14-e67c-4f3a-bd36-7d81b200e962",
"path": "<Keyboard>/w",
"interactions": "",
"processors": "",
"groups": "",
"action": "Move",
"isComposite": false,
"isPartOfComposite": true
},
{
"name": "down",
"id": "9382b306-bb74-4e3a-a7b2-a9c6e48e8dc7",
"path": "<Keyboard>/s",
"interactions": "",
"processors": "",
"groups": "",
"action": "Move",
"isComposite": false,
"isPartOfComposite": true
},
{
"name": "left",
"id": "0802d62f-0a3e-4546-9437-d24265331894",
"path": "<Keyboard>/a",
"interactions": "",
"processors": "",
"groups": "",
"action": "Move",
"isComposite": false,
"isPartOfComposite": true
},
{
"name": "right",
"id": "3f36ea06-cbd6-4a87-a538-ed89c4218e73",
"path": "<Keyboard>/d",
"interactions": "",
"processors": "",
"groups": "",
"action": "Move",
"isComposite": false,
"isPartOfComposite": true
}
]
}
],
"controlSchemes": []
}
54 changes: 48 additions & 6 deletions Assets/Player.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,60 @@
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;

public class Player : MonoBehaviour
{
// Start is called before the first frame update
void Start()
InputAction movement;
NewControls controls;

void Awake()
{

controls = new NewControls();
movement = controls.Player.Move;
}

void OnEnable()
{
movement.Enable();
movement.started += Started;
movement.performed += Performed;
movement.canceled += Canceled;
}

void OnDisable()
{
movement.Disable();
movement.started -= Started;
movement.performed -= Performed;
movement.canceled -= Canceled;
}

// Update is called once per frame
void Update()
private void FixedUpdate()
{

Vector2 move = movement.ReadValue<Vector2>();
Move(move.x, move.y);
}

private void Started(InputAction.CallbackContext obj)
{
Debug.Log("Started");
}

private void Performed(InputAction.CallbackContext obj)
{
Debug.Log("Performed");
}

private void Canceled(InputAction.CallbackContext obj)
{
Debug.Log("Canceled");
}

private void Move(float x, float y)
{
this.transform.position = new Vector2(this.transform.position.x + x, this.transform.position.y + y);
}
}

15 changes: 14 additions & 1 deletion Assets/Scenes/Game.unity
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ RenderSettings:
m_ReflectionIntensity: 1
m_CustomReflection: {fileID: 0}
m_Sun: {fileID: 0}
m_IndirectSpecularColor: {r: 0.44657826, g: 0.49641263, b: 0.57481676, a: 1}
m_IndirectSpecularColor: {r: 0.44657898, g: 0.4964133, b: 0.5748178, a: 1}
m_UseRadianceAmbientProbe: 0
--- !u!157 &3
LightmapSettings:
Expand Down Expand Up @@ -135,6 +135,7 @@ GameObject:
- component: {fileID: 878665220}
- component: {fileID: 878665219}
- component: {fileID: 878665218}
- component: {fileID: 878665222}
m_Layer: 0
m_Name: Cube
m_TagString: Untagged
Expand Down Expand Up @@ -228,6 +229,18 @@ Transform:
m_Children: []
m_Father: {fileID: 0}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &878665222
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 878665217}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 31097a75830dc3c49978f8d2637cf483, type: 3}
m_Name:
m_EditorClassIdentifier:
--- !u!1 &1055525152
GameObject:
m_ObjectHideFlags: 0
Expand Down

0 comments on commit 021a799

Please sign in to comment.