OctoCore 0.0.1
This commit is contained in:
@@ -0,0 +1,237 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
using UnityEngine.SceneManagement;
|
||||
#if ENABLE_INPUT_SYSTEM
|
||||
using UnityEngine.InputSystem;
|
||||
using UnityEngine.InputSystem.Controls;
|
||||
#endif
|
||||
|
||||
namespace OctoCore.Platform
|
||||
{
|
||||
/// <summary>
|
||||
/// Platform detection and input device management module.
|
||||
/// Detects the current platform and whether a controller is connected.
|
||||
/// Compatible with both old and new Unity Input System.
|
||||
/// </summary>
|
||||
[System.Serializable]
|
||||
public class OctoPlatform : IOctoModule
|
||||
{
|
||||
[SerializeField, OctoReadOnly] private OctoPlatforms _current;
|
||||
[SerializeField, OctoReadOnly] private bool _controller;
|
||||
|
||||
/// <summary>
|
||||
/// The current platform the game is running on.
|
||||
/// </summary>
|
||||
public OctoPlatforms Current => _current;
|
||||
|
||||
/// <summary>
|
||||
/// Whether a controller is currently being used (true) or keyboard/mouse (false).
|
||||
/// Only relevant on Standalone platform.
|
||||
/// </summary>
|
||||
public bool Controller => _controller;
|
||||
|
||||
/// <summary>
|
||||
/// Event invoked when the input device changes between controller and keyboard/mouse.
|
||||
/// </summary>
|
||||
public Action OnInputDeviceChange;
|
||||
|
||||
private bool _initialized;
|
||||
|
||||
public void Init(OctoSettings settings)
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
_current = OctoPlatforms.Standalone;
|
||||
_controller = false;
|
||||
#elif UNITY_GAMECORE_XBOXONE
|
||||
_current = OctoPlatforms.XboxOne;
|
||||
_controller = true;
|
||||
#elif UNITY_GAMECORE_SCARLETT
|
||||
_current = OctoPlatforms.XboxSeries;
|
||||
_controller = true;
|
||||
#elif UNITY_PS4
|
||||
_current = OctoPlatforms.PS4;
|
||||
_controller = true;
|
||||
#elif UNITY_PS5
|
||||
_current = OctoPlatforms.PS5;
|
||||
_controller = true;
|
||||
#elif UNITY_SWITCH
|
||||
_current = OctoPlatforms.Switch;
|
||||
_controller = true;
|
||||
#elif UNITY_ANDROID
|
||||
_current = OctoPlatforms.Android;
|
||||
_controller = false;
|
||||
#elif UNITY_IOS
|
||||
_current = OctoPlatforms.iOS;
|
||||
_controller = false;
|
||||
#else
|
||||
_current = OctoPlatforms.Standalone;
|
||||
_controller = false;
|
||||
#endif
|
||||
|
||||
_initialized = true;
|
||||
|
||||
// Callback OctoEvents
|
||||
|
||||
if (OctoEvents.Instance)
|
||||
OctoEvents.Instance.SetPlatformInitialized();
|
||||
}
|
||||
|
||||
public void Tick()
|
||||
{
|
||||
DetectInputDeviceChange();
|
||||
}
|
||||
|
||||
public void OnSceneChanged(Scene current, Scene next)
|
||||
{
|
||||
}
|
||||
|
||||
public void OnQuit()
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Detects changes between controller and keyboard/mouse input on Standalone platform.
|
||||
/// Works with both old Input Manager and new Input System.
|
||||
/// </summary>
|
||||
private void DetectInputDeviceChange()
|
||||
{
|
||||
if (!_initialized || _current != OctoPlatforms.Standalone)
|
||||
return;
|
||||
|
||||
#if ENABLE_INPUT_SYSTEM
|
||||
DetectInputDeviceChange_NewSystem();
|
||||
#else
|
||||
DetectInputDeviceChange_OldSystem();
|
||||
#endif
|
||||
}
|
||||
|
||||
#if ENABLE_INPUT_SYSTEM
|
||||
/// <summary>
|
||||
/// Input detection using the new Input System.
|
||||
/// </summary>
|
||||
private void DetectInputDeviceChange_NewSystem()
|
||||
{
|
||||
bool usingController = false;
|
||||
bool usingKeyboardOrMouse = false;
|
||||
|
||||
// Check for keyboard input
|
||||
var keyboard = Keyboard.current;
|
||||
if (keyboard != null && keyboard.anyKey.wasPressedThisFrame)
|
||||
{
|
||||
usingKeyboardOrMouse = true;
|
||||
}
|
||||
|
||||
// Check for mouse input
|
||||
var mouse = Mouse.current;
|
||||
if (mouse != null)
|
||||
{
|
||||
Vector2 mouseDelta = mouse.delta.ReadValue();
|
||||
if (mouseDelta.sqrMagnitude > 0.01f)
|
||||
{
|
||||
usingKeyboardOrMouse = true;
|
||||
}
|
||||
}
|
||||
|
||||
// Check for gamepad input
|
||||
var gamepad = Gamepad.current;
|
||||
if (gamepad != null)
|
||||
{
|
||||
// Check sticks
|
||||
if (gamepad.leftStick.ReadValue().sqrMagnitude > 0.01f ||
|
||||
gamepad.rightStick.ReadValue().sqrMagnitude > 0.01f)
|
||||
{
|
||||
usingController = true;
|
||||
}
|
||||
|
||||
// Check dpad
|
||||
if (gamepad.dpad.ReadValue().sqrMagnitude > 0.01f)
|
||||
{
|
||||
usingController = true;
|
||||
}
|
||||
|
||||
// Check triggers
|
||||
if (gamepad.leftTrigger.ReadValue() > 0.1f ||
|
||||
gamepad.rightTrigger.ReadValue() > 0.1f)
|
||||
{
|
||||
usingController = true;
|
||||
}
|
||||
|
||||
// Check buttons
|
||||
foreach (var control in gamepad.allControls)
|
||||
{
|
||||
if (control is ButtonControl button && button.wasPressedThisFrame)
|
||||
{
|
||||
usingController = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool changeToController = !_controller && usingController;
|
||||
bool changeToKeyboardAndMouse = _controller && usingKeyboardOrMouse;
|
||||
|
||||
if (changeToController || changeToKeyboardAndMouse)
|
||||
{
|
||||
_controller = changeToController;
|
||||
OnInputDeviceChange?.Invoke();
|
||||
|
||||
if (OctoCore.Instance && OctoCore.Instance.Development.Log)
|
||||
{
|
||||
Debug.Log($"Platform: {Current}, Input: {(_controller ? "Controller" : "Keyboard/Mouse")}");
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
/// <summary>
|
||||
/// Input detection using the old Input Manager.
|
||||
/// This is the original implementation.
|
||||
/// </summary>
|
||||
private void DetectInputDeviceChange_OldSystem()
|
||||
{
|
||||
#if !ENABLE_INPUT_SYSTEM
|
||||
bool usingController = Controller;
|
||||
bool usingKeyboardOrMouse = !Controller;
|
||||
|
||||
if (!usingController)
|
||||
{
|
||||
usingKeyboardOrMouse = Input.anyKeyDown || (Mathf.Abs(Input.GetAxis("Mouse X") + Input.GetAxis("Mouse Y")) > 0.1f);
|
||||
}
|
||||
else
|
||||
{
|
||||
usingController |= Mathf.Abs(Input.GetAxis("Joystick 1 X") + Input.GetAxis("Joystick 1 Y")) > 0.1f;
|
||||
usingController |= Mathf.Abs(Input.GetAxis("Joystick 2 X") + Input.GetAxis("Joystick 2 Y")) > 0.1f;
|
||||
usingController |= Input.GetAxis("Left Right Arrow") > 0.1f;
|
||||
usingController |= Input.GetAxis("Left Right Arrow") < -0.1f;
|
||||
usingController |= Input.GetAxis("Up Down Arrow") < -0.1f;
|
||||
usingController |= Input.GetAxis("Up Down Arrow") > 0.1f;
|
||||
usingController |= Input.GetAxis("Trigger L") > 0.2f;
|
||||
usingController |= Input.GetAxis("Trigger R") > 0.2f;
|
||||
|
||||
for (int i = 0; i <= 19; i++)
|
||||
{
|
||||
if (Input.GetKeyDown((KeyCode)((int)KeyCode.JoystickButton0 + i)))
|
||||
{
|
||||
usingController = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool changeToController = !_controller && usingController;
|
||||
bool changeToKeyboardAndMouse = _controller && usingKeyboardOrMouse;
|
||||
|
||||
if (changeToController || changeToKeyboardAndMouse)
|
||||
{
|
||||
_controller = changeToController;
|
||||
OnInputDeviceChange?.Invoke();
|
||||
|
||||
if (OctoCore.Instance && OctoCore.Instance.Development.Log)
|
||||
{
|
||||
Debug.Log($"Platform: {Current}");
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 5bdb4059a4feb6249b5fae7b226996bd
|
||||
@@ -0,0 +1,17 @@
|
||||
namespace OctoCore.Platform
|
||||
{
|
||||
/// <summary>
|
||||
/// Supported platforms for the OctoCore framework.
|
||||
/// </summary>
|
||||
public enum OctoPlatforms
|
||||
{
|
||||
Standalone,
|
||||
XboxOne,
|
||||
XboxSeries,
|
||||
PS4,
|
||||
PS5,
|
||||
Switch,
|
||||
Android,
|
||||
iOS
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 5ed0a37e089e9564283e401f5d46c4b4
|
||||
Reference in New Issue
Block a user