Simple QuickAccess setting to more easily retrieve common data
This commit is contained in:
@@ -22,3 +22,4 @@ MonoBehaviour:
|
||||
- {fileID: 6399527186463168339, guid: ac41583865245bc4bb3de6c15929b9fc, type: 3}
|
||||
- {fileID: 2755712733105741372, guid: 70e6fca1164d9a140b271f4261f1f023, type: 3}
|
||||
- {fileID: 5034240524438268576, guid: adbb9bfd5489f3f4995966535ca5f24b, type: 3}
|
||||
- {fileID: 2326026072467672024, guid: c8d9eb8c3ca524b4eb67f6364b455b87, type: 3}
|
||||
|
||||
46
Assets/Prefabs/Managers/QuickAccess.prefab
Normal file
46
Assets/Prefabs/Managers/QuickAccess.prefab
Normal file
@@ -0,0 +1,46 @@
|
||||
%YAML 1.1
|
||||
%TAG !u! tag:unity3d.com,2011:
|
||||
--- !u!1 &2326026072467672024
|
||||
GameObject:
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
serializedVersion: 6
|
||||
m_Component:
|
||||
- component: {fileID: 2864744343528369288}
|
||||
- component: {fileID: 8478071710053730667}
|
||||
m_Layer: 0
|
||||
m_Name: QuickAccess
|
||||
m_TagString: Untagged
|
||||
m_Icon: {fileID: 0}
|
||||
m_NavMeshLayer: 0
|
||||
m_StaticEditorFlags: 0
|
||||
m_IsActive: 1
|
||||
--- !u!4 &2864744343528369288
|
||||
Transform:
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
m_GameObject: {fileID: 2326026072467672024}
|
||||
serializedVersion: 2
|
||||
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
|
||||
m_LocalPosition: {x: 2.98359, y: 1.67676, z: 0}
|
||||
m_LocalScale: {x: 1, y: 1, z: 1}
|
||||
m_ConstrainProportionsScale: 0
|
||||
m_Children: []
|
||||
m_Father: {fileID: 0}
|
||||
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
|
||||
--- !u!114 &8478071710053730667
|
||||
MonoBehaviour:
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
m_GameObject: {fileID: 2326026072467672024}
|
||||
m_Enabled: 1
|
||||
m_EditorHideFlags: 0
|
||||
m_Script: {fileID: 11500000, guid: 86c6fcf337334bf0a69a2d6e536c6f0b, type: 3}
|
||||
m_Name:
|
||||
m_EditorClassIdentifier: AppleHillsScripts::AppleHills.Core.QuickAccess
|
||||
7
Assets/Prefabs/Managers/QuickAccess.prefab.meta
Normal file
7
Assets/Prefabs/Managers/QuickAccess.prefab.meta
Normal file
@@ -0,0 +1,7 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c8d9eb8c3ca524b4eb67f6364b455b87
|
||||
PrefabImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
185
Assets/Scripts/Core/QuickAccess.cs
Normal file
185
Assets/Scripts/Core/QuickAccess.cs
Normal 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
|
||||
}
|
||||
}
|
||||
3
Assets/Scripts/Core/QuickAccess.cs.meta
Normal file
3
Assets/Scripts/Core/QuickAccess.cs.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 86c6fcf337334bf0a69a2d6e536c6f0b
|
||||
timeCreated: 1760104860
|
||||
3
Assets/Scripts/Examples.meta
Normal file
3
Assets/Scripts/Examples.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d5112d15beb144088e8b8752879deda3
|
||||
timeCreated: 1760105142
|
||||
85
Assets/Scripts/Examples/QuickAccessExample.cs
Normal file
85
Assets/Scripts/Examples/QuickAccessExample.cs
Normal 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}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
3
Assets/Scripts/Examples/QuickAccessExample.cs.meta
Normal file
3
Assets/Scripts/Examples/QuickAccessExample.cs.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c6086c2645c14cad92be0a9c7c191fdc
|
||||
timeCreated: 1760105142
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,3 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e276089fbc344b3d8cb14729dbe3508a
|
||||
timeCreated: 1760095787
|
||||
Reference in New Issue
Block a user