#if !NETFX_CORE using UnityEngine; using System.Collections; using System.Collections.Generic; using System; using System.Linq; namespace HeurekaGames { public static class Extensions { #region Vector3 public static Vector2 YZ(this Vector3 v) { return new Vector2(v.x, v.z); } public static Vector2[] YZ(this Vector3[] v) { Vector2[] new2DArray = new Vector2[v.Length]; for (int i = 0; i < v.Length; i++) { new2DArray[i] = new Vector2(v[i].x, v[i].z); } return new2DArray; } #endregion #region Float public static float Remap(this float value, float from1, float to1, float from2, float to2) { return (value - from1) / (to1 - from1) * (to2 - from2) + from2; } #endregion #region String public static string ToCamelCase(this string camelCaseString) { return System.Text.RegularExpressions.Regex.Replace(camelCaseString, "([a-z](?=[A-Z])|[A-Z](?=[A-Z][a-z]))", "$1 ").Trim(); } #endregion #region GameObject public static void SetComponentRecursively(this GameObject gameObject, bool tf) where T : Component { T[] comps = gameObject.GetComponentsInChildren(); foreach (T comp in comps) { try { System.Reflection.PropertyInfo pi = (typeof(T)).GetProperty("enabled"); if (null != pi && pi.CanWrite) { pi.SetValue(comp, tf, null); } else { Console.WriteLine("BLABLA"); Debug.Log("Property does not exist, or cannot write"); } } catch (NullReferenceException e) { Debug.Log("The property does not exist in MyClass." + e.Message); } } } #endregion #region List public static void CastList(this List targetList) { targetList = targetList.Cast().ToList(); } #endregion #region Enums and flags public static bool Has(this System.Enum type, T value) { try { return (((int)(object)type & (int)(object)value) == (int)(object)value); } catch { return false; } } public static bool Is(this System.Enum type, T value) { try { return (int)(object)type == (int)(object)value; } catch { return false; } } public static T Add(this System.Enum type, T value) { try { return (T)(object)(((int)(object)type | (int)(object)value)); } catch (Exception ex) { throw new ArgumentException( string.Format( "Could not append value from enumerated type '{0}'.", typeof(T).Name ), ex); } } public static T Remove(this System.Enum type, T value) { try { return (T)(object)(((int)(object)type & ~(int)(object)value)); } catch (Exception ex) { throw new ArgumentException( string.Format( "Could not remove value from enumerated type '{0}'.", typeof(T).Name ), ex); } } #endregion #region Color public static Color ModifiedAlpha(this Color color, float alpha) { Color modifiedColor = color; modifiedColor.a = alpha; return modifiedColor; } #endregion } } #endif