I want to create a loading system that will know the priority of some systems and will wait for them to instantiate first. For example, I need it to wait for my input manager to load on game boot and then load other stuff like Scene, and UI and PlayerController that rely on the input manager instance. I would also like to put a loading % if it’s possible e.g. for loading the scene
I’m implementing InputManager and UIManager as singletones. I’ll put the code for those below
InputManager:
using System;
using UnityEngine;
using UnityEngine.InputSystem;
public class InputReader : MonoBehaviour, InputSystemActions.IPlayerActions, InputSystemActions.IDialogueActions
{
[SerializeField] DialogueChannel dialogueChannel;
private InputSystemActions inputActions;
public event Action<Vector2> MoveEvent;
public event Action<Vector2> LookEvent;
public event Action JumpEvent;
public event Action InteractEvent;
public event Action SprintStartEvent;
public event Action SprintEndEvent;
public event Action PauseEvent;
public event Action ContinueEvent;
public event Action CallInventoryEvent;
private InputActionMap currentInputActionMap;
public static InputReader Instance { get; private set; }
private void Awake()
{
if (Instance == null)
{
Instance = this;
DontDestroyOnLoad(gameObject);
Debug.Log("Input Reader Created");
}
else
{
Destroy(gameObject);
}
}
private void OnEnable()
{
if (inputActions == null)
{
inputActions = new InputSystemActions();
inputActions.Player.SetCallbacks(this);
inputActions.Dialogue.SetCallbacks(this);
inputActions.Player.Enable();
}
dialogueChannel.OnDialogueStart += EnableDialogueScheme;
dialogueChannel.OnDialogueEnd += DisableDialogueScheme;
}
private void OnDisable()
{
inputActions.Disable();
dialogueChannel.OnDialogueStart -= EnableDialogueScheme;
dialogueChannel.OnDialogueEnd -= DisableDialogueScheme;
}
public void OnMove(InputAction.CallbackContext context)
{
MoveEvent?.Invoke(context.ReadValue<Vector2>());
}
public void OnLook(InputAction.CallbackContext context)
{
LookEvent?.Invoke(context.ReadValue<Vector2>());
}
public void OnSprint(InputAction.CallbackContext context)
{
if (context.phase == InputActionPhase.Performed)
{
SprintStartEvent?.Invoke();
}
else if (context.phase == InputActionPhase.Canceled)
{
SprintEndEvent?.Invoke();
}
}
public void OnJump(InputAction.CallbackContext context)
{
if (context.phase == InputActionPhase.Performed)
{
JumpEvent?.Invoke();
}
}
public void OnInteract(InputAction.CallbackContext context)
{
if (context.phase == InputActionPhase.Performed)
{
InteractEvent?.Invoke();
}
}
public void OnPause(InputAction.CallbackContext context)
{
if (context.phase == InputActionPhase.Performed)
{
PauseEvent?.Invoke();
}
}
public void OnInventory(InputAction.CallbackContext context)
{
if (context.phase == InputActionPhase.Performed)
{
CallInventoryEvent?.Invoke();
Debug.Log("Call Inventory Event");
}
}
public void OnContinue(InputAction.CallbackContext context)
{
if (context.phase == InputActionPhase.Performed)
{
ContinueEvent?.Invoke();
Debug.Log("Continue to the next node");
}
}
private void EnableDialogueScheme(DialogueSO dialogue)
{
inputActions.Disable();
inputActions.Dialogue.Enable();
}
private void DisableDialogueScheme(DialogueSO dialogue)
{
inputActions.Enable();
inputActions.Dialogue.Disable();
}
}
UIManager:
using System;
using UnityEngine;
public class UIManager : MonoBehaviour
{
[Header("Dialogue")]
[SerializeField] DialogueChannel dialogueChannel;
[SerializeField] GameObject dialogueUI;
[Header("Inventory")]
[SerializeField] GameObject inventoryUI;
private bool showInventoryUI = false;
public static UIManager Instance { get; private set; }
private void Awake()
{
if (Instance == null)
{
Instance = this;
DontDestroyOnLoad(gameObject);
}
else
{
Destroy(gameObject);
}
}
private void OnEnable()
{
dialogueChannel.OnDialogueStart += ShowDialogueUI;
dialogueChannel.OnDialogueEnd += HideDialogueUI;
InputReader.Instance.CallInventoryEvent += ToggleInventoryUI;
}
private void OnDisable()
{
dialogueChannel.OnDialogueStart -= ShowDialogueUI;
dialogueChannel.OnDialogueEnd -= HideDialogueUI;
InputReader.Instance.CallInventoryEvent -= ToggleInventoryUI;
}
private void ShowDialogueUI(DialogueSO dialogue)
{
dialogueUI.SetActive(true);
}
private void HideDialogueUI(DialogueSO dialogue)
{
dialogueUI.SetActive(false);
}
private void ToggleInventoryUI()
{
showInventoryUI = !showInventoryUI;
inventoryUI.SetActive(showInventoryUI);
}
}