unity深度查找某個子物體和遍歷所有子物體方法

本文總結一下關于unity的查找子物體的方法

?

首先說明一下這里將講三種查找子物體方法:

查找固定路徑的某一個子物體的方法、通過名字深度查找某個子物體的方法、查找父物體下所有子物體的方法。

?

第一:查找固定路徑的某一個子物體的方法

對于已知的路徑可以直接用go.transform.FindChild方法來查找。

例如:在這樣一個層級路徑下,我們要找到最后那個plane物體。

?

 1 using UnityEngine;
 2 using System.Collections;
 3  
 4 public class findchild : MonoBehaviour {
 5  
 6     // Use this for initialization
 7     void Start () {
 8     
 9     }
10     
11     // Update is called once per frame
12     void Update () {
13         if (Input.GetMouseButtonDown(1))
14         {
15             //查找物體方法
16             GameObject go = GameObject.Find("Cube");
17             //查找子物體,并且將得到的物體轉換成gameobject
18           GameObject objname= go.transform.FindChild("Sphere/Cylinder/Plane").gameObject;
19  
20           Debug.Log("得到最終子物體的名字是:"+ objname.name);
21         }
22     }
23 }

?

然后是執行結果:

?

?

?

==-------------------------------------------------------------------------------------------------------------

?

第二:通過名字深度查找某個子物體的方法

注意:要使用這個方法必須要滿足兩個條件:第一必須有你要查找的子物體的名字,第二必須要從一個父物體上開始查起

?

?

下面代碼中,check代表從這個父物體開始查起,name為你要查找的目標子物體的名稱。如return GetTransform(transform,"bone12");

該方法核心代碼:

?

?

而下面是查找的具體方法:

 1 Transform GetTransform(Transform check, string name)
 2     {
 3         Transform forreturn = null;
 4  
 5         foreach (Transform t in check.GetComponentsInChildren<Transform>())
 6         {
 7             if (t.name == name)
 8             {
 9                 Debug.Log("得到最終子物體的名字是:" + t.name);
10                 forreturn = t;
11                 return t;
12                 
13             }        
14            
15         }
16         return forreturn;
17     }

?

再看完整的測試代碼:還用上個的例子的,例如這次要查到Cylinder這個物體:

?

修改后的代碼:

 1 using UnityEngine;
 2 using System.Collections;
 3  
 4 public class findchild : MonoBehaviour {
 5  
 6     // Use this for initialization
 7     void Start () {
 8     
 9     }
10     
11     // Update is called once per frame
12     void Update () {
13         if (Input.GetMouseButtonDown(1))
14         {
15             //  //查找物體方法
16            GameObject go = GameObject.Find("Cube");
17             //  //查找子物體,并且將得到的物體轉換成gameobject
18             //GameObject objname= go.transform.FindChild("Sphere/Cylinder/Plane").gameObject;
19  
20             //Debug.Log("得到最終子物體的名字是:"+ objname.name);
21              
22  
23             GetTransform(go.transform, "Cylinder");
24             
25         }
26     }
27  
28     Transform GetTransform(Transform check, string name)
29     {
30         Transform forreturn = null;
31  
32         foreach (Transform t in check.GetComponentsInChildren<Transform>())
33         {
34             if (t.name == name)
35             {
36                 Debug.Log("得到最終子物體的名字是:" + t.name);
37                 forreturn = t;
38                 return t;
39                 
40             }        
41            
42         }
43         return forreturn;
44     }
45 }

?

測試結果:

?

-----------------------------------------------------------------------------------------------------

第三:接下來我們將獲取一個父物體下的所有子物體,然后銷毀其下所有子物體

注意:所有子物體都是同級關系,在同一層里。如圖:

?

核心方法:

 1 List<Transform> lst = new List<Transform>();
 2             foreach (Transform child in transform)
 3             {
 4                 lst.Add(child);
 5                 Debug.Log(child.gameObject.name);
 6             }
 7             for (int i = 0; i < lst.Count; i++)
 8             {
 9                 Destroy(lst[i].gameObject);
10             }

?

上面的transform就是該父物體的transform。具體案例代碼:

 1 using UnityEngine;
 2 using System.Collections;
 3 using System.Collections.Generic;
 4  
 5 public class findchild : MonoBehaviour {
 6  
 7     // Use this for initialization
 8     void Start () {
 9     
10     }
11     
12     // Update is called once per frame
13     void Update () {
14         if (Input.GetMouseButtonDown(1))
15         {
16             //  //查找物體方法
17            GameObject go = GameObject.Find("Cube");
18             List<Transform> lst = new List<Transform>();
19             foreach (Transform child in go.transform)
20             {
21                 lst.Add(child);
22                 Debug.Log(child.gameObject.name);
23             }
24             for (int i = 0; i < lst.Count; i++)
25             {
26                 Debug.Log("銷毀的物體是:"+ lst[i].gameObject);
27                 Destroy(lst[i].gameObject);
28             }
29  
30         }
31     }
32  
33    
34 }

測試結果,全被銷毀了:

?

?

以上就是我總結的常用的三種查找子物體的方法。

?

轉載于:https://www.cnblogs.com/macky/p/9335093.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/251220.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/251220.shtml
英文地址,請注明出處:http://en.pswp.cn/news/251220.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

javascript --- JSON字符串化

工具函數JSON.stringify()將JSON對象序列化為字符串時也用到了ToString. 看下面的代碼: console.log(JSON.stringify(42)); console.log(JSON.stringify("42")); console.log(JSON.stringify(null)); console.log(JSON.stringify(true));所有安全的JSON值都可以使用…

靜態鏈接和動態鏈接

靜態鏈接和動態鏈接 靜態鏈接方法&#xff1a;靜態鏈接的時候&#xff0c;載入代碼就會把程序會用到的動態代碼或動態代碼的地址確定下來 靜態庫的鏈接可以使用靜態鏈接&#xff0c;動態鏈接庫也可以使用這種方法鏈接導入庫 動態鏈接方法&#xff1a;使用這種方式的程序并不在一…

ES5-3 循環、引用值初始、顯示及隱式類型轉換

1. 循環 for循環的三個參數abc&#xff0c;a只執行一次&#xff0c;c在每次循環后執行 // 打印0-100的質數 1不是質數 var list [2] for (var i 3; i < 100; i i 2) {var flag falsefor (var j 0; j < list.length; j) {var cur list[j]if (i % cur 0 &…

hihocoder 二分

題目 一個簡單的二分&#xff0c;只是想說明一下&#xff0c;如若要查找一個數組中某個數的下標可以直接用lower_bound()這個函數。只是要考慮到要查找的數不在數組中的這種情況。 #include <cstdio> #include <iostream> #include <algorithm> using namesp…

ubuntu開啟ssh服務

更新資源列表&#xff1a;sudo apt-get update -> 輸入管理員密碼 安裝openssh-server: sudo apt-get install openssh-server 查看 ssh服務是否啟動&#xff1a;sudo ps -e |grep ssh 啟動ssh服務&#xff1a; sudo service ssh start 轉載于:https://www.cnblogs.com/ver…

javascript --- 判斷只有1個為真

下面寫一個用于判斷只有一個為真的函數: function onlyOne(a,b,c){return !!((a && !b && !c) ||(!a && b && !c) || (!a && !b && c)); } var a true; var b false;onlyOne(a,b,b) // true onlyOne(a,b,a); // false上述…

13 代碼分割之import靜動態導入

前端首屏優化方案之一 項目構建時會整體打包成一個bundle的JS文件&#xff0c;而有的代碼、模塊是加載時不需要的&#xff0c;需要分割出來單獨形成一個文件塊chunk&#xff08;不會打包在main里&#xff09;&#xff0c;讓模塊懶加載&#xff08;想加載時才加載&#xff09;&a…

2018.01.01(數字三角形,最長上升子序列等)

2017.12.24 簡單的動態規劃 1.數字三角形(算法引入) 題目描述&#xff1a;下圖所示是一個數字三角形&#xff0c;其中三角形中的數值為正整數&#xff0c;現規定從最頂層往下走到最底層&#xff0c;每一步可沿左斜線向下或右斜線向下走。設三角形有n層&#xff0c;編程計算出從…

Mac iOS 允許從任何來源下載應用并打開

一個快捷的小知識點&#xff0c;mark&#xff01; 允許從任何來源下載應用并打開&#xff0c;不用手動去允許&#xff0c;更加簡潔&#xff01; 只需一行命令 sudo spctl --master-disable 1.正常情況下&#xff0c;打開偏好設置&#xff0c;選擇安全性與隱私&#xff0c;界面是…

ES5-4 函數基礎與種類、形實參及映射、變量類型

模塊編程原則&#xff1a;高內聚&#xff0c;低耦合&#xff08;重復部分少&#xff09;&#xff0c;讓一個模塊有強的功能性、高的獨立性 → 單一責任制&#xff0c;用函數進行解耦合。 1. 函數命名規則 不能以數字開頭可以以字母_$開頭包含數字小駝峰命名法 函數聲明一定有…

javascript --- 抽象相等

字符串和數字之間的相等比較 var a 42; var b "42";a b; // false a b; // trueES5規范11.9.3.4-5定義如下: (1)如果Type(x)是數字,Type(y)是字符串,則返回 x ToNumber(y) 的結果 (2)如果Type(x)是字符串,Type(x)是數字,則返回 ToNumber(x) y 的結果// 總結…

Spring加載context的幾種方法

Spring中的context管理 Spring中IOC容器的初始化&#xff1a; ApplicationContext即是保存bean對象的容器&#xff0c;故容器本身的初始化&#xff0c;就是通過一系列的配置&#xff0c;將ApplicationContext進行初始化。 而配置ApplicationContext大方向上分為了3中&#xff1…

centos 6.5 配置網絡

編輯 vi /etc/sysconfig/network-scripts/ifcfg-eth0修改內容 DEVICE"eth0" BOOTPROTO"static" HWADDR"00:50:56:98:06:D0" IPV6INIT"no" MTU"1500" NM_CONTROLLED"no" ONBOOT"yes" TYPE…

ES5-5 參數默認值、遞歸、預編譯、暗示全局變量

1. 參數默認值 默認是undefined形參可以有默認值&#xff0c;形參、實參哪個有值取哪個ES6&#xff0c;默認值屬于ES6的內容&#xff0c;打印出的是符合人性化的結果形參有默認值&#xff0c;形參、實參無法統一、無論實參傳入有值還是undefined&#xff08;代碼表現&#xff…

javascript --- 優先級執行順序

優先級網址 優先級: a && b || c ? c || b ? a : c && b :a// 從優先級網址可以看出 // &&的優先級為:6 // ||的優先級為:5 // ...?...:...的優先級為:4 所以上面的執行順序為(括號的優先級最高為20): ((a && b) || c) ? (c || b) ?…

CodeForces 1009B(思路)

本來打算打打cf找找自信的&#xff0c;結果&#xff0c;死在了一個2000多人都做出來的B上&#xff0c;寫了170多行wr在t4&#xff0c;大佬十幾行代碼就過了&#xff0c;難受啊。 #include <iostream> #include <cstring> #include <algorithm> #include <…

Delphi及C++Builder經典圖書一覽表(持續更新中2018.01.02)

序號書名原版書名作者譯者出版社頁數年代定價備注1CBuilder 5程序設計大全CBuilder 5 Developer’s GuideJarrod Hollingworth康向東、汪浩、黃金才等機械工業出版社13932002.1138.00元2CBuilder應用開發大全Borland C Builder 3 UnleashedCharlie Calvert,et al.徐科、馮焱、呂…

javascript --- 非交互、交互、協作、任務

非交互: var res {};function foo(results) {res.foo results; }function bar(results) {res.bar results; }ajax( "http://some.url.1", foo); ajax( "http://some.url.2", bar);// foo和bar彼此不相關,誰先執行都無所謂..不影響執行結果交互: // 交…

ES5-6 作用域、作用域鏈、預編譯、閉包基礎

1. 作用域 上一級在執行時&#xff0c;內部函數被定義&#xff0c;內部函數便生成作用域和作用域鏈&#xff08;拿上一級的環境&#xff09;&#xff0c;內部函數執行前生成自己的AO&#xff0c;并排在頭部函數執行結束時&#xff0c;AO被銷毀&#xff08;回到被定義時的狀態&…

electron 項目的搭建方式,借助 node 和 npm

1&#xff0c;首先確定安裝了 node 和 npm 2&#xff0c;創建一個文件夾&#xff0c;如 aa 3&#xff0c;CMD 命令進入到 aa&#xff0c;用 npm 命令初始化一個項目 4&#xff0c; npm -init 根據提示完成配置 5&#xff0c;安裝 electron > npm i -D electronlatest, 這一…