diff --git a/JotunnLib/Extensions/GameObjectExtension.cs b/JotunnLib/Extensions/GameObjectExtension.cs index ec3729976..f769abd52 100644 --- a/JotunnLib/Extensions/GameObjectExtension.cs +++ b/JotunnLib/Extensions/GameObjectExtension.cs @@ -1,7 +1,9 @@ -using System.Reflection; - using TMPro; - using UnityEngine; +using System.Reflection; +using System; +using TMPro; +using UnityEngine; using UnityEngine.UI; +using Jotunn.Extensions; namespace Jotunn { @@ -26,7 +28,7 @@ public static GameObject OrNull(this GameObject @this) /// Any type that inherits MonoBehaviour /// this /// Returns null when MonoBehaviours.op_equality returns false. - public static T OrNull(this T @this) where T : Object + public static T OrNull(this T @this) where T : UnityEngine.Object { return (T)(@this ? @this : null); } @@ -104,6 +106,118 @@ public static Component AddComponentCopy(this GameObject gameObject, T duplic return target; } + + /// + /// Check if GameObject has any of the specified components. + /// + /// + /// + /// + public static bool HasAnyComponent(this GameObject gameObject, params Type[] components) + { + foreach (var compo in components) + { + if (gameObject.GetComponent(compo)) + { + return true; + } + } + return false; + } + + /// + /// Check if GameObject has any of the specified components. + /// + /// + /// + /// + public static bool HasAnyComponent(this GameObject gameObject, params string[] componentNames) + { + foreach (var name in componentNames) + { + if (gameObject.GetComponent(name)) + { + return true; + } + } + return false; + } + + /// + /// Check if GameObject has all of the specified components. + /// + /// + /// + /// + public static bool HasAllComponents(this GameObject gameObject, params string[] componentNames) + { + foreach (var name in componentNames) + { + if (!gameObject.GetComponent(name)) + { + return false; + } + } + return true; + } + + /// + /// Check if GameObject has all of the specified components. + /// + /// + /// + /// + public static bool HasAllComponents(this GameObject gameObject, params Type[] components) + { + foreach (var compo in components) + { + if (!gameObject.GetComponent(compo)) + { + return false; + } + } + return true; + } + + /// + /// Check if GameObject or any of it's children + /// have any of the specified components. + /// + /// + /// + /// + /// + public static bool HasAnyComponentInChildren( + this GameObject gameObject, + bool includeInactive = false, + params Type[] components + ) + { + foreach (var compo in components) + { + if (gameObject.GetComponentInChildren(compo, includeInactive)) + { + return true; + } + } + return false; + } + + /// + /// Extension method to find nested children by name using either + /// a breadth-first or depth-first search. Default is breadth-first. + /// + /// + /// Name of the child object to search for. + /// Whether to preform a breadth first or depth first search. Default is breadth first. + public static Transform FindDeepChild( + this GameObject gameObject, + string childName, + global::Utils.IterativeSearchType searchType = global::Utils.IterativeSearchType.BreadthFirst + ) + { + return gameObject.transform.FindDeepChild(childName, searchType); + } } /// diff --git a/JotunnLib/Extensions/StringExtensions.cs b/JotunnLib/Extensions/StringExtensions.cs new file mode 100644 index 000000000..44ae1d0d9 --- /dev/null +++ b/JotunnLib/Extensions/StringExtensions.cs @@ -0,0 +1,107 @@ +namespace Jotunn.Extensions +{ + public static class StringExtensions + { + /// + /// Returns true if the string contains any of the substrings. + /// + /// + /// + /// + public static bool ContainsAny(this string str, params string[] substrings) + { + foreach (var substring in substrings) + { + if (str.Contains(substring)) + { + return true; + } + } + return false; + } + + /// + /// Returns true if the string ends with any one of the suffixes. + /// + /// + /// + /// + public static bool EndsWithAny(this string str, params string[] suffixes) + { + foreach (var substring in suffixes) + { + if (str.EndsWith(substring)) + { + return true; + } + } + return false; + } + + /// + /// Returns true if the string starts with any one of the prefixes. + /// + /// + /// + /// + public static bool StartsWithAny(this string str, params string[] prefixes) + { + foreach (var substring in prefixes) + { + if (str.StartsWith(substring)) + { + return true; + } + } + return false; + } + + /// + /// If the string ends with the suffix then return a copy of the string + /// with the suffix stripped, otherwise return the original string. + /// + /// + /// + /// + public static string RemoveSuffix(this string s, string suffix) + { + if (s.EndsWith(suffix)) + { + return s.Substring(0, s.Length - suffix.Length); + } + + return s; + } + + /// + /// If the string starts with the prefix then return a copy of the string + /// with the prefix stripped, otherwise return the original string. + /// + /// + /// + /// + public static string RemovePrefix(this string s, string prefix) + { + if (s.StartsWith(prefix)) + { + return s.Substring(prefix.Length, s.Length - prefix.Length); + } + return s; + } + + /// + /// Returns a copy of the string with the first character capitalized + /// + /// + /// + public static string CapitalizeFirstLetter(this string s) + { + if (s.Length == 0) + return s; + else if (s.Length == 1) + return $"{char.ToUpper(s[0])}"; + else + return char.ToUpper(s[0]) + s.Substring(1); + } + } +} diff --git a/JotunnLib/Extensions/TransformExtensions.cs b/JotunnLib/Extensions/TransformExtensions.cs new file mode 100644 index 000000000..89fc5219a --- /dev/null +++ b/JotunnLib/Extensions/TransformExtensions.cs @@ -0,0 +1,27 @@ +using UnityEngine; + +namespace Jotunn.Extensions +{ + /// + /// Convenience methods for Transforms + /// + public static class TransformExtensions + { + /// + /// Extension method to find nested children by name using either + /// a breadth-first or depth-first search. Default is breadth-first. + /// + /// + /// Name of the child object to search for. + /// Whether to preform a breadth first or depth first search. Default is breadth first. + /// + public static Transform FindDeepChild( + this Transform transform, + string childName, + global::Utils.IterativeSearchType searchType = global::Utils.IterativeSearchType.BreadthFirst + ) + { + return global::Utils.FindChild(transform, childName, searchType); + } + } +}