-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMonoBehaviourExtensions.cs
162 lines (135 loc) · 5.65 KB
/
MonoBehaviourExtensions.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public static class MonoBehaviourExtensions
{
public static Action<bool> OnToggledMouse;
public static Action<bool> OnToggleInput;
public static Coroutine DelayedAction(this MonoBehaviour monoBehaviour, Action before, float delay, Action after)
{
return monoBehaviour.StartCoroutine(DelayedActionCoroutine(before, delay, after));
}
public static Coroutine DelayedByFrameAction(this MonoBehaviour monoBehaviour, Action before, Action after)
{
return monoBehaviour.StartCoroutine(DelayedByFrameActionCoroutine(before, after));
}
private static IEnumerator DelayedActionCoroutine(Action before, float delay, Action after)
{
before?.Invoke();
yield return new WaitForSeconds(delay);
after?.Invoke();
}
private static IEnumerator DelayedByFrameActionCoroutine(Action before, Action after)
{
before?.Invoke();
yield return new WaitForEndOfFrame();
after?.Invoke();
}
public static bool IsPointerOverUI(this MonoBehaviour monoBehaviour)
{
return UnityEngine.EventSystems.EventSystem.current.IsPointerOverGameObject();
}
public static void ToggleMouse(this MonoBehaviour monoBehaviour, bool on)
{
Cursor.visible = on;
Cursor.lockState = on ? CursorLockMode.None : CursorLockMode.Locked;
OnToggledMouse?.Invoke(on);
}
public static void ToggleInput(this MonoBehaviour monoBehaviour, bool on)
{
OnToggleInput?.Invoke(on);
}
public static void SaveData(this MonoBehaviour monoBehaviour)
{
GI_CustomGameInstance gameInstance = UnityEngine.Object.FindAnyObjectByType<GI_CustomGameInstance>();
gameInstance.SaveGame();
}
public static T GetGloballyLoadedSaveData<T>(this MonoBehaviour monoBehaviour) where T : SaveData
{
GI_CustomGameInstance gameInstance = UnityEngine.Object.FindAnyObjectByType<GI_CustomGameInstance>();
return (T)gameInstance.SaveData;
}
public static Vector3 SnapToGrid(this MonoBehaviour monoBehaviour, Vector3 position, float cellSize, Vector3 offset)
{
return new Vector3(
Mathf.Round((position.x - offset.x) / cellSize) * cellSize + offset.x,
Mathf.Round((position.y - offset.y) / cellSize) * cellSize + offset.y,
Mathf.Round((position.z - offset.z) / cellSize) * cellSize + offset.z
);
}
}
public static class TransfromExtensions
{
public static void ClearChildren(this Transform transform, Predicate<Transform> condition, bool sendToObjectPooler = false)
{
foreach (Transform child in transform)
{
if (condition != null && !condition(child)) continue;
if (sendToObjectPooler)
{
ObjectPooler.DespawnObject(child.gameObject);
}
else
{
GameObject.Destroy(child.gameObject);
}
}
}
}
public static class RigidbodyExtensions
{
/// <summary>
/// Returns a set of connected Rigidbody objects to the root object using bounds intersection and distance threshold.
/// </summary>
public static HashSet<Rigidbody> GetConnectedObjectsByBounds(this Rigidbody rootObject, float connectionThreshold, Predicate<Rigidbody> condition = null, int depth = -1)
{
HashSet<Rigidbody> connectedObjects = new HashSet<Rigidbody>();
Queue<Rigidbody> toProcess = new Queue<Rigidbody>();
toProcess.Enqueue(rootObject);
while (toProcess.Count > 0)
{
Rigidbody current = toProcess.Dequeue();
if (connectedObjects.Contains(current)) continue;
if (condition != null && !condition(current)) continue;
connectedObjects.Add(current);
if (depth != -1)
{
if (connectedObjects.Count >= depth)
{
break;
}
}
// Get current object's bounds
Collider currentCollider = current.GetComponent<Collider>();
if (currentCollider == null) continue;
Bounds currentBounds = currentCollider.bounds;
// Expand bounds slightly to account for the connection threshold
Vector3 expandedSize = currentBounds.size + Vector3.one * connectionThreshold;
Vector3 center = currentBounds.center;
// Find nearby colliders using Physics.OverlapBox
Collider[] nearbyColliders = Physics.OverlapBox(center, expandedSize / 2);
foreach (Collider nearbyCollider in nearbyColliders)
{
Rigidbody nearbyRb = nearbyCollider.attachedRigidbody;
if (nearbyRb == null || connectedObjects.Contains(nearbyRb) || nearbyRb == current) continue;
// Check if the bounds overlap or are within the connection threshold
Bounds nearbyBounds = nearbyCollider.bounds;
if (currentBounds.Intersects(nearbyBounds) ||
(currentBounds.SqrDistance(nearbyBounds.min) <= connectionThreshold * connectionThreshold) ||
(currentBounds.SqrDistance(nearbyBounds.max) <= connectionThreshold * connectionThreshold))
{
toProcess.Enqueue(nearbyRb);
}
}
}
return connectedObjects;
}
}
public static class Vector3Extensions
{
public static Vector3 Round(this Vector3 vector, int decimals)
{
return new Vector3((float)Math.Round(vector.x, decimals), (float)Math.Round(vector.y, decimals), (float)Math.Round(vector.z, decimals));
}
}