Skip to content

Commit ddc328f

Browse files
Merge pull request #1 from Polybroo/main
Collider debugging overhaul -> v1.1.1
2 parents 4a21e7a + 5f1a18a commit ddc328f

17 files changed

+566
-249
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using UnityEngine;
7+
8+
namespace DebugHelper.Basis
9+
{
10+
public abstract class BaseManager : MonoBehaviour
11+
{
12+
public float UpdateRepeatRate
13+
{
14+
get => m_updateRepeatRate;
15+
set
16+
{
17+
m_updateRepeatRate = value;
18+
if (isTicking)
19+
{
20+
StopTicking();
21+
StartTicking(m_updateRepeatRate);
22+
}
23+
}
24+
}
25+
private float m_updateRepeatRate = 1f;
26+
public bool isTicking { get; private set; }
27+
28+
#region Ticking
29+
public virtual void StartTicking(float startDelay = 0f)
30+
{
31+
isTicking = true;
32+
InvokeRepeating(nameof(Tick), startDelay, UpdateRepeatRate);
33+
}
34+
public abstract void Tick();
35+
public virtual void StopTicking()
36+
{
37+
isTicking = false;
38+
CancelInvoke(nameof(Tick));
39+
}
40+
#endregion
41+
}
42+
}
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using System;
2+
using System.Collections;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
8+
namespace DebugHelper.Basis
9+
{
10+
public abstract class BasePool<T>
11+
{
12+
protected HashSet<T> m_list = new HashSet<T>();
13+
14+
public abstract T Register(object target);
15+
public abstract void Unregister(T target);
16+
public virtual bool Contains(T target) => m_list.Contains(target);
17+
public abstract bool Contains(object target);
18+
public abstract int Clear();
19+
public IEnumerable<T> ToSet() => m_list.ToSet();
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -1,244 +1,39 @@
1-
using UnityEngine;
1+
using DebugHelper.Managers;
22
using SMLHelper.V2.Commands;
3-
using System.Collections;
4-
using System.Collections.Generic;
5-
using UWE;
63

74
namespace DebugHelper.Commands
85
{
96
public static class ColliderCommands
107
{
11-
private static Material colliderMaterial; // generic colliders
12-
private static Material physicsColliderMaterial; // colliders on non-kinematic rigidbodies
13-
private static Material triggerMaterial; // colliders with isTrigger set to true
14-
private static Material meshColliderMaterial; // anything with the MeshCollider component
15-
16-
private static List<GameObject> colliderRendererObjects = new List<GameObject>();
17-
18-
private static IEnumerator LoadStasisFieldMaterial()
19-
{
20-
CoroutineTask<GameObject> task = CraftData.GetPrefabForTechTypeAsync(TechType.StasisRifle);
21-
yield return task;
22-
23-
GameObject stasisRifle = task.GetResult();
24-
var stasisBall = stasisRifle.GetComponent<StasisRifle>().effectSpherePrefab.GetComponentInChildren<Renderer>();
25-
colliderMaterial = new Material(stasisBall.materials[1]);
26-
colliderMaterial.color = new Color(0.3f, 1f, 0f);
27-
physicsColliderMaterial = new Material(stasisBall.materials[1]);
28-
physicsColliderMaterial.color = new Color(1f, 0.2f, 0.2f);
29-
triggerMaterial = new Material(stasisBall.materials[1]);
30-
triggerMaterial.color = new Color(0.5f, 0.5f, 0.5f);
31-
meshColliderMaterial = new Material(stasisBall.materials[1]);
32-
meshColliderMaterial.color = new Color(0.2f, 0.2f, 1f);
33-
}
348

9+
/// <summary>
10+
/// Shows all colliders in wanted range
11+
/// </summary>
3512
[ConsoleCommand("showcolliders")]
36-
public static void ShowColliders(bool hitsTriggers = false)
13+
public static void ShowCollidersInRange(float range = 50f, bool hideMessage = false)
3714
{
38-
CoroutineHost.StartCoroutine(ShowCollidersCoroutine(hitsTriggers));
39-
}
40-
41-
[ConsoleCommand("showallcolliders")]
42-
public static void ShowCollidersInRange(float maxRange, bool hideMessages = false)
43-
{
44-
CoroutineHost.StartCoroutine(ShowCollidersInRangeCoroutine(maxRange, hideMessages));
15+
DebugCollidersManager.main.ShowCollidersRange(range);
16+
if (!hideMessage) ErrorMessage.AddMessage($"All colliders in {DebugCollidersManager.main.targetRange}m range are visible now.");
4517
}
18+
/// <summary>
19+
/// Shows all colliders in wanted range (alias)
20+
/// </summary>
21+
[ConsoleCommand("sw_colls")]
22+
public static void ShowCollidersInRange2(float range = 50f, bool hideMessage = false) => ShowCollidersInRange(range, hideMessage);
4623

24+
/// <summary>
25+
/// Hides all shown colliders
26+
/// </summary>
4727
[ConsoleCommand("hidecolliders")]
48-
public static void HideColliders(bool hideMessages = false)
49-
{
50-
int destruido = 0;
51-
foreach (var obj in colliderRendererObjects)
52-
{
53-
if (obj != null)
54-
{
55-
Object.Destroy(obj);
56-
destruido++;
57-
}
58-
}
59-
colliderRendererObjects.Clear();
60-
if (!hideMessages) ErrorMessage.AddMessage($"Destroyed all {destruido} collider renderers.");
61-
}
62-
63-
private static IEnumerator ShowCollidersCoroutine(bool hitsTriggers)
64-
{
65-
if (colliderMaterial == null)
66-
{
67-
yield return CoroutineHost.StartCoroutine(LoadStasisFieldMaterial());
68-
}
69-
Transform scanTransform = SNCameraRoot.main.transform;
70-
if (Physics.Raycast(scanTransform.position + scanTransform.forward, scanTransform.forward, out RaycastHit hit, float.MaxValue, -1, hitsTriggers ? QueryTriggerInteraction.Collide : QueryTriggerInteraction.Ignore))
71-
{
72-
if (hit.collider.GetComponentInParent<Player>())
73-
{
74-
ErrorMessage.AddMessage("Warning: raycast hit player");
75-
yield break;
76-
}
77-
Transform root = hit.collider.transform;
78-
var prefabIdentifier = hit.collider.GetComponentInParent<PrefabIdentifier>();
79-
if (prefabIdentifier != null)
80-
{
81-
root = prefabIdentifier.transform;
82-
}
83-
else
84-
{
85-
var rb = hit.collider.attachedRigidbody;
86-
if (rb != null)
87-
{
88-
root = rb.transform;
89-
}
90-
}
91-
ErrorMessage.AddMessage(string.Format("Showing colliders within object '{0}'.", root.gameObject.name));
92-
RenderCollidersInChildren(root);
93-
}
94-
else
95-
{
96-
ErrorMessage.AddMessage("Raycast failed to hit anything.");
97-
}
98-
}
99-
100-
private static IEnumerator ShowCollidersInRangeCoroutine(float maxRange, bool hideMessages = false)
101-
{
102-
if (colliderMaterial == null)
103-
{
104-
yield return CoroutineHost.StartCoroutine(LoadStasisFieldMaterial());
105-
}
106-
HideColliders(hideMessages);
107-
var center = SNCameraRoot.main.transform.position;
108-
var allColliders = Physics.OverlapSphere(center, maxRange); // all colliders
109-
var colliders = new List<Collider>(); // just colliders we want to use
110-
var playerRb = Player.main.rigidBody;
111-
foreach (var collider in allColliders)
112-
{
113-
if (collider.attachedRigidbody != playerRb) // don't show player colliders
114-
{
115-
colliders.Add(collider);
116-
}
117-
}
118-
if (!hideMessages) ErrorMessage.AddMessage($"Showing all {colliders.Count} colliders within {(int)maxRange} meters.");
119-
foreach (var collider in colliders)
120-
{
121-
RenderCollider(collider);
122-
}
123-
if (!hideMessages) ErrorMessage.AddMessage("Use the 'hidecolliders' command to revert these changes. Running the command again will also reset the colliders.");
124-
}
125-
126-
private static void RenderCollidersInChildren(Transform objectRoot)
127-
{
128-
foreach (var collider in objectRoot.GetComponentsInChildren<Collider>())
129-
{
130-
RenderCollider(collider);
131-
}
132-
}
133-
134-
private static void RenderCollider(Collider col)
135-
{
136-
var colliderType = ColliderType.Collider;
137-
if (col.attachedRigidbody != null && !col.attachedRigidbody.isKinematic) colliderType = ColliderType.PhysicsCollider;
138-
if (col.isTrigger) colliderType = ColliderType.Trigger;
139-
if (col is MeshCollider) colliderType = ColliderType.Mesh;
140-
var parent = col.transform;
141-
if (col is SphereCollider sphere)
142-
{
143-
var sphereTransform = RenderCollider(parent, GameObject.CreatePrimitive(PrimitiveType.Sphere), Vector3.one * sphere.radius * 2f, sphere.center, colliderType);
144-
sphereTransform.transform.parent = null;
145-
sphereTransform.transform.localScale = Vector3.one * sphere.radius * 2f; // spheres are always spherical regardless of parent size!
146-
sphereTransform.transform.parent = parent;
147-
}
148-
if (col is BoxCollider box)
149-
{
150-
RenderCollider(parent, GameObject.CreatePrimitive(PrimitiveType.Cube), box.size, box.center, colliderType);
151-
}
152-
if (col is CapsuleCollider capsule)
153-
{
154-
var capsuleTransform = RenderCollider(parent, GameObject.CreatePrimitive(PrimitiveType.Capsule), new Vector3(capsule.radius * 2f, capsule.height / 2, capsule.radius * 2f), capsule.center, colliderType);
155-
capsuleTransform.localEulerAngles = AnglesFromCapsuleDirection(capsule.direction);
156-
}
157-
if (col is MeshCollider meshCollider)
158-
{
159-
var rendererObj = new GameObject("Mesh Collider Object");
160-
rendererObj.AddComponent<MeshRenderer>();
161-
var mesh = new Mesh();
162-
mesh = meshCollider.sharedMesh;
163-
rendererObj.AddComponent<MeshFilter>().mesh = mesh;
164-
RenderCollider(parent, rendererObj, Vector3.one, Vector3.zero, colliderType);
165-
}
166-
}
167-
168-
private static Transform RenderCollider(Transform parent, GameObject shape, Vector3 localScale, Vector3 center, ColliderType colliderType)
169-
{
170-
Object.DestroyImmediate(shape.GetComponent<Collider>());
171-
shape.transform.parent = parent;
172-
shape.transform.localScale = localScale;
173-
shape.transform.localPosition = center;
174-
shape.transform.localEulerAngles = Vector3.zero;
175-
var r = shape.GetComponent<Renderer>();
176-
r.material = GetMaterialForColliderType(colliderType);
177-
colliderRendererObjects.Add(shape);
178-
return shape.transform;
179-
}
180-
181-
private static Material GetMaterialForColliderType(ColliderType type)
182-
{
183-
switch (type)
184-
{
185-
default: return colliderMaterial; ;
186-
case ColliderType.Trigger: return triggerMaterial;
187-
case ColliderType.PhysicsCollider: return physicsColliderMaterial;
188-
case ColliderType.Mesh: return meshColliderMaterial;
189-
}
190-
191-
}
192-
193-
private static Vector3 AnglesFromCapsuleDirection(int capsuleDirection)
194-
{
195-
switch (capsuleDirection)
196-
{
197-
default: // x
198-
return Vector3.forward * 90;
199-
case 1: // y
200-
return Vector3.zero;
201-
case 2: // z
202-
return Vector3.right * 90f;
203-
}
204-
}
205-
206-
private enum ColliderType
207-
{
208-
Collider,
209-
PhysicsCollider,
210-
Trigger,
211-
Mesh
212-
}
213-
214-
[ConsoleCommand("lookingat")]
215-
public static void LookingAt(bool hitTriggers = false)
28+
public static void HideColliders(bool hideMessage = false)
21629
{
217-
Transform scanTransform = MainCameraControl.main.transform;
218-
if (Physics.Raycast(scanTransform.position + scanTransform.forward, scanTransform.forward, out RaycastHit hit, float.MaxValue, -1, hitTriggers ? QueryTriggerInteraction.Collide : QueryTriggerInteraction.Ignore))
219-
{
220-
var hitGameObject = hit.collider.gameObject;
221-
var parent = hitGameObject.transform.parent;
222-
var attachedRb = hit.collider.attachedRigidbody;
223-
var root = UWE.Utils.GetEntityRoot(hitGameObject);
224-
ErrorMessage.AddMessage($"Raycast hit collider of name '{hitGameObject.name}'");
225-
if (parent != null)
226-
{
227-
ErrorMessage.AddMessage($"Collider is direct child of '{parent.name}'");
228-
}
229-
if (attachedRb != null)
230-
{
231-
ErrorMessage.AddMessage($"Collider is attached to the RigidBody '{attachedRb.gameObject.name}'");
232-
}
233-
if (root != null)
234-
{
235-
ErrorMessage.AddMessage($"Entity root of this collider is '{root.name}'");
236-
}
237-
}
238-
else
239-
{
240-
ErrorMessage.AddMessage("Raycast failed.");
241-
}
30+
DebugCollidersManager.main.HideColliders();
31+
if (!hideMessage) ErrorMessage.AddMessage($"Colliders are no longer visible.");
24232
}
33+
/// <summary>
34+
/// Hides all shown colliders (alias)
35+
/// </summary>
36+
[ConsoleCommand("hd_colls")]
37+
public static void HideColliders2(bool hideMessage = false) => HideColliders(hideMessage);
24338
}
24439
}

DebugHelper/DebugHelper/Commands/GeneralCommands.cs

+30
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,35 @@ public static void DrawStar(float duration, float radius = 1f)
3838
{
3939
Utils.DebugDrawStar(Player.main.transform.position, radius, Color.cyan, duration);
4040
}
41+
42+
[ConsoleCommand("lookingat")]
43+
public static void LookingAt(bool hitTriggers = false)
44+
{
45+
Transform scanTransform = MainCameraControl.main.transform;
46+
if (Physics.Raycast(scanTransform.position + scanTransform.forward, scanTransform.forward, out RaycastHit hit, float.MaxValue, -1, hitTriggers ? QueryTriggerInteraction.Collide : QueryTriggerInteraction.Ignore))
47+
{
48+
var hitGameObject = hit.collider.gameObject;
49+
var parent = hitGameObject.transform.parent;
50+
var attachedRb = hit.collider.attachedRigidbody;
51+
var root = UWE.Utils.GetEntityRoot(hitGameObject);
52+
ErrorMessage.AddMessage($"Raycast hit collider of name '{hitGameObject.name}'");
53+
if (parent != null)
54+
{
55+
ErrorMessage.AddMessage($"Collider is direct child of '{parent.name}'");
56+
}
57+
if (attachedRb != null)
58+
{
59+
ErrorMessage.AddMessage($"Collider is attached to the Rigidbody '{attachedRb.gameObject.name}'");
60+
}
61+
if (root != null)
62+
{
63+
ErrorMessage.AddMessage($"Entity root of this collider is '{root.name}'");
64+
}
65+
}
66+
else
67+
{
68+
ErrorMessage.AddMessage("Raycast failed.");
69+
}
70+
}
4171
}
4272
}

DebugHelper/DebugHelper/Commands/LightCommands.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using UnityEngine;
22
using SMLHelper.V2.Commands;
33
using System.Collections.Generic;
4-
using UWE;
4+
using DebugHelper.Interfaces;
55
using DebugHelper.Systems;
66

77
namespace DebugHelper.Commands

DebugHelper/DebugHelper/Systems/IDebugIcon.cs DebugHelper/DebugHelper/Interfaces/IDebugIcon.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using UnityEngine;
2+
using DebugHelper.Systems;
23

3-
namespace DebugHelper.Systems
4+
namespace DebugHelper.Interfaces
45
{
56
public interface IDebugIcon
67
{

0 commit comments

Comments
 (0)