-
Notifications
You must be signed in to change notification settings - Fork 4
/
IgnoreCollision.cs
79 lines (67 loc) · 2.38 KB
/
IgnoreCollision.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class IgnoreCollision : MonoBehaviour {
// Start is called before the first frame update
Dictionary<int, List<Collider2D>> igList = new Dictionary<int, List<Collider2D>> ();
List<Collider2D> colliders = new List<Collider2D> ();
void Start () {
foreach (var coll in GetComponentsInChildren<Collider2D> ()) {
colliders.Add (coll);
}
for (int i = 0; i < colliders.Count; i++) {
for (int k = i + 1; k < colliders.Count; k++) {
Physics2D.IgnoreCollision (colliders[i], colliders[k]);
}
}
}
public void AddIgnoreList (int id, Collider2D[] colliders) {
foreach (var collider in colliders) {
AddToIgnoreList (id, collider);
}
}
public void AddIgnoreList (int id, List<Collider2D> colliders) {
foreach (var collider in colliders) {
AddToIgnoreList (id, collider);
}
}
public void AddToIgnoreList (int id, Collider2D collider) {
if (igList.TryGetValue (id, out var list)) {
list.Add (collider);
IgnoreForigineCollider (collider);
} else {
list = new List<Collider2D> ();
list.Add (collider);
igList.Add (id, list);
IgnoreForigineCollider (collider);
}
}
public void EnableColliders (int id) {
StartCoroutine (EnableDelay (id, 0.5f));
}
private void FinalEnable (int id) {
if (igList.TryGetValue (id, out var list)) {
foreach (var coll in list) {
IgnoreForigineCollider (coll, false);
}
igList.Remove (id);
}
}
public void IgnoreForigineCollider (Collider2D collider, bool ignore = true) {
for (int i = 0; i < colliders.Count; i++) {
Physics2D.IgnoreCollision (colliders[i], collider, ignore);
}
}
public void IgnoreCollider (Collider2D collider, bool ignore = true) {
IgnoreForigineCollider (collider, ignore);
if (colliders.Contains (collider)) {
if (!ignore)
colliders.Remove (collider);
} else if (ignore)
colliders.Add (collider);
}
IEnumerator EnableDelay (int id, float time) {
yield return new WaitForSeconds (time);
FinalEnable (id);
}
}