Skip to content

Instantly share code, notes, and snippets.

@SonicZentropy
Created May 10, 2020 00:02
Show Gist options
  • Save SonicZentropy/f99583a80e3da59ef6a9049eb5a33f9f to your computer and use it in GitHub Desktop.
Save SonicZentropy/f99583a80e3da59ef6a9049eb5a33f9f to your computer and use it in GitHub Desktop.

Revisions

  1. SonicZentropy created this gist May 10, 2020.
    308 changes: 308 additions & 0 deletions flags.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,308 @@
    #pragma warning disable 0414, 0219, 649, 169, 1570
    using System;
    using UnityEngine;
    using System.Collections.Generic;
    using System.Linq;

    public enum Tags : long
    {
    None = 0,
    Player = 1 << 0,
    NPC = 1 << 1,
    Eclipse = 1 << 2,
    Foreground = 1 << 3,
    Middleground = 1 << 4,
    Background = 1 << 5,
    Weapons = 1 << 6,
    Lighting = 1 << 7,
    Laser = 1 << 8,
    Missile = 1 << 9,
    Enemy = 1 << 10,
    Ally = 1 << 11,
    Station = 1 << 12,
    Planet = 1 << 13,
    Star = 1 << 14,
    PlayerRangeTrigger = 1 << 15,
    DisableDistanceTrigger = 1 << 16,
    Ship = 1 << 17
    }

    public static class ExtensionMethodsZen
    {
    public static T GetComponentDownward<T>(this Component gameObject)
    where T : Component
    {
    T comp = gameObject.GetComponent<T>();
    if (comp == null)
    comp = gameObject.GetComponentInChildren<T>();

    return comp;
    }

    public static T GetComponentDownward<T>(this GameObject gameObject)
    where T : Component
    {
    T comp = gameObject.GetComponent<T>();
    if (comp == null)
    comp = gameObject.GetComponentInChildren<T>();

    return comp;
    }

    public static T GetComponentDownThenUp<T>(this Component gameObject)
    where T : Component
    {
    T comp = gameObject.GetComponentDownward<T>();
    if (comp == null)
    comp = gameObject.GetComponentUpward<T>();

    return comp;
    }

    public static T GetComponentDownThenUp<T>(this GameObject gameObject)
    where T : Component
    {
    T comp = gameObject.GetComponentDownward<T>();
    if (comp == null)
    comp = gameObject.GetComponentUpward<T>();

    return comp;
    }

    public static T GetComponentUpward<T>(this Component gameObject)
    where T : Component
    {
    T result = gameObject.GetComponent<T>();
    if (result != null) return result;

    for (Transform t = gameObject.transform; t != null; t = t.parent)
    {
    result = t.GetComponent<T>();
    if (result != null)
    return result;
    }

    return null;
    }

    public static T GetComponentUpward<T>(this GameObject gameObject)
    where T : Component
    {
    T result = gameObject.GetComponent<T>();
    if (result != null) return result;

    for (Transform t = gameObject.transform; t != null; t = t.parent)
    {
    result = t.GetComponent<T>();
    if (result != null)
    return result;
    }

    return null;
    }

    public static T[] GetComponentsInParentsAsArray<T>(this Component gameObject)
    where T : Component
    {
    List<T> results = new List<T>();
    for (Transform t = gameObject.transform; t != null; t = t.parent)
    {
    T result = t.GetComponent<T>();
    if (result != null)
    results.Add(result);
    }

    return results.ToArray();
    }

    public static List<T> GetComponentsInParents<T>(this Component gameObject)
    where T : Component
    {
    List<T> results = new List<T>();
    for (Transform t = gameObject.transform; t != null; t = t.parent)
    {
    T result = t.GetComponent<T>();
    if (result != null)
    results.Add(result);
    }

    return results;
    }

    public static List<T> GetComponentsDownward<T>(this Component gameObject)
    where T : Component
    {
    List<T> results = new List<T>();
    results.AddRange( gameObject.GetComponents<T>().ToList() );
    results.AddRange(gameObject.GetComponentsInChildren<T>().ToList());

    return results;
    }

    public static T[] GetComponentsDownwardAsArray<T>(this Component gameObject)
    where T : Component
    {
    List<T> results = new List<T>();
    results.AddRange(gameObject.GetComponents<T>().ToList());
    results.AddRange(gameObject.GetComponentsInChildren<T>().ToList());

    return results.ToArray();
    }

    public static void SetAllActiveRecursively(this GameObject rootObject, bool active)
    {
    rootObject.SetActive(active);

    foreach (Transform childTransform in rootObject.transform)
    {
    SetAllActiveRecursively(childTransform.gameObject, active);
    }
    }

    public static void SetChildrenLayerRecursively(this GameObject rootObject)
    {
    int newLayer = rootObject.layer;
    foreach (Transform t in rootObject.transform)
    t.gameObject.layer = newLayer;
    }


    public static T[] RemoveAt<T>(this T[] source, int index)
    {
    T[] dest = new T[source.Length - 1];
    int i = 0, j = 0;

    while (i < source.Length)
    {
    if (i != index)
    {
    dest[j] = source[i];
    j++;
    }
    i++;
    }

    return dest;
    }

    /// <summary>
    /// Gets or add a component. Usage example:
    /// BoxCollider boxCollider = transform.GetOrAddComponent<BoxCollider>();
    /// </summary>
    public static T GetOrAddComponent<T>(this Component child) where T : Component
    {
    T result = child.GetComponent<T>();
    if (result == null)
    {
    result = child.gameObject.AddComponent<T>();
    }
    return result;

    }

    // public static void AddZenTag(this GameObject go, Tags tagToAdd)
    // {
    // go.Tags |= (long)tagToAdd;
    // }

    public static void AddZenTags(this GameObject go, params Tags[] tagsToAdd)
    {
    for (int i = 0; i < tagsToAdd.Length; i++)
    go.Tags |= (long)tagsToAdd[i];
    }

    public static void RemoveZenTags(this GameObject go, params Tags[] tagsToRemove)
    {
    for (int i = 0; i < tagsToRemove.Length; i++)
    go.Tags &= ~((long)tagsToRemove[i]);
    }

    public static bool HasZenTag(this GameObject go, Tags tagToCheck)
    {
    return ((go.Tags & (long)tagToCheck) != 0);

    }

    public static bool HasZenTagUpward(this GameObject go, Tags tagToCheck)
    {
    bool tagFound = false;

    for (Transform g = go.transform; g != null; g = g.transform.parent)
    {
    if (g.gameObject.HasZenTag(tagToCheck))
    return true;
    }

    return false;

    }

    public static bool HasNoZenTag(this GameObject go, Tags tagToCheck)
    {
    return ((go.Tags & (long)tagToCheck) == 0);

    }

    public static void ClearZenTags(this GameObject go)
    {
    go.Tags = 0;
    }

    public static RaycastHit2D[] FilterObjects(this RaycastHit2D[] hits, params GameObject[] objsToFilter)
    {
    List<RaycastHit2D> filtered = new List<RaycastHit2D>(hits.Length);
    for (int j = 0; j < hits.Length; j++)
    {
    bool shouldFilter = false;
    for (int k = 0; k < objsToFilter.Length; k++)
    {
    if (hits[j] && hits[j].transform.gameObject == objsToFilter[k])
    {
    shouldFilter = true;
    }
    }

    if (!shouldFilter) filtered.Add(hits[j]);
    }

    return filtered.ToArray();
    }

    //[UnityEditor.Callbacks.DidReloadScripts]
    //private static void OnScriptsReloaded()
    //{
    // var logEntries = System.Type.GetType("UnityEditorInternal.LogEntries,UnityEditor.dll");
    // var clearMethod = logEntries.GetMethod("Clear", System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public);
    // clearMethod.Invoke(null, null);
    //}

    public static bool HasFlag(this Enum e, Enum flag)
    {
    return (Convert.ToInt64(e) & Convert.ToInt64(flag)) != 0;
    // Check whether the flag was given
    //if (flag == null)
    //{
    // throw new ArgumentNullException(nameof(flag));
    //}
    //
    //// Get the type code of the enumeration
    //var typeCode = e.GetTypeCode();
    //
    //// If the underlying type of the flag is signed
    //if (typeCode == TypeCode.SByte || typeCode == TypeCode.Int16 || typeCode == TypeCode.Int32 || typeCode == TypeCode.Int64)
    //{
    // return (Convert.ToInt64(e) & Convert.ToInt64(flag)) != 0;
    //}
    //
    //// If the underlying type of the flag is unsigned
    //if (typeCode == TypeCode.Byte || typeCode == TypeCode.UInt16 || typeCode == TypeCode.UInt32 || typeCode == TypeCode.UInt64)
    //{
    // return (Convert.ToUInt64(e) & Convert.ToUInt64(flag)) != 0;
    //}
    //
    //// Unsupported flag type
    //throw new Exception($"The comparison of the type {e.GetType().Name} is not implemented.");
    }

    }