GetComponentsInChildren









How do I use Get Components In Children
Below are practical examples compiled from projects for learning and reference purposes

Featured Snippets


File name: Extensions.cs Copy
22     public static PhotonView[] GetPhotonViewsInChildren(this UnityEngine.GameObject go)
23     {
24         return go.GetComponentsInChildren(true) as PhotonView[];
25     }
File name: NetworkingPeer.cs Copy
2493     protected internal void RemoveInstantiatedGO(GameObject go, bool localOnly)
2494     {
2495         if (go == null)
2496         {
2497             Debug.LogError("Failed to 'network-remove' GameObject because it's null.");
2498             return;
2499         }
2500
2501         // Don't remove the GO if it doesn't have any PhotonView
2502         PhotonView[] views = go.GetComponentsInChildren(true);
2503         if (views == null || views.Length <= 0)
2504         {
2505             Debug.LogError("Failed to 'network-remove' GameObject because has no PhotonView components: " + go);
2506             return;
2507         }
2508
2509         PhotonView viewZero = views[0];
2510         int creatorId = viewZero.CreatorActorNr; // creatorId of obj is needed to delete EvInstantiate (only if it's from that user)
2511         int instantiationId = viewZero.instantiationId; // actual, live InstantiationIds start with 1 and go up
2512
2513         // Don't remove GOs that are owned by others (unless this is the master and the remote player left)
2514         if (!localOnly)
2515         {
2516             if (!viewZero.isMine)
2517             {
2518                 Debug.LogError("Failed to 'network-remove' GameObject. Client is neither owner nor masterClient taking over for owner who left: " + viewZero);
2519                 return;
2520             }
2521
2522             // Don't remove the Instantiation from the server, if it doesn't have a proper ID
2523             if (instantiationId < 1)
2524             {
2525                 Debug.LogError("Failed to 'network-remove' GameObject because it is missing a valid InstantiationId on view: " + viewZero + ". Not Destroying GameObject or PhotonViews!");
2526                 return;
2527             }
2528         }
2529
2530
2531         // cleanup instantiation (event and local list)
2532         if (!localOnly)
2533         {
2534             this.ServerCleanInstantiateAndDestroy(instantiationId, creatorId, viewZero.isRuntimeInstantiated); // server cleaning
2535         }
2536
2537
2538         // cleanup PhotonViews and their RPCs events (if not localOnly)
2539         for (int j = views.Length - 1; j >= 0; j--)
2540         {
2541             PhotonView view = views[j];
2542             if (view == null)
2543             {
2544                 continue;
2545             }
2546
2547             // we only destroy/clean PhotonViews that were created by PhotonNetwork.Instantiate (and those have an instantiationId!)
2548             if (view.instantiationId >= 1)
2549             {
2550                 this.LocalCleanPhotonView(view);
2551             }
2552             if (!localOnly)
2553             {
2554                 this.OpCleanRpcBuffer(view);
2555             }
2556         }
2557
2558         if (PhotonNetwork.logLevel >= PhotonLogLevel.Full)
2559             Debug.Log("Network destroy Instantiated GO: " + go.name);
2560
2561         GameObject.Destroy(go);
2562     }
File name: DialogUnity.cs Copy
26         public void setText(string oneline)
27         {
28             bitmapFont = new BitmapFont("Fonts/shop_font", "Fonts/shop_font_xml", label1);
29             bitmapFont.setText(oneline, 0, 15);
30             Transform[] fontTransforms = label1.GetComponentsInChildren(true);
31             for (int i = 0; i < fontTransforms.Length; i++)
32             {
33                 if (fontTransforms[i].gameObject.GetComponent() != null)
34                 {
35                     fontTransforms[i].gameObject.layer = LayerMask.NameToLayer("GUI");
36                     fontTransforms[i].gameObject.GetComponent().sortingLayerName = "GUI";
37                 }
38             }
39         }
File name: DialogUnity.cs Copy
41         public void setText(string line1, string line2)
42         {
43             bitmapFont = new BitmapFont("Fonts/shop_font", "Fonts/shop_font_xml", label1);
44             bitmapFont.setText(line1, 0, 15);
45             Transform[] fontTransforms = label1.GetComponentsInChildren(true);
46             for (int i = 0; i < fontTransforms.Length; i++)
47             {
48
49                 if (fontTransforms[i].gameObject.GetComponent() != null)
50                 {
51                     fontTransforms[i].gameObject.layer = LayerMask.NameToLayer("GUI");
52                     fontTransforms[i].gameObject.GetComponent().sortingLayerName = "GUI";
53                 }
54             }
55
56             BitmapFont bitmapFont2 = new BitmapFont(bitmapFont, label2);
57             bitmapFont2.setText(line2, 0, 15);
58             Transform[] fontTransforms2 = label2.GetComponentsInChildren(true);
59             for (int i = 0; i < fontTransforms2.Length; i++)
60             {
61                 if (fontTransforms2[i].gameObject.GetComponent() != null)
62                 {
63                     fontTransforms2[i].gameObject.layer = LayerMask.NameToLayer("GUI");
64                     fontTransforms2[i].gameObject.GetComponent().sortingLayerName = "GUI";
65                 }
66             }
67         }
File name: Animals.cs Copy
77         private void setSortingLayer(GameObject gObject, string sortingLayerName)
78         {
79             Transform[] transforms = gObject.GetComponentsInChildren(true);
80             for (int i = 0; i < transforms.Length; i++)
81             {
82                 if (transforms[i].GetComponent() != null)
83                     transforms[i].GetComponent().sortingLayerName = sortingLayerName;
84             }
85         }
File name: GameScreen.cs Copy
62         public void Start()
63         {
64             InputController.Name = InputNames.DIALOG;
65
66             string[] mapNames = new string[] {"jungle","southpole","desert","volcano"};
67             int[] lvs = new int[] { 1, 2, 3, 2, 3, 4, 3, 4, 5, 1, 2, 4, 3, 4, 5 };
68
69             Debug.Log("WorldIndex : " + Attr.currentWorld + ", LevelIndex : " + Attr.currentLevel);
70
71             mapObject = (GameObject)Instantiate(Resources.Load("Maps/" + mapNames[Attr.currentWorld] + lvs[Attr.currentLevel]), new Vector3(-4, 2.5f, 0), Quaternion.identity);
72             mapObject.transform.localScale = new Vector3(0.01f, 0.01f, mapObject.transform.localScale.z);
73
74             revivalPositions = new List();
75
76             getSkillChoosed();
77
78             setupMap(mapObject);
79
80             int mapLayer = LayerMask.NameToLayer("Map");
81             mapObject.layer = mapLayer;
82
83             Transform[] transforms = mapObject.GetComponentsInChildren(true);
84             for (int i = 0; i < transforms.Length; i++)
85             {
86                 transforms[i].gameObject.layer = mapLayer;
87             }
88
89             shopFont = new BitmapFont("Fonts/shop_font", "Fonts/shop_font_xml", null);
90
91             BitmapFont comboFont = new BitmapFont("Fonts/font_combo", "Fonts/font_combo_xml", null);
92             combo1.setFont(comboFont);
93             combo2.setFont(comboFont);
94
95             createDialog();
96
97             m_gold = 0;
98             m_time = 0;
99             m_score = 0;
100
101             perSkills = new int[] { 30, 30, 35, 35, 40, 40, 45, 45, 50, 55, 60, 60, 65, 65, 70 };
102
103             isRunning = true;
104             //prepareGame();//khong goi o day vi co the chua chay het cac ham Start
105             isPrepare = true;
106         }
File name: SortingLayerShop.cs Copy
8     public void Start()
9     {
10         Transform[] transforms = gameObject.GetComponentsInChildren(true);
11         for (int i = 0; i < transforms.Length; i++)
12         {
13             GameObject gObject = transforms[i].gameObject;
14             if (gObject.GetComponent() != null)
15                 gObject.GetComponent().sortingLayerName = sortingName;
16         }
17     }
File name: ScrollSnap.cs Copy
69  public void PopLayoutElement() {
70   LayoutElement[] elements = content.GetComponentsInChildren();
71   Destroy(elements[elements.Length - 1].gameObject);
72   SetContentSize(LayoutElementCount() - 1);
73   if(cellIndex == CalculateMaxIndex()) {
74    cellIndex -= 1;
75   }
76  }
File name: ScrollSnap.cs Copy
195  void WrapElementAround() {
196   if(cellIndex <= 0) {
197    var elements = content.GetComponentsInChildren();
198    elements[elements.Length - 1].transform.SetAsFirstSibling();
199    cellIndex += 1;
200    content.anchoredPosition = new Vector2(content.anchoredPosition.x - cellSize.x, content.anchoredPosition.y);
201   } else if(cellIndex >= CalculateMaxIndex()) {
202    var element = content.GetComponentInChildren();
203    element.transform.SetAsLastSibling();
204    cellIndex -= 1;
205    content.anchoredPosition = new Vector2(content.anchoredPosition.x + cellSize.x, content.anchoredPosition.y);
206   }
207  }
File name: BitmapFont.cs Copy
187     private void removeChildren()
188     {
189         if (fontObject.transform.childCount > 0)
190         {
191             Transform[] children = fontObject.GetComponentsInChildren();
192             for (int i = 0; i < children.Length; i++)
193                 Object.Destroy(children[i].gameObject);
194         }
195     }

GetComponentsInChildren 123 lượt xem

Gõ tìm kiếm nhanh...