Tanoda
GCHelper.cs
Go to the documentation of this file.
1using System;
2using System.Collections;
3using UnityEngine;
4
5namespace TriLibCore
6{
10 public class GCHelper : MonoBehaviour
11 {
15 public float Interval = 1f;
16
17 private int _loadingCount;
18
19 private static GCHelper _instance;
20
25 public static GCHelper GetInstance()
26 {
27 if (!Application.isPlaying)
28 {
29 return null;
30 }
31 if (_instance == null)
32 {
33 _instance = new GameObject("TriLibGCHelper").AddComponent<GCHelper>();
34 }
35 return _instance;
36 }
37
41 private void Start()
42 {
43 StartCoroutine(CollectGC());
44 }
45
50 private IEnumerator CollectGC()
51 {
52 if (!Application.isPlaying)
53 {
54 Destroy(_instance.gameObject);
55 yield break;
56 }
57 while (true)
58 {
59 if (_loadingCount > 0)
60 {
61 yield return new WaitForSeconds(Interval);
62 GC.Collect();
63 }
64 yield return null;
65 }
66 }
67
73 private IEnumerator RemoveInstanceInternal(float interval)
74 {
75 yield return new WaitForSeconds(interval);
76 _loadingCount = Mathf.Max(0, _loadingCount-1);
77 }
78
82 public void RegisterLoading()
83 {
84 _loadingCount++;
85 }
86
91 public void UnRegisterLoading(float interval)
92 {
93 StartCoroutine(RemoveInstanceInternal(interval));
94 }
95 }
96}
Represents a class that forces GC collection using a fixed interval.
Definition: GCHelper.cs:11
float Interval
The interval to do the GC Collection.
Definition: GCHelper.cs:15
void UnRegisterLoading(float interval)
Indicates a model has finished loading or an error occurred.
Definition: GCHelper.cs:91
static GCHelper GetInstance()
Gets the GCHelper instance and setup the given internal.
Definition: GCHelper.cs:25
void RegisterLoading()
Indicates a new model is loading.
Definition: GCHelper.cs:82