UnityEssentials
Small but useful tools and features for Unity
TransformExtensions.cs
Go to the documentation of this file.
1 using System.Collections.Generic;
2 using System.Linq;
3 
4 namespace UnityEngine
5 {
9  public static class TransformExtensions
10  {
14  public static void ResetTransform(this Transform self)
15  {
16  self.SetProperties(Vector3.zero, Quaternion.identity, Vector3.one);
17  }
18 
23  public static void SetProperties(this Transform self, Transform properties)
24  {
25  self.SetProperties(properties.position, properties.rotation, properties.localScale);
26  }
33  public static void SetProperties(this Transform self, Vector3 position, Quaternion rotation, Vector3 scale)
34  {
35  self.position = position;
36  self.rotation = rotation;
37  self.localScale = scale;
38  }
44  public static void SetProperties(this Transform self, Quaternion rotation, Vector3 scale)
45  {
46  self.rotation = rotation;
47  self.localScale = scale;
48  }
54  public static void SetProperties(this Transform self, Vector3 position, Vector3 scale)
55  {
56  self.position = position;
57  self.localScale = scale;
58  }
64  public static void SetProperties(this Transform self, Vector3 position, Quaternion rotation)
65  {
66  self.position = position;
67  self.rotation = rotation;
68  }
69 
75  public static Quaternion GetLookAtRotation(this Transform self, Vector3 target)
76  {
77  return Quaternion.LookRotation(target - self.position);
78  }
84  public static Quaternion GetLookAtRotation(this Transform self, Transform target)
85  {
86  return GetLookAtRotation(self, target.position);
87  }
88 
93  public static void LookAway(this Transform self, Vector3 target)
94  {
95  self.rotation = GetLookAwayRotation(self, target);
96  }
101  public static void LookAway(this Transform self, Transform target)
102  {
103  self.rotation = GetLookAwayRotation(self, target);
104  }
105 
111  public static Quaternion GetLookAwayRotation(this Transform self, Vector3 target)
112  {
113  return Quaternion.LookRotation(self.position - target);
114  }
115 
121  public static Quaternion GetLookAwayRotation(this Transform self, Transform target)
122  {
123  return GetLookAwayRotation(self, target.position);
124  }
125 
130  public static void SetLerp(this Transform self, Transform a, Transform b, float t)
131  {
132  self.SetProperties(Vector3.Lerp(a.position, b.position, t), Quaternion.Lerp(a.rotation, b.rotation, t), Vector3.Lerp(a.localScale, b.localScale, t));
133  }
134 
140  public static void DestroyImmediateAllChildren(this Transform self, IEnumerable<Transform> exceptions = null)
141  {
142  Transform[] exceptionsArray = exceptions != null ? exceptions.ToArray() : new Transform[0];
143 
144  while ((exceptions == null && self.childCount > 0) || (exceptions != null && self.childCount > exceptionsArray.Length))
145  foreach (Transform child in self)
146  if (exceptions == null || !exceptionsArray.Contains(child))
147  Object.DestroyImmediate(child.gameObject);
148  }
149 
154  public static void DestroyAllChildren(this Transform self, IEnumerable<Transform> exceptions = null)
155  {
156  Transform[] exceptionsArray = exceptions != null ? exceptions.ToArray() : new Transform[0];
157 
158  //while ((exceptions == null && self.childCount > 0) || (exceptions != null && self.childCount > exceptionsArray.Length))
159  foreach (Transform child in self)
160  if (exceptions == null || !exceptionsArray.Contains(child))
161  Object.Destroy(child.gameObject);
162  }
163 
164  }
165 }
Extensions for the Transform component
static void SetProperties(this Transform self, Vector3 position, Vector3 scale)
Sets the transform position and scale to the given values.
static Quaternion GetLookAtRotation(this Transform self, Transform target)
Find the rotation to look at a target.
static void DestroyAllChildren(this Transform self, IEnumerable< Transform > exceptions=null)
Destroys the children of a transform.
static Quaternion GetLookAwayRotation(this Transform self, Vector3 target)
Find the rotation to look in the opposite direction of the target.
static void SetLerp(this Transform self, Transform a, Transform b, float t)
Linearly interpolates between two transforms.
static void SetProperties(this Transform self, Vector3 position, Quaternion rotation)
Sets the transform position and rotation to the given values.
static void LookAway(this Transform self, Vector3 target)
Instantly look in the opposite direction of the target.
static void LookAway(this Transform self, Transform target)
Instantly look in the opposite direction of the target.
static Quaternion GetLookAwayRotation(this Transform self, Transform target)
Find the rotation to look away from a target transform.
static void SetProperties(this Transform self, Vector3 position, Quaternion rotation, Vector3 scale)
Sets the transform position, rotation and scale to the given values.
static void ResetTransform(this Transform self)
Sets the transform position, rotation and scale to the default values. Position = (0,...
static Quaternion GetLookAtRotation(this Transform self, Vector3 target)
Find the rotation to look at a target.
static void SetProperties(this Transform self, Transform properties)
Sets the transform position, rotation and scale to the same values in the given transform.
static void SetProperties(this Transform self, Quaternion rotation, Vector3 scale)
Sets the transform rotation and scale to the given values.
static void DestroyImmediateAllChildren(this Transform self, IEnumerable< Transform > exceptions=null)
Destroys immediately the children of a transform.