Skip to content

Instantly share code, notes, and snippets.

@HilariousCow
Created January 13, 2015 12:51
Show Gist options
  • Save HilariousCow/2d11bf6ccaa230a52271 to your computer and use it in GitHub Desktop.
Save HilariousCow/2d11bf6ccaa230a52271 to your computer and use it in GitHub Desktop.
some handy extensions i use in unity to clean up pretty boring instantiate code
using UnityEngine;
using System.Collections;
public static class TransformExtensions
{
public static void SetLayer(this Transform trans, int layer)
{
trans.gameObject.layer = layer;
foreach(Transform child in trans)
child.SetLayer( layer);
}
//Wasn't there a recursive version of this?
public static T FindChild<T>(this Transform t, string path) where T : Component
{
Transform target = t;
string[] pathArray = path.Split('.');
for (int index = 0; index < pathArray.Length; index++)
{
string p = pathArray[index];
target = target.FindChild(p);
}
T targetComponent = target.GetComponent<T>() as T;
return targetComponent;
}
//Find or inject
public static T FindChildOrInject<T>(this Transform t, string path) where T : Component
{
Transform child = t.FindChild<Transform>(path);
if (child == null)
{
Debug.Log("Specified Game Object Not Found... " + path);
return null;
}
T Component = child.GetComponent<T>();
if (Component == null)
{
//Inject and return
return child.gameObject.AddComponent<T>();
} else //Already exists
{
return Component;
}
}
public static Quaternion LookRotation(this Transform trans, Vector3 targetPos)
{
if ((targetPos - trans.position).sqrMagnitude > 0.10f)
{
return Quaternion.LookRotation((targetPos - trans.position), trans.up);
} else
{
return Quaternion.identity;
}
}
public static void ResetToParent(this Transform trans)
{
if (trans is RectTransform)
{
RectTransform uiTransform = trans as RectTransform;
//todo: not sure how to reset this, really?
//typically, if you're spawning ui elements, assume you want to keep what ever offset they had when you were designing them?
}
else
{
trans.localPosition = Vector3.zero;
trans.localRotation = Quaternion.identity;
trans.localScale = Vector3.one;
}
}
public static T InstantiateChild<T>(this Transform trans, string name/* = null*/) where T : MonoBehaviour
{
GameObject go = new GameObject();
go.name = (name == null) ? typeof (T).Name : name;
go.transform.parent = trans;
go.transform.ResetToParent();
return go.AddComponent<T>();
}
public static T InstantiateChild<T>(this Transform trans, T prefab) where T : MonoBehaviour
{
T instantiatedThing = (Object.Instantiate(prefab.gameObject) as GameObject).GetComponent<T>();
if (instantiatedThing.transform is RectTransform)
{
(instantiatedThing.transform as RectTransform).SetParent(trans, false);
}
else
{
instantiatedThing.transform.parent = trans;
}
instantiatedThing.transform.ResetToParent();
return instantiatedThing;
}
public static GameObject InstantiateChild(this Transform trans, GameObject prefab)
{
GameObject instantiatedThing = (Object.Instantiate(prefab) ) as GameObject;
instantiatedThing.transform.parent = trans;
instantiatedThing.transform.ResetToParent();
return instantiatedThing;
}
//Instantiates the given prefab.
//Returns the T given if it's there
//If it's not, it creates it for free and returns that
public static T InstantiateAndInjectChild<T>(this Transform trans, GameObject prefab) where T : MonoBehaviour
{
GameObject prefabHandle = (Object.Instantiate(prefab) as GameObject);
T instantiatedThing = prefabHandle.GetComponent<T>();
if (instantiatedThing == null)
{
instantiatedThing = prefabHandle.AddComponent<T>();
}
instantiatedThing.transform.parent = trans;
instantiatedThing.transform.ResetToParent();
return instantiatedThing;
}
public static void SetPosX(this Transform trans, float x)
{
var temp = trans.position;
temp.x = x;
trans.position = temp;
}
public static void SetFlipX(this Transform trans, bool flipped)
{
var temp = trans.localScale;
float magX = Mathf.Abs(temp.x);
if (flipped)
{
temp.x = -magX;
}
else
{
temp.x = magX;
}
trans.localScale = temp;
}
public static void SetPosY(this Transform trans, float y)
{
var temp = trans.position;
temp.y = y;
trans.position = temp;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment