[Misc] Singleton pattern improvements, get rid of warnings and errors

This commit is contained in:
Michal Pikulski
2025-09-08 08:45:13 +02:00
parent f161d0069b
commit 3b0988243e
7 changed files with 52 additions and 57 deletions

View File

@@ -6,6 +6,8 @@
public class GameManager : MonoBehaviour
{
private static GameManager _instance;
private static bool _isQuitting = false;
/// <summary>
/// Singleton instance of the GameManager.
/// </summary>
@@ -13,14 +15,14 @@ public class GameManager : MonoBehaviour
{
get
{
if (_instance == null)
if (_instance == null && Application.isPlaying && !_isQuitting)
{
_instance = FindAnyObjectByType<GameManager>();
if (_instance == null)
{
var go = new GameObject("GameManager");
_instance = go.AddComponent<GameManager>();
DontDestroyOnLoad(go);
// DontDestroyOnLoad(go);
}
}
return _instance;
@@ -33,7 +35,12 @@ public class GameManager : MonoBehaviour
void Awake()
{
_instance = this;
DontDestroyOnLoad(gameObject);
// DontDestroyOnLoad(gameObject);
}
void OnApplicationQuit()
{
_isQuitting = true;
}
// Accessors for game settings

View File

@@ -62,42 +62,4 @@ public class GameSettings : ScriptableObject
[Header("Item Configuration")]
public System.Collections.Generic.List<CombinationRule> combinationRules;
public System.Collections.Generic.List<SlotItemConfig> slotItemConfigs;
// Singleton pattern
private static GameSettings _instance;
/// <summary>
/// Singleton instance of GameSettings loaded from Resources.
/// </summary>
public static GameSettings Instance {
get {
if (_instance == null) {
_instance = Resources.Load<GameSettings>("GameSettings");
if (_instance == null) {
Debug.LogError("GameSettings asset not found in Resources folder!");
}
}
return _instance;
}
}
// Static property wrappers for easy access
public static float PlayerStopDistance => Instance.playerStopDistance;
public static float FollowerPickupDelay => Instance.followerPickupDelay;
public static float FollowDistance => Instance.followDistance;
public static float ManualMoveSmooth => Instance.manualMoveSmooth;
public static float ThresholdFar => Instance.thresholdFar;
public static float ThresholdNear => Instance.thresholdNear;
public static float StopThreshold => Instance.stopThreshold;
public static float MoveSpeed => Instance.moveSpeed;
public static float StopDistance => Instance.stopDistance;
public static bool UseRigidbody => Instance.useRigidbody;
public static float FollowUpdateInterval => Instance.followUpdateInterval;
public static float FollowerSpeedMultiplier => Instance.followerSpeedMultiplier;
public static float HeldIconDisplayHeight => Instance.heldIconDisplayHeight;
public static GameObject BasePickupPrefab => Instance.basePickupPrefab;
public static float EndlessDescenderLerpSpeed => Instance.endlessDescenderLerpSpeed;
public static float EndlessDescenderMaxOffset => Instance.endlessDescenderMaxOffset;
public static float EndlessDescenderClampXMin => Instance.endlessDescenderClampXMin;
public static float EndlessDescenderClampXMax => Instance.endlessDescenderClampXMax;
public static float EndlessDescenderSpeedExponent => Instance.endlessDescenderSpeedExponent;
}

View File

@@ -10,6 +10,7 @@ using UnityEngine.SceneManagement;
public class SceneManagerService : MonoBehaviour
{
private static SceneManagerService _instance;
private static bool _isQuitting = false;
/// <summary>
/// Singleton instance of the SceneManagerService.
/// </summary>
@@ -17,14 +18,14 @@ public class SceneManagerService : MonoBehaviour
{
get
{
if (_instance == null)
if (_instance == null && Application.isPlaying && !_isQuitting)
{
_instance = FindAnyObjectByType<SceneManagerService>();
if (_instance == null)
{
var go = new GameObject("SceneManagerService");
_instance = go.AddComponent<SceneManagerService>();
DontDestroyOnLoad(go);
// DontDestroyOnLoad(go);
}
}
return _instance;
@@ -45,7 +46,12 @@ public class SceneManagerService : MonoBehaviour
void Awake()
{
_instance = this;
DontDestroyOnLoad(gameObject);
// DontDestroyOnLoad(gameObject);
}
void OnApplicationQuit()
{
_isQuitting = true;
}
/// <summary>

View File

@@ -8,18 +8,20 @@ using UnityEngine.InputSystem;
public class InputManager : MonoBehaviour
{
private static InputManager _instance;
private static bool _isQuitting = false;
public static InputManager Instance
{
get
{
if (_instance == null)
if (_instance == null && Application.isPlaying && !_isQuitting)
{
_instance = FindAnyObjectByType<InputManager>();
if (_instance == null)
{
var go = new GameObject("InputManager");
_instance = go.AddComponent<InputManager>();
DontDestroyOnLoad(go);
// DontDestroyOnLoad(go);
}
}
return _instance;
@@ -36,7 +38,7 @@ public class InputManager : MonoBehaviour
void Awake()
{
_instance = this;
DontDestroyOnLoad(gameObject);
// DontDestroyOnLoad(gameObject);
playerInput = GetComponent<PlayerInput>();
if (playerInput == null)
{
@@ -70,6 +72,11 @@ public class InputManager : MonoBehaviour
}
}
void OnApplicationQuit()
{
_isQuitting = true;
}
/// <summary>
/// Sets the default ITouchInputConsumer to receive input events.
/// </summary>

View File

@@ -16,7 +16,6 @@ namespace Input
private Vector2 lastHoldPosition;
private Coroutine pathfindingDragCoroutine;
private float pathfindingDragUpdateInterval = 0.1f; // Interval in seconds
private bool pendingTap = false; // Track if OnHoldEnd is following a tap (legacy, see below)
// --- Unity/Component References ---
private AIPath aiPath;
@@ -254,8 +253,5 @@ namespace Input
OnArrivedAtTarget?.Invoke();
}
}
// --- Legacy/Unused fields ---
// pendingTap: This field is not used in the current input flow. If you are not using tap/hold distinction logic elsewhere, consider removing it.
// If you want to remove it, please confirm and I will do so.
}
}

View File

@@ -57,9 +57,19 @@ public class Pickup : MonoBehaviour
/// <summary>
/// Draws gizmos for pickup interaction range in the editor.
/// </summary>
/*void OnDrawGizmos()
void OnDrawGizmos()
{
float playerStopDistance = GameManager.Instance.PlayerStopDistance;
float playerStopDistance;
if (Application.isPlaying)
{
playerStopDistance = GameManager.Instance.PlayerStopDistance;
}
else
{
// Load settings directly from asset path in editor
var settings = UnityEditor.AssetDatabase.LoadAssetAtPath<GameSettings>("Assets/Data/Settings/DefaultSettings.asset");
playerStopDistance = settings != null ? settings.playerStopDistance : 1.0f;
}
Gizmos.color = Color.yellow;
Gizmos.DrawWireSphere(transform.position, playerStopDistance);
GameObject playerObj = GameObject.FindGameObjectWithTag("Player");
@@ -69,7 +79,7 @@ public class Pickup : MonoBehaviour
Gizmos.color = Color.cyan;
Gizmos.DrawSphere(stopPoint, 0.15f);
}
}*/
}
#endif
/// <summary>

View File

@@ -7,6 +7,8 @@ using System.Collections.Generic;
public class PuzzleManager : MonoBehaviour
{
private static PuzzleManager _instance;
private static bool _isQuitting = false;
/// <summary>
/// Singleton instance of the PuzzleManager.
/// </summary>
@@ -14,14 +16,14 @@ public class PuzzleManager : MonoBehaviour
{
get
{
if (_instance == null)
if (_instance == null && Application.isPlaying && !_isQuitting)
{
_instance = FindAnyObjectByType<PuzzleManager>();
if (_instance == null)
{
var go = new GameObject("PuzzleManager");
_instance = go.AddComponent<PuzzleManager>();
DontDestroyOnLoad(go);
// DontDestroyOnLoad(go);
}
}
return _instance;
@@ -40,7 +42,7 @@ public class PuzzleManager : MonoBehaviour
void Awake()
{
_instance = this;
DontDestroyOnLoad(gameObject);
// DontDestroyOnLoad(gameObject);
}
/// <summary>
@@ -168,4 +170,9 @@ public class PuzzleManager : MonoBehaviour
// TODO: Fire puzzle complete event or trigger outcome logic
}
}
void OnApplicationQuit()
{
_isQuitting = true;
}
}