Simple QuickAccess setting to more easily retrieve common data

This commit is contained in:
Michal Pikulski
2025-10-10 16:19:05 +02:00
parent 7288228b99
commit 5b3f77c67b
10 changed files with 333 additions and 35 deletions

View File

@@ -0,0 +1,185 @@
using UnityEngine;
using Interactions;
using System.Collections.Generic;
using AppleHills.Core.Settings;
using AppleHills.Data.CardSystem;
using CinematicsM;
using Core;
using Input;
using Minigames.DivingForPictures;
using PuzzleS;
namespace AppleHills.Core
{
/// <summary>
/// Provides quick access to frequently used game objects, components, and manager instances.
/// References are cached for performance and automatically invalidated on scene changes.
/// </summary>
public class QuickAccess : MonoBehaviour
{
#region Singleton Setup
private static QuickAccess _instance;
private static bool _isQuitting = false;
public static QuickAccess Instance
{
get
{
if (_instance == null && Application.isPlaying && !_isQuitting)
{
_instance = FindAnyObjectByType<QuickAccess>();
if (_instance == null)
{
var go = new GameObject("QuickAccess");
_instance = go.AddComponent<QuickAccess>();
}
}
return _instance;
}
}
void OnApplicationQuit()
{
_isQuitting = true;
}
#endregion Singleton Setup
#region Manager Instances
// Core Managers
public GameManager GameManager => GameManager.Instance;
public ItemManager ItemManager => ItemManager.Instance;
public SceneManagerService SceneManager => SceneManagerService.Instance;
// Other Managers
public InputManager InputManager => InputManager.Instance;
public PuzzleManager PuzzleManager => PuzzleManager.Instance;
public CinematicsManager CinematicsManager => CinematicsManager.Instance;
public CardSystemManager CardSystemManager => CardSystemManager.Instance;
#endregion
#region Player and Follower References
private GameObject _playerGameObject;
private GameObject _followerGameObject;
private PlayerTouchController _playerController;
private FollowerController _followerController;
private Camera _mainCamera;
private bool _initialized = false;
/// <summary>
/// Returns the player GameObject. Finds it if not already cached.
/// </summary>
public GameObject PlayerGameObject
{
get
{
if (_playerGameObject == null)
{
_playerGameObject = GameObject.FindGameObjectWithTag("Player");
}
return _playerGameObject;
}
}
/// <summary>
/// Returns the follower (Pulver) GameObject. Finds it if not already cached.
/// </summary>
public GameObject FollowerGameObject
{
get
{
if (_followerGameObject == null)
{
_followerGameObject = GameObject.FindGameObjectWithTag("Pulver");
}
return _followerGameObject;
}
}
/// <summary>
/// Returns the player controller component. Finds it if not already cached.
/// </summary>
public PlayerTouchController PlayerController
{
get
{
if (_playerController == null && PlayerGameObject != null)
{
_playerController = PlayerGameObject.GetComponent<PlayerTouchController>();
}
return _playerController;
}
}
/// <summary>
/// Returns the follower controller component. Finds it if not already cached.
/// </summary>
public FollowerController FollowerController
{
get
{
if (_followerController == null && FollowerGameObject != null)
{
_followerController = FollowerGameObject.GetComponent<FollowerController>();
}
return _followerController;
}
}
/// <summary>
/// Returns the main camera. Caches for performance.
/// </summary>
public Camera MainCamera
{
get
{
if (_mainCamera == null)
{
_mainCamera = Camera.main;
}
return _mainCamera;
}
}
#endregion
#region Initialization and Scene Management
private void Awake()
{
if (!_initialized)
{
// Subscribe to scene changes
if (SceneManager != null)
{
SceneManager.SceneLoadCompleted += OnSceneLoadCompleted;
}
_initialized = true;
}
}
/// <summary>
/// Handle scene changes by clearing cached references.
/// </summary>
private void OnSceneLoadCompleted(string sceneName)
{
ClearReferences();
}
/// <summary>
/// Clear all cached references.
/// </summary>
public void ClearReferences()
{
_playerGameObject = null;
_followerGameObject = null;
_playerController = null;
_followerController = null;
_mainCamera = null;
}
#endregion
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 86c6fcf337334bf0a69a2d6e536c6f0b
timeCreated: 1760104860

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: d5112d15beb144088e8b8752879deda3
timeCreated: 1760105142

View File

@@ -0,0 +1,85 @@
using UnityEngine;
using AppleHills.Core;
namespace AppleHills.Examples
{
/// <summary>
/// Example script demonstrating how to use QuickAccess to quickly retrieve and use game objects
/// </summary>
public class QuickAccessExample : MonoBehaviour
{
void Start()
{
// Retrieve player and follower objects using QuickAccess
GameObject player = QuickAccess.Instance.PlayerGameObject;
GameObject follower = QuickAccess.Instance.FollowerGameObject;
// Print info about the player
if (player != null)
{
Debug.Log($"[QuickAccessExample] Player found: {player.name}");
Debug.Log($"[QuickAccessExample] Player position: {player.transform.position}");
// Access player controller
var playerController = QuickAccess.Instance.PlayerController;
if (playerController != null)
{
Debug.Log($"[QuickAccessExample] Player controller found on object");
}
else
{
Debug.LogWarning($"[QuickAccessExample] Player controller not found");
}
}
else
{
Debug.LogWarning($"[QuickAccessExample] Player not found in scene");
}
// Print info about the follower (Pulver)
if (follower != null)
{
Debug.Log($"[QuickAccessExample] Follower found: {follower.name}");
Debug.Log($"[QuickAccessExample] Follower position: {follower.transform.position}");
// Access follower controller
var followerController = QuickAccess.Instance.FollowerController;
if (followerController != null)
{
Debug.Log($"[QuickAccessExample] Follower controller found on object");
}
else
{
Debug.LogWarning($"[QuickAccessExample] Follower controller not found");
}
}
else
{
Debug.LogWarning($"[QuickAccessExample] Follower not found in scene");
}
// Access camera
var camera = QuickAccess.Instance.MainCamera;
if (camera != null)
{
Debug.Log($"[QuickAccessExample] Main camera found: {camera.name}");
Debug.Log($"[QuickAccessExample] Camera position: {camera.transform.position}");
}
else
{
Debug.LogWarning($"[QuickAccessExample] Main camera not found");
}
// Access managers
try
{
Debug.Log($"[QuickAccessExample] Game Manager instance accessed: {QuickAccess.Instance.GameManager != null}");
Debug.Log($"[QuickAccessExample] Input Manager instance accessed: {QuickAccess.Instance.InputManager != null}");
}
catch (System.Exception e)
{
Debug.LogError($"[QuickAccessExample] Error accessing managers: {e.Message}");
}
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: c6086c2645c14cad92be0a9c7c191fdc
timeCreated: 1760105142

View File

@@ -1,32 +0,0 @@
using UnityEngine;
namespace Utils
{
public class QuickAccess : MonoBehaviour
{
private static QuickAccess _instance;
private static bool _isQuitting = false;
public static QuickAccess Instance
{
get
{
if (_instance == null && Application.isPlaying && !_isQuitting)
{
_instance = FindAnyObjectByType<QuickAccess>();
if (_instance == null)
{
var go = new GameObject("QuickAccess");
_instance = go.AddComponent<QuickAccess>();
}
}
return _instance;
}
}
void OnApplicationQuit()
{
_isQuitting = true;
}
}
}

View File

@@ -1,3 +0,0 @@
fileFormatVersion: 2
guid: e276089fbc344b3d8cb14729dbe3508a
timeCreated: 1760095787