Utils.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. using System;
  2. using UnityEngine;
  3. namespace Leap.Unity
  4. {
  5. public static class Utils
  6. {
  7. public static void IgnoreCollisions(GameObject first, GameObject second, bool ignore = true)
  8. {
  9. if (first == null || second == null)
  10. {
  11. return;
  12. }
  13. Collider[] componentsInChildren = first.GetComponentsInChildren<Collider>();
  14. Collider[] componentsInChildren2 = second.GetComponentsInChildren<Collider>();
  15. for (int i = 0; i < componentsInChildren.Length; i++)
  16. {
  17. for (int j = 0; j < componentsInChildren2.Length; j++)
  18. {
  19. if (componentsInChildren[i] != componentsInChildren2[j] && componentsInChildren[i].enabled && componentsInChildren2[j].enabled)
  20. {
  21. Physics.IgnoreCollision(componentsInChildren[i], componentsInChildren2[j], ignore);
  22. }
  23. }
  24. }
  25. }
  26. public static void DrawCircle(Vector3 center, Vector3 normal, float radius, Color color, int quality = 32, float duration = 0f, bool depthTest = true)
  27. {
  28. Vector3 forward = Vector3.Slerp(normal, -normal, 0.5f);
  29. Utils.DrawArc(360f, center, forward, normal, radius, color, quality, duration, depthTest);
  30. }
  31. public static void DrawArc(float arc, Vector3 center, Vector3 forward, Vector3 normal, float radius, Color color, int quality = 32, float duration = 0f, bool depthTest = true)
  32. {
  33. Vector3 normalized = Vector3.Cross(normal, forward).normalized;
  34. float num = arc / (float)quality;
  35. Vector3 start = center + forward * radius;
  36. Vector3 vector = default(Vector3);
  37. float num2 = 0f;
  38. while (Mathf.Abs(num2) <= Mathf.Abs(arc))
  39. {
  40. float num3 = Mathf.Cos(num2 * 0.017453292f);
  41. float num4 = Mathf.Sin(num2 * 0.017453292f);
  42. vector.x = center.x + radius * (num3 * forward.x + num4 * normalized.x);
  43. vector.y = center.y + radius * (num3 * forward.y + num4 * normalized.y);
  44. vector.z = center.z + radius * (num3 * forward.z + num4 * normalized.z);
  45. Debug.DrawLine(start, vector, color, duration, depthTest);
  46. start = vector;
  47. num2 += num;
  48. }
  49. }
  50. public static void DrawCone(Vector3 origin, Vector3 direction, float angle, float height, Color color, int quality = 4, float duration = 0f, bool depthTest = true)
  51. {
  52. float num = height / (float)quality;
  53. for (float num2 = num; num2 <= height; num2 += num)
  54. {
  55. Utils.DrawCircle(origin + direction * num2, direction, Mathf.Tan(angle * 0.017453292f) * num2, color, quality * 8, duration, depthTest);
  56. }
  57. }
  58. }
  59. }