diff --git a/Assets/Prefabs/Block.prefab b/Assets/Prefabs/Block.prefab index 749259bd78074743a7689ccc3c5fca39ced21c91..5823e86e857073432b95844c7b79bebc37680670 100644 --- a/Assets/Prefabs/Block.prefab +++ b/Assets/Prefabs/Block.prefab @@ -138,7 +138,7 @@ MonoBehaviour: m_EditorClassIdentifier: RotateAnchorPrefab: {fileID: 2874721107243629612, guid: 12accfaeb3e281a48acb41e0100ee76d, type: 3} inputTransform: {fileID: 0} - activateRotation: {fileID: 4987378303488695193, guid: c348712bda248c246b8c49b3db54643f, type: 3} + activateRotationEvent: {fileID: 4987378303488695193, guid: c348712bda248c246b8c49b3db54643f, type: 3} --- !u!114 &7216982225745790517 MonoBehaviour: m_ObjectHideFlags: 0 diff --git a/Assets/Scenes/SampleScene.unity b/Assets/Scenes/SampleScene.unity index 009d271133c187eddea58fd0a5cc948d8c2ae7dc..d796b1d23c956264e3e6e0c633b53cda59ed87bc 100644 --- a/Assets/Scenes/SampleScene.unity +++ b/Assets/Scenes/SampleScene.unity @@ -4494,7 +4494,7 @@ MonoBehaviour: m_EditorClassIdentifier: spawnPrefab: {fileID: 8126816444565180231, guid: f24fe5caf4a57c148bd86d63b19a1248, type: 3} inputTransform: {fileID: 1044421913} - triggerEventAction: {fileID: 4987378303488695193, guid: c348712bda248c246b8c49b3db54643f, type: 3} + SpawnEventAction: {fileID: 4987378303488695193, guid: c348712bda248c246b8c49b3db54643f, type: 3} programController: {fileID: 426651922} --- !u!4 &426651920 Transform: diff --git a/Assets/Scrips/Components/Block/BlockRotate.cs b/Assets/Scrips/Components/Block/BlockRotate.cs index ff692953e7250ee65f89ef4c84d56c4274aed3be..f8c99d918a3c366df952942252b2b49bd8d25a2c 100644 --- a/Assets/Scrips/Components/Block/BlockRotate.cs +++ b/Assets/Scrips/Components/Block/BlockRotate.cs @@ -9,7 +9,9 @@ public class BlockRotate : MonoBehaviour { public GameObject RotateAnchorPrefab; public Transform inputTransform; - public InputActionReference activateRotation; + public InputActionReference activateRotationEvent; + + private InputLoader _inputActivateRotation; private GameObject _rotateAnchor; private XRSimpleInteractable _simpleInteractable; @@ -49,9 +51,7 @@ public class BlockRotate : MonoBehaviour _simpleInteractable.hoverEntered.AddListener(HandleHoverEnter); _simpleInteractable.hoverExited.AddListener(HandleHoverExit); - activateRotation.action.Enable(); - activateRotation.action.performed += AcitvateRotationMode; - InputSystem.onDeviceChange += OnDeviceChange; + _inputActivateRotation = new(activateRotationEvent, AcitvateRotationMode); this._meshRenderer = GetComponent<MeshRenderer>(); _meshRenderer.material = new Material(_meshRenderer.material); @@ -86,28 +86,6 @@ public class BlockRotate : MonoBehaviour } } - private void OnDestroy() - { - activateRotation.action.Disable(); - activateRotation.action.performed -= AcitvateRotationMode; - InputSystem.onDeviceChange -= OnDeviceChange; - } - - private void OnDeviceChange(InputDevice device, InputDeviceChange change) - { - switch (change) - { - case InputDeviceChange.Disconnected: - activateRotation.action.Disable(); - activateRotation.action.performed -= AcitvateRotationMode; - break; - case InputDeviceChange.Reconnected: - activateRotation.action.Enable(); - activateRotation.action.performed += AcitvateRotationMode; - break; - } - } - private void FixedUpdate() { EditState.EditType mode = EditState.Instance._editState; @@ -122,9 +100,4 @@ public class BlockRotate : MonoBehaviour _rotateAnchor.SetActive(false); } - // Update is called once per frame - void Update() - { - - } } diff --git a/Assets/Scrips/Contexts/EditState.cs b/Assets/Scrips/Contexts/EditState.cs index 229ad29c58c524bb5ac4e4006ecbcdc6c925b7e5..2e3122570145b366afad1e511216be8617d51b69 100644 --- a/Assets/Scrips/Contexts/EditState.cs +++ b/Assets/Scrips/Contexts/EditState.cs @@ -8,6 +8,8 @@ public class EditState : MonoBehaviour public static EditState Instance { get; private set; } public InputActionReference editStateAction; + private InputLoader _inputEditState; + public enum EditType { Deform = 0, @@ -33,42 +35,7 @@ public class EditState : MonoBehaviour Instance = this; } - editStateAction.action.Enable(); - editStateAction.action.performed += SwitchState; - InputSystem.onDeviceChange += OnDeviceChange; - } - - private void OnDestroy() - { - editStateAction.action.Disable(); - editStateAction.action.performed -= SwitchState; - InputSystem.onDeviceChange -= OnDeviceChange; - } - - private void OnDeviceChange(InputDevice device, InputDeviceChange change) - { - switch (change) - { - case InputDeviceChange.Disconnected: - editStateAction.action.Disable(); - editStateAction.action.performed -= SwitchState; - break; - case InputDeviceChange.Reconnected: - editStateAction.action.Enable(); - editStateAction.action.performed += SwitchState; - break; - } - } - - // Start is called before the first frame update - void Start() - { + _inputEditState = new(editStateAction, SwitchState); _editState = EditType.Deform; } - - // Update is called once per frame - void Update() - { - - } } diff --git a/Assets/Scrips/Controllers/ProgramController.cs b/Assets/Scrips/Controllers/ProgramController.cs index f29d85cfbefdba3ebe7da7074a033187de4b543d..4391a348452be898bc45fa2d91605f2637a1fb2a 100644 --- a/Assets/Scrips/Controllers/ProgramController.cs +++ b/Assets/Scrips/Controllers/ProgramController.cs @@ -15,59 +15,26 @@ public class ProgramController : MonoBehaviour public InputActionReference saveEventAction; public InputActionReference loadEventAction; + private InputLoader _inputSave; + private InputLoader _inputLoad; - private List<GameObject> _spawnedBlocks = new(); + private readonly List<GameObject> _spawnedBlocks = new(); private string _savePath; + private SurrogateSelector _surrogateSelector; + private BinaryFormatter _binaryFormatter; private void Awake() { this._savePath = Application.persistentDataPath + "/program.save"; - saveEventAction.action.Enable(); - saveEventAction.action.performed += SaveProgram; + _inputSave = new(saveEventAction, SaveProgram); + _inputLoad = new(loadEventAction, LoadProgram); - loadEventAction.action.Enable(); - loadEventAction.action.performed += LoadProgram; - - // InputSystem.onDeviceChange += OnDeviceChange; - } - - private void OnDestroy() - { - saveEventAction.action.Disable(); - saveEventAction.action.performed -= SaveProgram; - - loadEventAction.action.Disable(); - loadEventAction.action.performed -= LoadProgram; - - // InputSystem.onDeviceChange -= OnDeviceChange; - } - - private void OnDeviceChange(InputDevice device, InputDeviceChange change) - { - switch (change) - { - case InputDeviceChange.Disconnected: - saveEventAction.action.Disable(); - saveEventAction.action.performed -= SaveProgram; - break; - case InputDeviceChange.Reconnected: - saveEventAction.action.Enable(); - saveEventAction.action.performed += SaveProgram; - break; - } - } - - // Start is called before the first frame update - void Start() - { - - } - - // Update is called once per frame - void Update() - { + _surrogateSelector = new(); + _surrogateSelector.AddSurrogate(typeof(Vector3), new StreamingContext(StreamingContextStates.All), new Vector3SerializationSurrogate()); + _binaryFormatter = new(); + _binaryFormatter.SurrogateSelector = _surrogateSelector; } public void RegisterBlock(GameObject block) @@ -110,14 +77,9 @@ public class ProgramController : MonoBehaviour public void SaveProgram(InputAction.CallbackContext context) { Save save = CreateSave(); - SurrogateSelector surrogateSelector = new(); - surrogateSelector.AddSurrogate(typeof(Vector3), new StreamingContext(StreamingContextStates.All), new Vector3SerializationSurrogate()); - - BinaryFormatter binaryFormatter = new(); - binaryFormatter.SurrogateSelector = surrogateSelector; FileStream fs = File.Create(this._savePath); - binaryFormatter.Serialize(fs, save); + _binaryFormatter.Serialize(fs, save); fs.Close(); } @@ -134,14 +96,9 @@ public class ProgramController : MonoBehaviour _spawnedBlocks.Clear(); } - SurrogateSelector surrogateSelector = new(); - surrogateSelector.AddSurrogate(typeof(Vector3), new StreamingContext(StreamingContextStates.All), new Vector3SerializationSurrogate()); - - BinaryFormatter binaryFormatter = new(); - binaryFormatter.SurrogateSelector = surrogateSelector; FileStream fs = File.Open(this._savePath, FileMode.Open); - Save save = (Save)binaryFormatter.Deserialize(fs); + Save save = (Save)_binaryFormatter.Deserialize(fs); fs.Close(); UserTransform.position = save.UserTransform[0]; diff --git a/Assets/Scrips/Events/SpawnBlockEvent.cs b/Assets/Scrips/Events/SpawnBlockEvent.cs index 037e70af41f3ae0fbacedde1c83918186ddfc01c..f058592c0d383bc4a586628fa9e50ec759c2bd96 100644 --- a/Assets/Scrips/Events/SpawnBlockEvent.cs +++ b/Assets/Scrips/Events/SpawnBlockEvent.cs @@ -9,24 +9,16 @@ public class SpawnBlockEvent : MonoBehaviour { public GameObject spawnPrefab; public Transform inputTransform; - public InputActionReference triggerEventAction; + public InputActionReference SpawnEventAction; + + private InputLoader _inputSpawn; public ProgramController programController; private void Awake() { spawnPrefab.GetComponent<BlockRotate>().inputTransform = this.inputTransform; - - triggerEventAction.action.Enable(); - triggerEventAction.action.performed += SpawnMesh; - InputSystem.onDeviceChange += OnDeviceChange; - } - - private void OnDestroy() - { - triggerEventAction.action.Disable(); - triggerEventAction.action.performed -= SpawnMesh; - InputSystem.onDeviceChange -= OnDeviceChange; + _inputSpawn = new(SpawnEventAction, SpawnMesh); } private void SpawnMesh(InputAction.CallbackContext context) @@ -68,21 +60,6 @@ public class SpawnBlockEvent : MonoBehaviour } } - private void OnDeviceChange(InputDevice device, InputDeviceChange change) - { - switch (change) - { - case InputDeviceChange.Disconnected: - triggerEventAction.action.Disable(); - triggerEventAction.action.performed -= SpawnMesh; - break; - case InputDeviceChange.Reconnected: - triggerEventAction.action.Enable(); - triggerEventAction.action.performed += SpawnMesh; - break; - } - } - // Start is called before the first frame update void Start() { @@ -95,9 +72,4 @@ public class SpawnBlockEvent : MonoBehaviour // programController.RegisterBlock(spawnedObject); } - // Update is called once per frame - void Update() - { - - } } diff --git a/Assets/Scrips/Utils/InputLoader.cs b/Assets/Scrips/Utils/InputLoader.cs new file mode 100644 index 0000000000000000000000000000000000000000..a6cff116358148e91c996400f9fc22abbc083aaf --- /dev/null +++ b/Assets/Scrips/Utils/InputLoader.cs @@ -0,0 +1,42 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using UnityEngine; +using UnityEngine.InputSystem; + +public class InputLoader +{ + public InputActionReference InputActionEvent { get; set; } + public Action<InputAction.CallbackContext> InputActionMethod { get; set; } + + public InputLoader(InputActionReference inputActionEvent, Action<InputAction.CallbackContext> inputActionMethod) + { + this.InputActionEvent = inputActionEvent; + this.InputActionMethod = inputActionMethod; + + this.InputActionEvent.action.Enable(); + this.InputActionEvent.action.performed += this.InputActionMethod; + InputSystem.onDeviceChange += OnDeviceChange; + } + + ~InputLoader() + { + this.InputActionEvent.action.Disable(); + this.InputActionEvent.action.performed -= this.InputActionMethod; + } + + private void OnDeviceChange(InputDevice device, InputDeviceChange change) + { + switch (change) + { + case InputDeviceChange.Disconnected: + InputActionEvent.action.Disable(); + InputActionEvent.action.performed -= InputActionMethod; + break; + case InputDeviceChange.Reconnected: + InputActionEvent.action.Enable(); + InputActionEvent.action.performed += InputActionMethod; + break; + } + } +} diff --git a/Assets/Scrips/Utils/InputLoader.cs.meta b/Assets/Scrips/Utils/InputLoader.cs.meta new file mode 100644 index 0000000000000000000000000000000000000000..b24c89ce1cc5fc601238b808dade23f888c48a95 --- /dev/null +++ b/Assets/Scrips/Utils/InputLoader.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: b2c05abe52712084c858d3df570f5526 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: