additional package setup
This commit is contained in:
@@ -0,0 +1,103 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Boxfriend.Extensions
|
||||
{
|
||||
public static class GameObjectExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Recursively changes game object and its children to specified layer
|
||||
/// </summary>
|
||||
/// <param name="layer">Layer to change all objects to</param>
|
||||
public static void SetLayerRecursively(this GameObject obj, int layer)
|
||||
{
|
||||
obj.layer = layer;
|
||||
foreach (Transform child in obj.transform)
|
||||
{
|
||||
child.gameObject.SetLayerRecursively(layer);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the first child of the current GameObject that has the specified tag. Does not include itself.
|
||||
/// </summary>
|
||||
public static GameObject FindChildWithTag(this GameObject obj, string tag)
|
||||
{
|
||||
foreach(Transform child in obj.transform)
|
||||
{
|
||||
if(child.gameObject == obj) continue;
|
||||
|
||||
if(child.CompareTag(tag))
|
||||
return child.gameObject;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns an array containing all children of the current GameObject that have the specified tag. Does not include itself.
|
||||
/// </summary>
|
||||
public static GameObject[] FindChildrenWithTag(this GameObject obj, string tag)
|
||||
{
|
||||
var taggedArray = new GameObject[obj.transform.childCount];
|
||||
var index = 0;
|
||||
foreach(Transform child in obj.transform)
|
||||
{
|
||||
if(child.CompareTag(tag))
|
||||
{
|
||||
taggedArray[index] = child.gameObject;
|
||||
index++;
|
||||
}
|
||||
}
|
||||
|
||||
if(index == 0) return null;
|
||||
|
||||
Array.Resize(ref taggedArray, index);
|
||||
return taggedArray;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a List containing all children of the current GameObject that have the specified tag. Does not include itself.
|
||||
/// </summary>
|
||||
public static List<GameObject> FindChildrenWithTagList(this GameObject obj, string tag)
|
||||
{
|
||||
var taggedList = new List<GameObject>();
|
||||
|
||||
foreach(Transform child in obj.transform)
|
||||
{
|
||||
if(child.gameObject == obj) continue;
|
||||
|
||||
if(child.CompareTag(tag))
|
||||
taggedList.Add(child.gameObject);
|
||||
}
|
||||
return taggedList;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Destroys all children of the GameObject not including itself\
|
||||
/// </summary>
|
||||
public static void DestroyChildren(this GameObject parent)
|
||||
{
|
||||
var children = new Transform[parent.transform.childCount];
|
||||
for (var i = 0; i < parent.transform.childCount; i++)
|
||||
children[i] = parent.transform.GetChild(i);
|
||||
for (var i = 0; i < children.Length; i++)
|
||||
GameObject.Destroy(children[i].gameObject);
|
||||
}
|
||||
|
||||
///<summary>
|
||||
/// Checks if a GameObject is tagged with any of the strings in the provided collection
|
||||
///</summary>
|
||||
public static bool CompareTags(this GameObject go, IEnumerable<string> tags)
|
||||
{
|
||||
foreach (var tag in tags)
|
||||
{
|
||||
if (go.CompareTag(tag))
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 70306add1702cb54c8da10e7385f4afd
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,44 @@
|
||||
using System.Runtime.CompilerServices;
|
||||
using UnityEngine;
|
||||
namespace Boxfriend.Extensions
|
||||
{
|
||||
public static class MathExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Checks if value is within specified range.
|
||||
/// </summary>
|
||||
/// <param name="min">Lowest value of the range</param>
|
||||
/// <param name="max">Largest value of the range</param>
|
||||
/// <returns>True if less than min and greater than max</returns>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static bool InRange(this int value, int min, int max) => (value >= min) && (value <= max);
|
||||
/// <summary>
|
||||
/// Checks if value is within specified range.
|
||||
/// </summary>
|
||||
/// <param name="min">Lowest value of the range</param>
|
||||
/// <param name="max">Largest value of the range</param>
|
||||
/// <returns>True if less than min and greater than max</returns>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static bool InRange(this float value, float min, float max) => (value >= min) && (value <= max);
|
||||
/// <summary>
|
||||
/// Checks if value is within specified range.
|
||||
/// </summary>
|
||||
/// <param name="min">Lowest value of the range</param>
|
||||
/// <param name="max">Largest value of the range</param>
|
||||
/// <returns>True if less than min and greater than max</returns>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static bool InRange(this double value, double min, double max) => (value >= min) && (value <= max);
|
||||
|
||||
public static Vector2 Rotate(this Vector2 vector, float degrees)
|
||||
{
|
||||
float sin = Mathf.Sin(degrees * Mathf.Deg2Rad);
|
||||
float cos = Mathf.Cos(degrees * Mathf.Deg2Rad);
|
||||
|
||||
float tx = vector.x;
|
||||
float ty = vector.y;
|
||||
vector.x = (cos * tx) - (sin * ty);
|
||||
vector.y = (sin * tx) + (cos * ty);
|
||||
return vector;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 750ee6f30f2644ffa9864f245b532846
|
||||
timeCreated: 1640819035
|
||||
@@ -0,0 +1,22 @@
|
||||
using System.Runtime.CompilerServices;
|
||||
using UnityEngine;
|
||||
namespace Boxfriend.Extensions
|
||||
{
|
||||
public static class StringExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Checks if two strings are the same without case sensitivity.
|
||||
/// </summary>
|
||||
/// <param name="value">String being compared</param>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static bool CaseInsensitveEquals (this string str, string value) => (str.ToLower() == value.ToLower());
|
||||
|
||||
/// <summary>
|
||||
/// Applies a rich text color to string
|
||||
/// </summary>
|
||||
/// <param name="text">String to be colored</param>
|
||||
/// <param name="col">Unity Color applied to all of 'text'</param>
|
||||
public static string AddColor(this string text, Color col) => $"<color={ColorHexFromUnityColor(col)}>{text}</color>";
|
||||
public static string ColorHexFromUnityColor(this Color unityColor) => $"#{ColorUtility.ToHtmlStringRGBA(unityColor)}";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ca16c6205a144610a6da0422c8bfa8c0
|
||||
timeCreated: 1640894511
|
||||
@@ -0,0 +1,27 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace Boxfriend.Extensions
|
||||
{
|
||||
public static class TransformExtensions
|
||||
{
|
||||
public static T GetComponentInInactiveParent<T>(this Transform transform) where T : Component
|
||||
{
|
||||
while(transform.parent != null)
|
||||
{
|
||||
transform = transform.parent;
|
||||
if(transform.TryGetComponent(out T comp))
|
||||
return comp;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static void UnparentAll(this Transform transform)
|
||||
{
|
||||
foreach(Transform child in transform)
|
||||
{
|
||||
child.UnparentAll();
|
||||
}
|
||||
transform.SetParent(null);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: f29e0d162e8b8054e86ab1bc8b687c8a
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,11 @@
|
||||
using UnityEngine;
|
||||
namespace Boxfriend.Extensions
|
||||
{
|
||||
public static class Vector2Extensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Changes a Vector2 into a Vector3 where the V2 Y axis is represented on the V3 Z axis
|
||||
/// </summary>
|
||||
public static Vector3 To3D(this Vector2 v2) => new Vector3(v2.x, 0, v2.y);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a6f23bb55cb64524aca4cf21119a963b
|
||||
timeCreated: 1641338003
|
||||
Reference in New Issue
Block a user