Skip to content

YoavTC/helper-functions

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

HelperFunctions Class Documentation

The HelperFunctions class provides a collection of utility methods for various common tasks in Unity, including UI interaction, timing, and children management. The class also includes helper methods for color conversion and working with Quaternions.

Table of Contents

  1. Unity Runtime Related
  2. Children Related
  3. Math & Time Related
  4. UI Related
  5. Singleton Class

Unity Runtime Related

IsOverUI

public static bool IsOverUI()

Checks if the mouse is currently over a UI element.

GetWait

public static WaitForSeconds GetWait(float time)

Returns a cached WaitForSeconds object for the given time if available; otherwise, creates and returns a new one.

  • time: The duration of the wait.

GetWaitRealTime

public static WaitForSecondsRealtime GetWaitRealTime(float time)

Returns a cached WaitForSecondsRealtime object for the given time if available; otherwise, creates and returns a new one.

  • time: The duration of the wait.

GetRandomObject

public static Transform GetRandomObject(List<Transform> list, Transform avoidedObject = null)

Returns a random object from a list of Transforms, avoiding a specified object if provided.

  • list: List of Transform objects to choose from.
  • avoidedObject: (Optional) A Transform object to avoid.

Children Related

DestroyChildren

public static void DestroyChildren(Transform parent)

Destroys all children of the specified parent Transform.

public static void DestroyChildren(Transform parent, string tag, bool destroyWithoutTag = false)

Destroys children under a given Transform parent based on a tag. If destroyWithoutTag is true, all children without the specified tag are destroyed.

  • parent: The parent Transform.
  • tag: The tag to filter children by.
  • destroyWithoutTag: Whether to destroy children without the specified tag.

GetFirstChildWithTag

public static Transform GetFirstChildWithTag(Transform parent, string tag)

Returns the first child of the parent Transform with the specified tag.

  • parent: The parent Transform.
  • tag: The tag to search for.

GetFirstChildWithComponent

public static T GetFirstChildWithComponent<T>(Transform parent) where T : Component

Returns the first child with the specified component, searched recursively.

  • parent: The parent Transform.
  • T: The type of component to search for.

GetChildren

public static List<Transform> GetChildren(Transform parent)

Returns a list of all children of the parent Transform.

  • parent: The parent Transform.

GetTransformsWithTag

public static List<Transform> GetTransformsWithTag(List<Transform> list, string tag)

Returns a list of Transforms from the input list that have the specified tag.

  • list: List of Transform objects to filter.
  • tag: The tag to filter by.

GetChildrenWithTag

public static List<Transform> GetChildrenWithTag(Transform parent, string tag)

Returns a list of children of the parent Transform that have the specified tag.

  • parent: The parent Transform.
  • tag: The tag to filter by.

GetTransformsWithComponent

public static List<Transform> GetTransformsWithComponent<T>(List<Transform> list) where T : Component

Returns a list of Transforms from the input list that have the specified component attached.

  • list: List of Transform objects to filter.
  • T: The type of component to filter by.

GetChildrenWithComponent

public static List<Transform> GetChildrenWithComponent<T>(Transform parent) where T : Component

Returns a list of children of the parent Transform that have the specified component attached.

  • parent: The parent Transform.
  • T: The type of component to filter by.

Math & Time Related

ConvertToOrdinal

public static string ConvertToOrdinal(int number)

Converts an integer to its ordinal representation (e.g., 1 -> 1st, 2 -> 2nd).

  • number: The integer to convert.

GetUUID

public static string GetUUID()

Generates a unique identifier (UUID).

GetUnixTime

public static long GetUnixTime(float hoursOffset = 0f)

Returns the Unix timestamp, optionally offset by a specified number of hours.

  • hoursOffset: The number of hours to offset the timestamp by.

ReverseQuaternion

public static Quaternion ReverseQuaternion(Quaternion quaternion)

Returns the inverse of the given Quaternion.

  • quaternion: The Quaternion to reverse.

UI Related

HexToColor

public static Color HexToColor(string hex)

Converts a hexadecimal color string to a Color object.

  • hex: The hexadecimal color string (e.g., "#FFFFFF").

Singleton Class

public class Singleton<T> : MonoBehaviour where T : MonoBehaviour

A generic Singleton class for MonoBehaviour-based objects. Ensures that only one instance of a type exists.

  • Instance: The Singleton instance.

Methods

Awake

protected virtual void Awake()

Initializes the Singleton instance. If an instance already exists, destroys the new one.

About

A C# script for Unity that adds a bunch of utility functions

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages