-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameObjectCache.cs
More file actions
43 lines (37 loc) · 1.14 KB
/
GameObjectCache.cs
File metadata and controls
43 lines (37 loc) · 1.14 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
using System;
using System.Collections.Generic;
using UnityEngine;
namespace DebugMod
{
class GameObjectCache<T> where T : UnityEngine.Object
{
Dictionary<int, GameObject> objectCache;
public GameObjectCache(int capacity)
{
objectCache = new Dictionary<int, GameObject>(capacity);
}
public GameObjectCache() : this(5) { }
public void ClearCache() => objectCache.Clear();
public void UpdateCache(Func<T, GameObject> createFunc, bool state)
{
foreach(var t in UnityEngine.Object.FindObjectsOfType<T>())
{
int instId = t.GetInstanceID();
if(!objectCache.ContainsKey(instId))
{
var obj = createFunc(t);
if (!obj) continue;
objectCache.Add(instId, obj);
}
objectCache[instId].SetActive(state);
}
}
public void ToggleAllObjects(bool state)
{
foreach(var pair in objectCache)
{
pair.Value.SetActive(state);
}
}
}
}