Skip to content
This repository has been archived by the owner on Apr 10, 2024. It is now read-only.

Commit

Permalink
Add utility and extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
Anatoly Brizhan committed Feb 27, 2024
1 parent 2612d1a commit c5c065c
Show file tree
Hide file tree
Showing 16 changed files with 205 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
using UnityEngine;

namespace Better.Extensions.Runtime.Runtime.Extensions
namespace Better.Extensions.Runtime
{
public static class BoundsExtensions
{
public static bool Approximately(this Bounds current, Bounds other)
public static bool Approximately(this Bounds self, Bounds other)
{
return BoundsUtility.Approximately(current, other);
return BoundsUtility.Approximately(self, other);
}
}
}
6 changes: 3 additions & 3 deletions Assets/BetterExtensions/Runtime/Extensions/FloatExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ public static class FloatExtensions
/// <summary>
/// Check that point in two other points
/// </summary>
/// <param name="value"></param>
/// <param name="self"></param>
/// <param name="min"></param>
/// <param name="max"></param>
/// <returns></returns>
private static bool InRange(this float value, float min, float max)
private static bool InRange(this float self, float min, float max)
{
return FloatUtility.InRange(value, min, max);
return FloatUtility.InRange(self, min, max);
}
}
}
6 changes: 3 additions & 3 deletions Assets/BetterExtensions/Runtime/Extensions/IntExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ public static class IntExtensions
/// <summary>
/// Check that point in two other points
/// </summary>
/// <param name="value"></param>
/// <param name="self"></param>
/// <param name="min"></param>
/// <param name="max"></param>
/// <returns></returns>
private static bool InRange(this int value, int min, int max)
private static bool InRange(this int self, int min, int max)
{
return IntUtility.InRange(value, min, max);
return IntUtility.InRange(self, min, max);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Better.Extensions.Runtime.TasksExtension
namespace Better.Extensions.Runtime
{
public static class TaskExtensions
{
Expand Down
12 changes: 12 additions & 0 deletions Assets/BetterExtensions/Runtime/Extensions/Vector2Extensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using UnityEngine;

namespace Better.Extensions.Runtime
{
public static class Vector2Extensions
{
public static bool Approximately(this Vector2 self, Vector2 other)
{
return Vector2Utility.Approximately(self, other);
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 35 additions & 0 deletions Assets/BetterExtensions/Runtime/Extensions/Vector3Extensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System.Collections.Generic;
using Unity.Collections;
using UnityEngine;

namespace Better.Extensions.Runtime
{
public static class Vector3Extensions
{
public static bool Approximately(this Vector3 self, Vector3 other)
{
return Vector3Utility.Approximately(self, other);
}

/// <summary>
/// Multiplies Vector3 and Quaternion
/// </summary>
/// <param name="self"></param>
/// <param name="quaternion"></param>
/// <returns></returns>
public static Vector3 Multiply(this Vector3 self, Quaternion quaternion)
{
return Vector3Utility.Multiply(self, quaternion);
}

public static Vector3 Average(this NativeArray<Vector3> self)
{
return Vector3Utility.Average(self);
}

public static Vector3 Average(this IEnumerable<Vector3> self)
{
return Vector3Utility.Average(self);
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

68 changes: 68 additions & 0 deletions Assets/BetterExtensions/Runtime/Utility/AssetDatabaseUtility.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
using System;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using Object = UnityEngine.Object;

namespace Better.Extensions.Runtime
{
public static class AssetDatabaseUtility
{
private const string PrefabFilter = "t:prefab";
private const string ScriptableObjectFilter = "t:ScriptableObject";

public static T[] FindPrefabsOfType<T>()
{
var prefabs = new List<T>();
var gameObjects = FindAssetsOfType<GameObject>(PrefabFilter);

foreach (var gameObject in gameObjects)
{
var components = gameObject.GetComponents<Component>();
foreach (var component in components)
{
if (component is not T item) continue;

prefabs.Add(item);
break;
}
}

return prefabs.ToArray();
}

public static T[] FindScriptableObjectsOfType<T>() where T : ScriptableObject
{
return FindAssetsOfType<T>(ScriptableObjectFilter);
}

public static T[] FindAssetsOfType<T>(string filter) where T : Object
{
if (filter.IsNullOrEmpty() || filter.IsNullOrWhiteSpace())
{
var message = $"{nameof(filter)} cannot be Null or Empty";
DebugUtility.LogException<ArgumentException>(message);
return Array.Empty<T>();
}

var assets = new List<T>();
#if UNITY_EDITOR
var guids = AssetDatabase.FindAssets(filter);

foreach (var guid in guids)
{
var path = AssetDatabase.GUIDToAssetPath(guid);
var asset = AssetDatabase.LoadAssetAtPath<T>(path);

if (asset != null)
{
assets.Add(asset);
}
}

#endif

return assets.ToArray();
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Assets/BetterExtensions/Runtime/Utility/BoundsUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ public static class BoundsUtility
{
public static bool Approximately(Bounds current, Bounds other)
{
return VectorUtility.Approximately(current.center, other.center) &&
VectorUtility.Approximately(current.size, other.size);
return current.center.Approximately(other.center) &&
current.size.Approximately(other.size);
}
}
}
2 changes: 1 addition & 1 deletion Assets/BetterExtensions/Runtime/Utility/EnumUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public static IEnumerable<Enum> GetAllValues(Type enumType)
var values = Enum.GetValues(enumType);
return values.ToEnumerable<Enum>();
}

public static IEnumerable<TEnum> GetAllValues<TEnum>()
where TEnum : Enum
{
Expand Down
47 changes: 47 additions & 0 deletions Assets/BetterExtensions/Runtime/Utility/Vector2Utility.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using UnityEngine;

namespace Better.Extensions.Runtime
{
public struct Vector2Utility
{
public static bool Approximately(Vector2 current, Vector2 other)
{
return Mathf.Approximately(current.x, other.x) &&
Mathf.Approximately(current.y, other.y);
}

public static Vector2 MiddlePoint(Vector2 start, Vector2 end)
{
var t = start + end;
return t / 2;
}

public static Vector2 MiddlePoint(Vector2 start, Vector2 end, Vector2 offset)
{
var middlePoint = MiddlePoint(start, end);
return middlePoint + offset;
}

public static Vector2 AxesInverseLerp(Vector2 a, Vector2 b, Vector2 value)
{
return new Vector2(
Mathf.InverseLerp(a.x, b.x, value.x),
Mathf.InverseLerp(a.y, b.y, value.y)
);
}

public static float InverseLerp(Vector2 a, Vector2 b, Vector2 value)
{
if (a == b)
{
return default;
}

var ab = b - a;
var av = value - a;

var result = Vector2.Dot(av, ab) / Vector2.Dot(ab, ab);
return Mathf.Clamp01(result);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace Better.Extensions.Runtime
{
public struct VectorUtility
public struct Vector3Utility
{
public static bool Approximately(Vector3 current, Vector3 other)
{
Expand All @@ -14,12 +14,6 @@ public static bool Approximately(Vector3 current, Vector3 other)
Mathf.Approximately(current.z, other.z);
}

public static bool Approximately(Vector2 current, Vector2 other)
{
return Mathf.Approximately(current.x, other.x) &&
Mathf.Approximately(current.y, other.y);
}

/// <summary>
/// Take projection of point on Plane
/// </summary>
Expand Down Expand Up @@ -124,16 +118,27 @@ public static Vector3 MiddlePoint(Vector3 start, Vector3 end, Vector3 offset)
return middlePoint + offset;
}

public static Vector2 MiddlePoint(Vector2 start, Vector2 end)
public static float InverseLerp(Vector3 a, Vector3 b, Vector3 value)
{
var t = start + end;
return t / 2;
if (a == b)
{
return default;
}

var ab = b - a;
var av = value - a;

var result = Vector3.Dot(av, ab) / Vector3.Dot(ab, ab);
return Mathf.Clamp01(result);
}

public static Vector2 MiddlePoint(Vector2 start, Vector2 end, Vector2 offset)
public static Vector3 AxesInverseLerp(Vector3 a, Vector3 b, Vector3 value)
{
var middlePoint = MiddlePoint(start, end);
return middlePoint + offset;
return new Vector3(
Mathf.InverseLerp(a.x, b.x, value.x),
Mathf.InverseLerp(a.y, b.y, value.y),
Mathf.InverseLerp(a.z, b.z, value.z)
);
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit c5c065c

Please sign in to comment.