Skip to content

Instantly share code, notes, and snippets.

@aberloni
Last active September 29, 2015 08:13
Show Gist options
  • Save aberloni/f03f2196cbaac6e08806 to your computer and use it in GitHub Desktop.
Save aberloni/f03f2196cbaac6e08806 to your computer and use it in GitHub Desktop.
A series of useful statics functions to work in Unity
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class UnityTools {
static public Transform fetchInChildren(Transform parent, string partName){
foreach(Transform t in parent){
if(t.name.IndexOf(partName) > -1) return t;
Transform child = fetchInChildren(t, partName);
if(child != null) return child;
}
return null;
}
static public bool isInChildren(Transform parent, Transform target){
bool isIn = false;
if(parent == target) isIn = true;
if(!isIn){
foreach(Transform child in parent){
if(isIn) continue;
if(child == target) isIn = true;
if(child.childCount > 0) isIn = isInChildren(child, target);
}
}
return isIn;
}
static public Transform[] getAllTransform(Transform t){
List<Transform> trs = new List<Transform>();
trs.Add(t);
foreach(Transform child in t){
if(child.childCount > 0){
Transform[] children = getAllTransform(child);
//for (int i = 0; i < children.Length; i++) Debug.Log(child.name+" >> "+children[i].name);
trs.AddRange(children);
}else{
trs.Add(child);
}
}
return trs.ToArray();
}
static public string getFullPath(Transform obj){
string path = obj.name;
Transform parent = obj.parent;
while(parent != null){
path = parent.name + "/" + path;
parent = parent.parent;
}
return path;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment