Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //There are four classes:
- // -OptionsManager
- // -SavingSystem
- // -ControlsData - This is where the player is getting the KeyCodes.
- // -KeyID - This is an id for the buttons on the options menu.
- //-----OptionsManager-----//
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using TMPro;
- using UnityEngine;
- using UnityEngine.UI;
- using Invaders.Controls;
- using Invaders.Enums;
- using Invaders.MainMenu;
- using Invaders.Save;
- using System.IO;
- namespace Invaders.Managers
- {
- public class OptionsManager : MonoBehaviour
- {
- private bool _canEdit = true;
- private Button _selectedButton = null;
- private Navigation _navigation = new Navigation();
- private Dictionary<KeyIDEnum, KeyCode> _controls = new Dictionary<KeyIDEnum, KeyCode>();
- private const string ControlsSaveFile = "/Controls.sav";
- private void Awake()
- {
- InitializeControls(ControlsSaveFile);
- }
- private void InitializeControls(string saveFile)
- {
- if (File.Exists(SavingSystem.GetSavePath(saveFile)))
- {
- _controls = SavingSystem.Load(saveFile) as Dictionary<KeyIDEnum, KeyCode>;
- }
- else
- {
- CreateDefaultControls(saveFile);
- }
- ControlsData.SetControls(_controls);
- }
- private void CreateDefaultControls(string saveFile)
- {
- _controls.Clear();
- _controls.Add(KeyIDEnum.Left, KeyCode.LeftArrow);
- _controls.Add(KeyIDEnum.Right, KeyCode.RightArrow);
- _controls.Add(KeyIDEnum.Shoot, KeyCode.Space);
- SavingSystem.Save(saveFile, _controls);
- }
- //ONCLICK EVENT OPTIONS BUTTON
- public void LoadControlsUI()
- {
- ReloadControlsUI(_controls);
- }
- private void ReloadControlsUI(Dictionary<KeyIDEnum, KeyCode> controls)
- {
- foreach(Transform t in KeyID.GetButtonsList)
- {
- var buttonKeyID = t.GetComponent<KeyID>().GetKeyEnumID;
- var fieldTextUI = t.GetChild(0).GetComponent<TextMeshProUGUI>();
- foreach(KeyIDEnum controlKeyPair in controls.Keys)
- {
- if(buttonKeyID == controlKeyPair)
- {
- fieldTextUI.text = controls[controlKeyPair].ToString().ToUpper();
- }
- }
- }
- }
- //ONCLICK EVENT DEFAULT BUTTON
- public void ResetDefaults()
- {
- CreateDefaultControls(ControlsSaveFile);
- ReloadControlsUI(_controls);
- }
- //ONCLICK EVENT BINDABLE KEYS BUTTONS
- public void StartKeybindRoutine()
- {
- if (_canEdit)
- {
- _canEdit = false;
- _selectedButton = MenuManager.CurrentSelectedButton;
- SetButtonNavigationMode(Navigation.Mode.None);
- StartCoroutine(KeyListener());
- }
- }
- private IEnumerator KeyListener()
- {
- yield return new WaitUntil(() => Input.anyKeyDown);
- yield return new WaitUntil(() => Input.anyKeyDown);
- if (Input.GetKeyDown(KeyCode.Escape))
- {
- CancelEdit();
- yield break;
- }
- foreach (KeyCode vKey in Enum.GetValues(typeof(KeyCode)))
- {
- if (Input.GetKey(vKey))
- {
- if(_controls.ContainsValue(vKey))
- {
- CancelEdit();
- yield break;
- }
- SetNewControls(vKey);
- }
- }
- }
- private void CancelEdit()
- {
- _canEdit = true;
- SetButtonNavigationMode(Navigation.Mode.Automatic);
- ReloadControlsUI(_controls);
- }
- private void SetNewControls(KeyCode vKey)
- {
- var keyID = _selectedButton.GetComponent<KeyID>().GetKeyEnumID;
- _controls[keyID] = vKey;
- SavingSystem.Save(ControlsSaveFile, _controls);
- _canEdit = true;
- SetButtonNavigationMode(Navigation.Mode.Automatic);
- ControlsData.SetControls(_controls);
- ReloadControlsUI(_controls);
- }
- private void SetButtonNavigationMode(Navigation.Mode mode)
- {
- _navigation.mode = mode;
- _selectedButton.navigation = _navigation;
- }
- }
- }
- //-----SavingSystem-----//
- using UnityEngine;
- using System.IO;
- using System.Runtime.Serialization.Formatters.Binary;
- namespace Invaders.Save
- {
- public static class SavingSystem
- {
- public static void Save(string saveFile, object data)
- {
- using (FileStream stream = File.Open(GetSavePath(saveFile), FileMode.Create))
- {
- BinaryFormatter formatter = new BinaryFormatter();
- formatter.Serialize(stream, data);
- }
- }
- public static object Load(string saveFile)
- {
- using (FileStream stream = File.Open(GetSavePath(saveFile), FileMode.Open))
- {
- BinaryFormatter formatter = new BinaryFormatter();
- return formatter.Deserialize(stream);
- }
- }
- public static string GetSavePath(string saveFile)
- {
- return Path.Combine(Application.persistentDataPath + saveFile);
- }
- }
- }
- //-----ControlsData-----//
- using UnityEngine;
- using System.Collections.Generic;
- using Invaders.Enums;
- namespace Invaders.Controls
- {
- public class ControlsData
- {
- public static Dictionary<KeyIDEnum, KeyCode> CurrentControlsMap { get; private set; }
- public static void SetControls(Dictionary<KeyIDEnum, KeyCode> controls)
- {
- CurrentControlsMap = controls;
- }
- }
- }
- //-----KeyID-----//
- using UnityEngine;
- using System.Collections.Generic;
- using Invaders.Enums;
- namespace Invaders.MainMenu
- {
- public enum KeyIDEnum
- {
- None,
- Left,
- Right,
- Shoot
- }
- public class KeyID : MonoBehaviour
- {
- [SerializeField] private KeyIDEnum _key = KeyIDEnum.None;
- public KeyIDEnum GetKeyEnumID => _key;
- private static readonly List<Transform> _buttons = new List<Transform>();
- public static List<Transform> GetButtonsList { get; private set; }
- private void Awake()
- {
- _buttons.Add(transform);
- GetButtonsList = _buttons;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement