总是会有一些奇奇怪怪的需求,总得根据这些奇奇怪怪的需求去实现对应的功能。
实现:获取某个物体所有子物体身上的材质球。
////// 查找当前物体的子物体包含的所有材质球 /// /// 需要查找的物体 ///返回获取到的所有材质球 public static ListFindChildMaterials(this Transform parent) { List t = new List (); Find (parent, ref t); return t; } private static void Find (Transform parent, ref List list) where T : Object { if (parent.GetComponent ()) { Material[] m = parent.GetComponent ().materials; if (m != null && m.Length > 0) { foreach (var item in m) { list.Add(item); } } } if (parent.GetComponent ()) { Material[] m = parent.GetComponent ().materials; if (m != null && m.Length > 0) { foreach (var item in m) { list.Add(item); } } } int number = parent.childCount; while (number > 0) { for (int i = 0; i < parent.childCount; i++) { if (parent.GetChild(i).childCount > 0) Find (parent.GetChild(i), ref list); number--; if (parent.GetChild(i).GetComponent ()) { Material[] m = parent.GetChild(i).GetComponent ().materials; if (m != null && m.Length > 0) { foreach (var item in m) { list.Add(item); } } } if (parent.GetChild(i).GetComponent ()) { Material[] m = parent.GetChild(i).GetComponent ().materials; if (m != null && m.Length > 0) { foreach (var item in m) { list.Add(item); } } } } } }



