Skip to content

Instantly share code, notes, and snippets.

@Ludomancer
Last active April 20, 2024 06:42
Show Gist options
  • Save Ludomancer/14c2c421a8ef6c83c87d to your computer and use it in GitHub Desktop.
Save Ludomancer/14c2c421a8ef6c83c87d to your computer and use it in GitHub Desktop.
Unity Extensions
using UnityEngine;
public static class ColliderExtensions {
public static bool IsVisibleFrom(this Collider collider, Camera camera)
{
Plane[] planes = GeometryUtility.CalculateFrustumPlanes(camera);
return GeometryUtility.TestPlanesAABB(planes, collider.bounds);
}
public static bool IsVisibleFrom(this Collider collider, Camera camera, float safeZone)
{
Bounds _comfortZone = new Bounds(collider.bounds.center, collider.bounds.size * safeZone);
Plane[] planes = GeometryUtility.CalculateFrustumPlanes(camera);
return GeometryUtility.TestPlanesAABB(planes, _comfortZone);
}
}
using UnityEngine;
using System.Collections.Generic;
internal static class MeshExtensions {
public static void SetVertexColor(this Mesh mesh, Color c)
{
Vector3[] vertices = mesh.vertices;
Color[] colors = new Color[vertices.Length];
for (int i = 0; i < vertices.Length; i++)
{
colors[i] = c;
}
mesh.colors = colors;
}
}
using System;
static class MiscExtensions
{
public static void Shuffle<T>(this Random rng, ref T[] array)
{
int n = array.Length;
while (n > 1)
{
int k = rng.Next(n--);
T temp = array[n];
array[n] = array[k];
array[k] = temp;
}
}
}
using UnityEngine;
public static class RendererExtensions
{
public static bool IsVisibleFrom(this Renderer renderer, Camera camera)
{
Plane[] planes = GeometryUtility.CalculateFrustumPlanes(camera);
return GeometryUtility.TestPlanesAABB(planes, renderer.bounds);
}
public static bool IsVisibleFrom(this Renderer renderer, Camera camera,float safeZone)
{
Bounds _comfortZone = new Bounds(renderer.bounds.center, renderer.bounds.size * safeZone);
Plane[] planes = GeometryUtility.CalculateFrustumPlanes(camera);
return GeometryUtility.TestPlanesAABB(planes, _comfortZone);
}
}
using UnityEngine;
using System.Collections;
public static class ValueExtensions
{
public static bool AlmostEquals(this float float1, float float2)
{
return (Mathf.Abs(float1 - float2) <= float.Epsilon);
}
public static bool IsSimiliar(this float float1, float float2, float precision)
{
return (Mathf.Abs(float1 - float2) <= precision);
}
public static bool IsDifferanceLessThan(this float float1, float float2, float precision)
{
return (Mathf.Abs(float1 - float2) < precision);
}
public static float RoundToMultiple(this float float1, float multiple)
{
if (float1 >= 0)
return Mathf.Floor((float1 + multiple / 2) / multiple) * multiple;
else
return Mathf.Ceil((float1 - multiple / 2) / multiple) * multiple;
}
}
using UnityEngine;
public static class ValueExtensions
{
public static bool AlmostEquals(this float float1, float float2)
{
return (Mathf.Abs(float1 - float2) <= float.Epsilon);
}
public static bool IsSimiliar(this float float1, float float2, float precision)
{
return (Mathf.Abs(float1 - float2) <= precision);
}
public static bool IsDifferanceLessThan(this float float1, float float2, float precision)
{
return (Mathf.Abs(float1 - float2) < precision);
}
public static float RoundToMultiple(this float float1, float multiple)
{
if (float1 >= 0) return Mathf.Floor((float1 + multiple / 2) / multiple) * multiple;
return Mathf.Ceil((float1 - multiple / 2) / multiple) * multiple;
}
public static int RoundToMultiple(this int int1, float multiple)
{
return (int)RoundToMultiple((float)int1, multiple);
}
public static float FloorToMultiple(this float float1, float multiple)
{
return Mathf.Floor((float1) / multiple) * multiple;
}
public static int FloorToMultiple(this int int1, float multiple)
{
return (int)FloorToMultiple((float)int1, multiple);
}
public static float CeilToMultiple(this float float1, float multiple)
{
return Mathf.Ceil((float1) / multiple) * multiple;
}
public static int CeilToMultiple(this int int1, float multiple)
{
return (int)CeilToMultiple((float)int1, multiple);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment