This repository has been archived by the owner on Dec 20, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 31
/
EgoCleanUp.cs
65 lines (54 loc) · 1.45 KB
/
EgoCleanUp.cs
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
using UnityEngine;
using System;
using System.Collections.Generic;
public class EgoCleanUp
{
static List<Action> _cleanUps = new List<Action>();
static List<GameObject> _destroyedGameObjects = new List<GameObject>();
public static void AddCleanUp( Action cleanup )
{
_cleanUps.Add(cleanup);
}
public static void CleanUp()
{
// Destroy Components
for( var i = 0; i < _cleanUps.Count; i++ )
{
_cleanUps[ i ]();
}
// Destroy GameObjects
for( var i = 0; i < _destroyedGameObjects.Count; i++ )
{
UnityEngine.Object.Destroy( _destroyedGameObjects[ i ] );
}
_destroyedGameObjects.Clear();
}
public static void Destroy( GameObject gameObject )
{
_destroyedGameObjects.Add( gameObject);
}
}
public class EgoCleanUp<C>
where C : Component
{
static List<Tuple<EgoComponent, C>> _tuples = new List<Tuple<EgoComponent, C>>();
static EgoCleanUp()
{
EgoCleanUp.AddCleanUp( CleanUp );
}
public static void Destroy( EgoComponent egoComponent, C component )
{
_tuples.Add( new Tuple<EgoComponent, C>( egoComponent, component ) );
}
static void CleanUp()
{
for( var i = 0; i < _tuples.Count; i++ )
{
var egoComponent = _tuples[ i ].first;
var component = _tuples[ i ].second;
egoComponent.mask[ ComponentIDs.Get( typeof( C ) ) ] = false;
UnityEngine.Object.Destroy( component );
}
_tuples.Clear();
}
}