Last active
October 16, 2016 08:26
-
-
Save matt123miller/39193a5532b324e34c511c186fe54e4a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections.Generic; | |
public static class Extensions | |
{ | |
// Collections | |
public static T RandomItem<T>(this T[] input) // do arrays and list share a type? | |
{ | |
return input[Random.Range(0, input.Length - 1)]; | |
} | |
//public static T RandomItem<T>(this T input) where T : Something | |
//{ | |
// return input[Random.Range(0, input. - 1)]; | |
//} | |
// Transforms | |
public static void AddChild(this Transform parent, Transform child) | |
{ | |
if (!child.IsChildOf(parent)) | |
{ | |
child.SetParent(child); | |
} | |
} | |
public static void AddChildren(this Transform parent, Transform[] children) | |
{ | |
for (int i = 0; i < children.Length; i++) | |
{ | |
Transform child = children[i]; | |
if (!child.IsChildOf(parent)){ | |
child.SetParent(parent); | |
} | |
} | |
} | |
public static void RemoveFromParent(this Transform child) | |
{ | |
if (child.parent != null) | |
{ | |
child.parent = null; | |
} | |
} | |
public static Transform FindChildWithTag (this Transform parent, string tag) | |
{ | |
foreach (Transform tr in parent) | |
{ | |
if (tr.CompareTag(tag)) | |
{ | |
return tr; | |
} | |
} | |
return null; | |
} | |
public static T FindComponentInChildWithTag<T>(this Transform parent, string tag) where T : Component | |
{ | |
T target = (T)(new Component()); | |
foreach (Transform tr in parent) | |
{ | |
if (tr.CompareTag(tag)) | |
{ | |
target = tr.GetComponent<T>(); | |
} | |
} | |
return target as T; | |
} | |
// 2D | |
// Logic created by @DanInFiction on Twitter, I stole it to make an extension method. | |
public static void LookAt2D(this Transform self, Transform target){ | |
self.right = target.position - self.position; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment