Created
February 12, 2018 14:28
-
-
Save drZool/790702013ca100b40f0ea0852de4cd9a to your computer and use it in GitHub Desktop.
This file contains hidden or 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; | |
| using System.Collections.Generic; | |
| public static class TransformExtensions | |
| { | |
| static Stack<Transform> moveTargets = new Stack<Transform> (); //This is only used by SetLayerToThisAndChildren | |
| public static void SetLayerToThisAndChildren (this Transform root, int layer) | |
| { | |
| moveTargets.Push (root); | |
| Transform currentTarget; | |
| while (moveTargets.Count != 0) { | |
| currentTarget = moveTargets.Pop (); | |
| currentTarget.gameObject.layer = layer; //.gameObject adds GC. | |
| int childCount = currentTarget.childCount; | |
| for (int i = 0; i < childCount; ++i) | |
| moveTargets.Push (currentTarget.GetChild (i)); //.GetChild(i) adds GC. | |
| } | |
| } | |
| public static string GetPath (this Transform t) | |
| { | |
| if (t.parent == null) | |
| return t.name; | |
| return t.parent.GetPath () + "/" + t.name; | |
| } | |
| public static T GetOrAddComponent<T> (this Component c) where T:Component | |
| { | |
| T instance = c.GetComponent<T> (); | |
| if (instance == null) | |
| instance = c.gameObject.AddComponent<T> (); | |
| return instance; | |
| } | |
| public static void SetTransformRectToPlayerCamera (this RectTransform rectTransform, Camera cam) | |
| { | |
| if (cam == null) | |
| return; | |
| var min = rectTransform.anchorMin; | |
| min.x = cam.rect.x; | |
| rectTransform.anchorMin = min; | |
| var max = rectTransform.anchorMax; | |
| max.x = rectTransform.anchorMin.x + cam.rect.width; | |
| rectTransform.anchorMax = max; | |
| } | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment