using UnityEngine;
//Unity的一些扩展方法
static public class MethodExtensionForUnity
{
///
/// Gets or add a component. Usage example:
/// BoxCollider boxCollider = transform.GetOrAddComponent();
///
static public T GetOrAddComponent(this Component child, bool set_enable = false) where T : Component
{
T result = child.GetComponent();
if (result == null)
{
result = child.gameObject.AddComponent();
}
var bcomp = result as Behaviour;
if (set_enable)
{
if (bcomp != null) bcomp.enabled = true;
}
return result;
}
static public T GetOrAddComponent(this GameObject go) where T : Component
{
T result = go.transform.GetComponent();
if (result == null)
{
result = go.AddComponent();
}
var bcomp = result as Behaviour;
if (bcomp != null) bcomp.enabled = true;
return result;
}
public static void walk(this GameObject o, System.Action f)
{
f(o);
int numChildren = o.transform.childCount;
for (int i = 0; i < numChildren; ++i)
{
walk(o.transform.GetChild(i).gameObject, f);
}
}
}