1 using System.Collections.Generic;
9 namespace droid.Runtime.Utilities.Misc {
12 public static class NeodroidUtilities {
16 public static Vector4 Zero {
get {
return new Vector4(0, 0, 0, 0); } }
22 public static float KineticEnergy(Rigidbody rb) {
26 * Mathf.Pow(rb.velocity.magnitude,
36 public static Vector3 Vector3Clamp(ref Vector3 vec, Vector3 min_point, Vector3 max_point) {
37 vec.x = Mathf.Clamp(vec.x, min_point.x, max_point.x);
38 vec.y = Mathf.Clamp(vec.y, min_point.y, max_point.y);
39 vec.z = Mathf.Clamp(vec.z, min_point.z, max_point.z);
48 public static void DrawLine(Vector3 p1, Vector3 p2,
float width) {
49 var count = Mathf.CeilToInt(width);
51 Gizmos.DrawLine(p1, p2);
53 var c = Camera.current;
55 Debug.LogError(
"Camera.current is null");
59 var v1 = (p2 - p1).normalized;
60 var v2 = (c.transform.position - p1).normalized;
61 var n = Vector3.Cross(v1, v2);
62 for (var i = 0; i < count; i++) {
64 var o = n * width * ((float)i / (count - 1) - 0.5f);
65 Gizmos.DrawLine(p1 + o, p2 + o);
74 public static AnimationCurve DefaultAnimationCurve() {
75 return new AnimationCurve(
new Keyframe(1, 1),
new Keyframe(0, 0));
81 public static Gradient DefaultGradient() {
82 var gradient =
new Gradient {
86 new GradientColorKey(
new Color(1, 1, 1), 0),
87 new GradientColorKey(
new Color(1, 1, 1), 1f),
88 new GradientColorKey(
new Color(1, 1, 1), 0)
92 new GradientAlphaKey(1, 0),
93 new GradientAlphaKey(1, 1),
94 new GradientAlphaKey(1, 0)
106 public static Texture2D RenderTextureImage(Camera camera) {
108 var current_render_texture = RenderTexture.active;
109 RenderTexture.active = camera.targetTexture;
111 var target_texture = camera.targetTexture;
112 var texture =
new Texture2D(target_texture.width, target_texture.height);
113 texture.ReadPixels(
new Rect(0, 0, target_texture.width, target_texture.height), 0, 0);
115 RenderTexture.active = current_render_texture;
135 RegisterCollisionTriggerCallbacksOnChildren<TChildColliderSensor, TCollider, TCollision>(
152 var children_with_colliders = parent.GetComponentsInChildren<TCollider>();
156 foreach (var child
in children_with_colliders) {
157 var child_sensors = child.GetComponents<TChildColliderSensor>();
159 foreach (var child_sensor
in child_sensors) {
160 if (child_sensor.Caller != null && child_sensor.Caller == caller) {
161 collider_sensor = child_sensor;
165 if (child_sensor.Caller == null) {
166 child_sensor.
Caller = caller;
167 collider_sensor = child_sensor;
172 if (collider_sensor == null) {
173 collider_sensor = child.gameObject.AddComponent<TChildColliderSensor>();
174 collider_sensor.Caller = caller;
177 if (on_collision_enter_child != null) {
178 collider_sensor.OnCollisionEnterDelegate = on_collision_enter_child;
181 if (on_trigger_enter_child != null) {
182 collider_sensor.OnTriggerEnterDelegate = on_trigger_enter_child;
185 if (on_collision_exit_child != null) {
186 collider_sensor.OnCollisionExitDelegate = on_collision_exit_child;
189 if (on_trigger_exit_child != null) {
190 collider_sensor.OnTriggerExitDelegate = on_trigger_exit_child;
193 if (on_trigger_stay_child != null) {
194 collider_sensor.OnTriggerStayDelegate = on_trigger_stay_child;
197 if (on_collision_stay_child != null) {
198 collider_sensor.OnCollisionStayDelegate = on_collision_stay_child;
203 Debug.Log($
"{caller.name} has created {collider_sensor.name} on {child.name} under parent {parent.name}");
213 public static string ColorArrayToString(IEnumerable<Color> colors) {
215 foreach (var color
in colors) {
216 s += color.ToString();
225 bool only_parents =
false,
236 if (c.GetComponentInParent<
Actor>() != null) {
237 component = c.GetComponentInParent<
Actor>();
238 }
else if (!only_parents) {
243 if (component == null){
246 }
else if (!only_parents) {
252 if (component != null) {
257 Debug.Log($
"Could not find a IHasRegister<IActuator> recipient during registration");
279 bool only_parents =
false,
290 if (c.GetComponentInParent<
Actor>() != null) {
291 component = c.GetComponentInParent<
Actor>();
292 }
else if (!only_parents) {
297 if (component == null){
300 }
else if (!only_parents) {
306 if (component != null) {
311 Debug.Log($
"Could not find a IHasRegister<IActuator> recipient during registration");
330 public static TRecipient RegisterComponent<TRecipient, TCaller>(
333 bool only_parents =
false,
338 TRecipient component = null;
341 }
else if (c.GetComponentInParent<TRecipient>() != null) {
342 component = c.GetComponentInParent<TRecipient>();
343 }
else if (!only_parents) {
344 component = Object.FindObjectOfType<TRecipient>();
347 if (component != null) {
352 Debug.Log($
"Could not find a {typeof(TRecipient)} recipient during registration");
370 public static TRecipient RegisterComponent<TRecipient, TCaller>(
374 bool only_parents =
false,
377 TRecipient component = null;
380 }
else if (c.GetComponentInParent<TRecipient>() != null) {
381 component = c.GetComponentInParent<TRecipient>();
382 }
else if (!only_parents) {
383 component = Object.FindObjectOfType<TRecipient>();
386 if (component != null) {
387 component.Register(c, identifier);
391 Debug.Log($
"Could not find a {typeof(TRecipient)} recipient during registeration");
402 public static T[] FindAllObjectsOfTypeInScene<T>() {
404 var results =
new List<T>();
405 for (var i = 0; i < SceneManager.sceneCount; i++) {
406 var s = SceneManager.GetSceneAt(i);
411 var all_game_objects = s.GetRootGameObjects();
412 foreach (var go
in all_game_objects) {
413 results.AddRange(go.GetComponentsInChildren<T>(
true));
417 return results.ToArray();
424 public static GameObject[] FindAllGameObjectsExceptLayer(
int layer) {
425 var goa = Object.FindObjectsOfType<GameObject>();
426 var game_objects =
new List<GameObject>();
427 foreach (var go
in goa) {
428 if (go.layer != layer) {
429 game_objects.Add(go);
433 return game_objects.ToArray();
441 public static Vector3 BroadcastVector3(
this float a) {
return new Vector3 {x = a, y = a, z = a}; }
448 public static Vector2 BroadcastVector2(
this float a) {
return new Vector2 {x = a, y = a}; }
455 public static Vector4 BroadcastVector4(
this float a) {
return new Vector4 {x = a, y = a, z = a}; }
462 public static GameObject[] RecursiveChildGameObjectsExceptLayer(Transform parent,
int layer) {
463 var game_objects =
new List<GameObject>();
464 foreach (Transform go
in parent) {
466 if (go.gameObject.layer != layer) {
467 game_objects.Add(go.gameObject);
468 var children = RecursiveChildGameObjectsExceptLayer(go, layer);
469 if (children != null && children.Length > 0) {
470 game_objects.AddRange(children);
476 return game_objects.ToArray();
delegate void OnChildCollisionEnterDelegate(GameObject child_sensor_game_object, TCollision collision)
delegate void OnChildTriggerExitDelegate(GameObject child_sensor_game_object, TCollider collider)
delegate void OnChildTriggerEnterDelegate(GameObject child_sensor_game_object, TCollider collider)
delegate void OnChildCollisionStayDelegate(GameObject child_sensor_game_object, TCollision collision)
Environment to be used with the prototyping components.
delegate void OnChildTriggerStayDelegate(GameObject child_sensor_game_object, TCollider collider)
delegate void OnChildCollisionExitDelegate(GameObject child_sensor_game_object, TCollision collision)