-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameEntityManager.cs
More file actions
78 lines (69 loc) · 2.3 KB
/
GameEntityManager.cs
File metadata and controls
78 lines (69 loc) · 2.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
using System.Collections.Generic;
using LuaFlow.Core;
using LuaFlow.Interface;
using UnityEngine;
/// <summary>
/// Game Object Caching Manager.
/// This source code is designed based on Unity's Scene Adaptive approach (e.g., Chap1 scene, Player scene, Manager scene, ...).
/// This class is used for the purpose of maintaining object references across scenes and optimizing performance.
/// </summary>
public class GameEntityManager : MonoBehaviour, IGameEntityManager
{
public static GameEntityManager Instance { get; private set; }
private static readonly Dictionary<string, GameObject> _cachedGameObjects = new();
private void Awake()
{
if (Instance == null)
{
Instance = this;
LuaFlowServiceLocator.Register<IGameEntityManager>(this);
DontDestroyOnLoad(gameObject);
}
else
{
Destroy(gameObject);
}
}
private void OnDestroy()
{
Instance = null;
LuaFlowServiceLocator.Unregister<IGameEntityManager>();
}
/// <summary>
/// Register Game Object.
/// </summary>
/// <param name="key">Unique key to identify the object.</param>
/// <param name="value">Game object to register.</param>
public static void RegisterGameObject(string key, GameObject value)
{
_cachedGameObjects[key] = value;
Debug.Log($"Game object registered: {key}");
}
/// <summary>
/// Get Game Object.
/// </summary>
/// <param name="key">Key of the object to find.</param>
public GameObject GetGameObject(string key)
{
if (_cachedGameObjects.TryGetValue(key, out var cached))
return cached;
Debug.LogWarning($"Could not find game object corresponding to key '{key}'");
return null;
}
/// <summary>
/// Unregister a specific game object.
/// </summary>
/// <param name="key">Key of the object to remove.</param>
public static void UnregisterGameObject(string key)
{
if (_cachedGameObjects.Remove(key)) Debug.Log($"Game object removed: {key}");
}
/// <summary>
/// Remove all stored game objects.
/// </summary>
public void RemoveAllEntities()
{
_cachedGameObjects.Clear();
Debug.Log("All game object references have been removed.");
}
}