namespace SRF { using System.Collections.Generic; using UnityEngine; public static class SRFTransformExtensions { public static IEnumerable GetChildren(this Transform t) { var i = 0; while (i < t.childCount) { yield return t.GetChild(i); ++i; } } /// /// Reset all local values on a transform to identity /// /// public static void ResetLocal(this Transform t) { t.localPosition = Vector3.zero; t.localRotation = Quaternion.identity; t.localScale = Vector3.one; } /// /// Create an empty child object of this transform /// /// /// /// public static GameObject CreateChild(this Transform t, string name) { var go = new GameObject(name); go.transform.parent = t; go.transform.ResetLocal(); go.gameObject.layer = t.gameObject.layer; return go; } /// /// Set the parent of this transform, but maintain the localScale, localPosition, localRotation values. /// /// /// public static void SetParentMaintainLocals(this Transform t, Transform parent) { t.SetParent(parent, false); } /// /// Copy local position,rotation,scale from other transform /// /// /// public static void SetLocals(this Transform t, Transform from) { t.localPosition = from.localPosition; t.localRotation = from.localRotation; t.localScale = from.localScale; } /// /// Set position/rotation to from. Scale is unchanged /// /// /// public static void Match(this Transform t, Transform from) { t.position = from.position; t.rotation = from.rotation; } /// /// Destroy all child game objects /// /// public static void DestroyChildren(this Transform t) { foreach (var child in t) { Object.Destroy(((Transform) child).gameObject); } } } }