Unity3D_06_根據Transform、GameObject和Tag獲取子對象集合


導引:

因為項目中難免要多次進行獲取子對象或者子對象的集合,所以寫一個單獨的類,用來做這些操作。然后再實際的項目中,只需要使用 transform 或者 gameobject 調用這些方法就可以快速的得到這些數據,而並不需要自己在每個單獨的類里面都寫上一遍。

代碼如下:

  1 using System;  
  2 using System.Collections.Generic;  
  3 using System.Linq;  
  4 using System.Text;  
  5 using UnityEngine;  
  6 
  7 
  8 public static partial class ExtentionMethod  
  9 {  
 10     /// <summary>  
 11     /// 獲取子對象變換集合  
 12     /// </summary>  
 13     /// <param name="obj"></param>  
 14     /// <returns></returns>  
 15     public static List<Transform> GetChildCollection(this Transform obj)  
 16     {  
 17         List<Transform> list = new List<Transform>();  
 18         for (int i = 0; i < obj.childCount; i++)  
 19         {  
 20             list.Add(obj.GetChild(i));  
 21         }  
 22         return list;  
 23     }  
 24 
 25     /// <summary>  
 26     /// 獲取子對象集合  
 27     /// </summary>  
 28     /// <param name="obj"></param>  
 29     /// <returns></returns>  
 30     public static List<GameObject> GetChildCollection(this GameObject obj)  
 31     {  
 32         var list = obj.transform.GetChildCollection();  
 33         return list.ConvertAll(T => T.gameObject);  
 34     }  
 35 
 36     public static Transform GetRootParent(this Transform obj)  
 37     {  
 38         Transform Root = obj.parent;  
 39         while(Root.parent != null)  
 40         {  
 41             //Root = Root.root;   //transform.root,方法可以直接獲取最上父節點。  
 42             Root = Root.parent;  
 43         }  
 44         return Root;  
 45     }  
 46 
 47     /// <summary>  
 48     /// 把源對象身上的所有組件,添加到目標對象身上  
 49     /// </summary>  
 50     /// <param name="origin">源對象</param>  
 51     /// <param name="target">目標對象</param>  
 52     public static void CopyComponent(GameObject origin, GameObject target)  
 53     {  
 54         var originComs = origin.GetComponents<Component>();  
 55         foreach (var item in originComs)  
 56         {  
 57             target.AddComponent(item.GetType());  
 58         }  
 59     }  
 60 
 61     /// <summary>  
 62     /// 改變游戲腳本  
 63     /// </summary>  
 64     /// <param name="origin"></param>  
 65     /// <param name="target"></param>  
 66     public static void ChangeScriptTo(this MonoBehaviour origin, MonoBehaviour target)  
 67     {  
 68         target.enabled = true;  
 69         origin.enabled = false;  
 70     }  
 71 
 72 
 73     /// <summary>  
 74     /// 從當前對象的子對象中查找,返回一個用tag做標識的活動的游戲物體的鏈表.如果沒有找到則為空.   
 75     /// </summary>  
 76     /// <param name="obj">對象Transform</param>  
 77     /// <param name="tag">標簽</param>  
 78     /// <param name="transList">結果Transform集合</param> // 對一個父對象進行遞歸遍歷,如果有子對象的tag和給定tag相符合時,則把該子對象存到 鏈表數組中  
 79     public static void FindGameObjectsWithTagRecursive(this Transform obj, string tag, ref List<Transform> transList)  
 80     {  
 81         foreach (var item in obj.transform.GetChildCollection())  
 82         {  
 83             // 如果子對象還有子對象,則再對子對象的子對象進行遞歸遍歷  
 84             if (item.childCount > 0)  
 85             {  
 86                 item.FindGameObjectsWithTagRecursive(tag, ref transList);  
 87             }  
 88 
 89             if (item.tag == tag)  
 90             {  
 91                 transList.Add(item);  
 92             }  
 93         }  
 94     }  
 95 
 96     public static void FindGameObjectsWithTagRecursive(this GameObject obj, string tag, ref List<GameObject> objList)  
 97     {  
 98         List<Transform> list = new List<Transform>();  
 99         obj.transform.FindGameObjectsWithTagRecursive(tag, ref list);  
100 
101         objList.AddRange(list.ConvertAll(T => T.gameObject));  
102     }  
103 
104     /// <summary>  
105     /// 從父對象中查找組件  
106     /// </summary>  
107     /// <typeparam name="T">組件類型</typeparam>  
108     /// <param name="com">物體組件</param>  
109     /// <param name="parentLevel">向上查找的級別,使用 1 表示與本對象最近的一個級別</param>  
110     /// <param name="searchDepth">查找深度</param>  
111     /// <returns>查找成功返回相應組件對象,否則返回null</returns>  
112     public static T GetComponentInParent<T>(this Component com, int parentLevel = 1, int searchDepth = int.MaxValue) where T : Component  
113     {  
114         searchDepth--;  
115 
116         if (com != null && searchDepth > 0)  
117         {  
118             var component = com.transform.parent.GetComponent<T>();  
119             if (component != null)  
120             {  
121                 parentLevel--;  
122                 if (parentLevel == 0)  
123                 {  
124                     return component;  
125                 }  
126             }  
127 
128             return com.transform.parent.GetComponentInParent<T>(parentLevel, searchDepth);  
129         }  
130 
131         return null;  
132     }      
133 }  

 

補充:Unity中三種調用其他腳本函數的方法

第一種:被調用腳本函數為static類型,調用時直接用 腳本名.函數名()。很不實用~

第二種:GameObject.Find(“腳本所在物體名”).SendMessage(“函數名”); 此種方法可以調用public和private類型函數

第三種:GameObject.Find(“腳本所在物體名”).GetComponent<腳本名>().函數名();此種方法只可以調用public類型函數


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM