SetParent
How do I use Set Parent
Below are practical examples compiled from projects for learning and reference purposes
Featured Snippets
File name: Lobby.cs
Copy
67 private RoomButton GetRoomButton()
68 {
69 GameObject aGO = (GameObject)Instantiate(roomButtonPrefab, Vector3.zero, Quaternion.identity);
70 aGO.transform.SetParent(grid.transform, false);
71 return aGO.GetComponent
72 }
File name: HorizontalScrollSnap.cs
Copy
107 public void AddChild(GameObject GO, bool WorldPositionStays)
108 {
109 _scroll_rect.horizontalNormalizedPosition = 0;
110 GO.transform.SetParent(_screensContainer, WorldPositionStays);
111 DistributePages();
112 if (MaskArea) UpdateVisible();
113
114 SetScrollContainerPosition();
115 }
File name: HorizontalScrollSnap.cs
Copy
119 /// *Note, this is an index address (0-x)
123 public void RemoveChild(int index, out GameObject ChildRemoved)
124 {
125 ChildRemoved = null;
126 if (index < 0 || index > _screensContainer.childCount)
127 {
128 return;
129 }
130 _scroll_rect.horizontalNormalizedPosition = 0;
131
132 Transform child = _screensContainer.transform.GetChild(index);
133 child.SetParent(null);
134 ChildRemoved = child.gameObject;
135
136 DistributePages();
137 if (MaskArea) UpdateVisible();
138
139 if (_currentPage > _screens - 1)
140 {
141 CurrentPage = _screens - 1;
142 }
143
144 SetScrollContainerPosition();
145 }
File name: HorizontalScrollSnap.cs
Copy
151 public void RemoveAllChildren(out GameObject[] ChildrenRemoved)
152 {
153 var _screenCount = _screensContainer.childCount;
154 ChildrenRemoved = new GameObject[_screenCount];
155
156 for (int i = _screenCount - 1; i >= 0; i--)
157 {
158 ChildrenRemoved[i] = _screensContainer.GetChild(i).gameObject;
159 ChildrenRemoved[i].transform.SetParent(null);
160 }
161
162 _scroll_rect.horizontalNormalizedPosition = 0;
163 CurrentPage = 0;
164 InitialiseChildObjectsFromScene();
165 DistributePages();
166 if (MaskArea) UpdateVisible();
167 }
File name: ScrollSnapBase.cs
Copy
166 internal void InitialiseChildObjectsFromArray()
167 {
168 int childCount = ChildObjects.Length;
169 RectTransform childRect;
170 GameObject child;
171 for (int i = 0; i < childCount; i++)
172 {
173 child = GameObject.Instantiate(ChildObjects[i]);
174 //Optionally, use original GO transform when initialising, by default will use parent RectTransform position/rotation
175 if (UseParentTransform)
176 {
177 childRect = child.GetComponent
178 childRect.rotation = _screensContainer.rotation;
179 childRect.localScale = _screensContainer.localScale;
180 childRect.position = _screensContainer.position;
181 }
182
183 child.transform.SetParent(_screensContainer.transform);
184 ChildObjects[i] = child;
185 if (MaskArea && ChildObjects[i].activeSelf)
186 {
187 ChildObjects[i].SetActive(false);
188 }
189 }
190 }
File name: VerticalScrollSnap.cs
Copy
106 public void AddChild(GameObject GO, bool WorldPositionStays)
107 {
108 _scroll_rect.verticalNormalizedPosition = 0;
109 GO.transform.SetParent(_screensContainer, WorldPositionStays);
110 InitialiseChildObjectsFromScene();
111 DistributePages();
112 if (MaskArea) UpdateVisible();
113
114 SetScrollContainerPosition();
115 }
File name: VerticalScrollSnap.cs
Copy
119 /// *Note, this is an index address (0-x)
123 public void RemoveChild(int index, out GameObject ChildRemoved)
124 {
125 ChildRemoved = null;
126 if (index < 0 || index > _screensContainer.childCount)
127 {
128 return;
129 }
130 _scroll_rect.verticalNormalizedPosition = 0;
131
132 Transform child = _screensContainer.transform.GetChild(index);
133 child.SetParent(null);
134 ChildRemoved = child.gameObject;
135 InitialiseChildObjectsFromScene();
136 DistributePages();
137 if (MaskArea) UpdateVisible();
138
139 if (_currentPage > _screens - 1)
140 {
141 CurrentPage = _screens - 1;
142 }
143
144 SetScrollContainerPosition();
145 }
File name: VerticalScrollSnap.cs
Copy
151 public void RemoveAllChildren(out GameObject[] ChildrenRemoved)
152 {
153 var _screenCount = _screensContainer.childCount;
154 ChildrenRemoved = new GameObject[_screenCount];
155
156 for (int i = _screenCount - 1; i >= 0; i--)
157 {
158 ChildrenRemoved[i] = _screensContainer.GetChild(i).gameObject;
159 ChildrenRemoved[i].transform.SetParent(null);
160 }
161
162 _scroll_rect.verticalNormalizedPosition = 0;
163 CurrentPage = 0;
164 InitialiseChildObjectsFromScene();
165 DistributePages();
166 if (MaskArea) UpdateVisible();
167 }
File name: ScrollSnap.cs
Copy
64 public void PushLayoutElement(LayoutElement element) {
65 element.transform.SetParent(content.transform, false);
66 SetContentSize(LayoutElementCount());
67 }
File name: ScrollSnap.cs
Copy
78 public void UnshiftLayoutElement(LayoutElement element) {
79 cellIndex += 1;
80 element.transform.SetParent(content.transform, false);
81 element.transform.SetAsFirstSibling();
82 SetContentSize(LayoutElementCount());
83 content.anchoredPosition = new Vector2(content.anchoredPosition.x - cellSize.x, content.anchoredPosition.y);
84 }
SetParent 261 lượt xem
Gõ tìm kiếm nhanh...