Class GameObjectUtils
Utility methods for creating GameObjects Allows systems to subscribe to GameObjectInstantiated
Namespace: Unity.XR.CoreUtils
Syntax
public static class GameObjectUtils
Methods
ClonePrefabWithHideFlags(GameObject, Transform)
Clones the Prefab Game Object prefab
and copies the hide flags of each Game Object
in its hierarchy to the corresponding Game Object in the copy's hierarchy.
Declaration
public static GameObject ClonePrefabWithHideFlags(GameObject prefab, Transform parent = null)
Parameters
Type | Name | Description |
---|---|---|
GameObject | prefab | The Prefab Game Object to make a copy of |
Transform | parent | Optional parent that will be assigned to the clone of the original Game Object |
Returns
Type | Description |
---|---|
GameObject | The clone of the original Game Object |
CloneWithHideFlags(GameObject, Transform)
Clones the Game Object original
and copies the hide flags of each Game Object
in its hierarchy to the corresponding Game Object in the copy's hierarchy.
Declaration
public static GameObject CloneWithHideFlags(GameObject original, Transform parent = null)
Parameters
Type | Name | Description |
---|---|---|
GameObject | original | The Game Object to make a copy of |
Transform | parent | Optional parent that will be assigned to the clone of the original Game Object |
Returns
Type | Description |
---|---|
GameObject | The clone of the original Game Object |
Create()
Creates a new GameObject and returns it. This method also calls GameObjectInstantiated.
Declaration
public static GameObject Create()
Returns
Type | Description |
---|---|
GameObject | The new GameObject |
Create(String)
Creates a new GameObject and returns it. This method also calls GameObjectInstantiated.
Declaration
public static GameObject Create(string name)
Parameters
Type | Name | Description |
---|---|---|
String | name | The name to be given to the new GameObject |
Returns
Type | Description |
---|---|
GameObject | The new GameObject |
ExhaustiveComponentSearch<T>(GameObject)
Searches for a component in a scene with a 3 step process, getting more comprehensive with each step At edit time will find all objects in the scene, even if they are disabled At play time, will be unable to find disabled objects that are not a child of desiredSource
Declaration
public static T ExhaustiveComponentSearch<T>(GameObject desiredSource)
where T : Component
Parameters
Type | Name | Description |
---|---|---|
GameObject | desiredSource | The Game Object we expect to be a parent or owner of the component |
Returns
Type | Description |
---|---|
T | A component of the desired type, or NULL if no component was located |
Type Parameters
Name | Description |
---|---|
T | The type of component to find in the scene |
ExhaustiveTaggedComponentSearch<T>(GameObject, String)
Searches for a component in a scene with a 3 step process, getting more comprehensive with each step At edit time will find all objects in the scene, even if they are disabled At play time, will be unable to find disabled objects that are not a child of desiredSource
Declaration
public static T ExhaustiveTaggedComponentSearch<T>(GameObject desiredSource, string tag)
where T : Component
Parameters
Type | Name | Description |
---|---|---|
GameObject | desiredSource | The GameObject we expect to be a parent or owner of the component |
String | tag | The tag this component must have to match |
Returns
Type | Description |
---|---|
T | A component of the desired type, or NULL if no component was located |
Type Parameters
Name | Description |
---|---|
T | The type of component to find in the scene |
GetChildGameObjects(GameObject, List<GameObject>)
Get the direct children GameObjects of this GameObject.
Declaration
public static void GetChildGameObjects(this GameObject go, List<GameObject> childGameObjects)
Parameters
Type | Name | Description |
---|---|---|
GameObject | go | The parent GameObject that we will want to get the child GameObjects on. |
List<GameObject> | childGameObjects | The direct children of a GameObject. |
GetComponentInActiveScene<T>()
Retrieves the first component of the given type in the active scene
Declaration
public static T GetComponentInActiveScene<T>()
where T : Component
Returns
Type | Description |
---|---|
T | The first component found in the active scene, or null if none exists |
Type Parameters
Name | Description |
---|---|
T | The type of component to retrieve |
GetComponentInScene<T>(Scene)
Retrieves the first component of the given type in a scene
Declaration
public static T GetComponentInScene<T>(Scene scene)
where T : Component
Parameters
Type | Name | Description |
---|---|---|
Scene | scene | The scene to search |
Returns
Type | Description |
---|---|
T | The first component found in the active scene, or null if none exists |
Type Parameters
Name | Description |
---|---|
T | The type of component to retrieve |
GetComponentsInActiveScene<T>(List<T>, Boolean)
Retrieves all components of the given type in the active scene
Declaration
public static void GetComponentsInActiveScene<T>(List<T> components, bool includeInactive = false)
where T : Component
Parameters
Type | Name | Description |
---|---|---|
List<T> | components | List that will be filled out with components retrieved |
Boolean | includeInactive | Should Components on inactive GameObjects be included in the found set? |
Type Parameters
Name | Description |
---|---|
T | The type of components to retrieve |
GetComponentsInAllScenes<T>(List<T>, Boolean)
Retrieves all components of the given type in all loaded scenes
Declaration
public static void GetComponentsInAllScenes<T>(List<T> components, bool includeInactive = false)
where T : Component
Parameters
Type | Name | Description |
---|---|---|
List<T> | components | List that will be filled out with components retrieved |
Boolean | includeInactive | Should Components on inactive GameObjects be included in the found set? |
Type Parameters
Name | Description |
---|---|
T | The type of components to retrieve |
GetComponentsInScene<T>(Scene, List<T>, Boolean)
Retrieves all components of the given type in a scene
Declaration
public static void GetComponentsInScene<T>(Scene scene, List<T> components, bool includeInactive = false)
where T : Component
Parameters
Type | Name | Description |
---|---|---|
Scene | scene | The scene to search |
List<T> | components | List that will be filled out with components retrieved |
Boolean | includeInactive | Should Components on inactive GameObjects be included in the found set? |
Type Parameters
Name | Description |
---|---|
T | The type of components to retrieve |
GetNamedChild(GameObject, String)
Gets a descendant GameObject with a specific name
Declaration
public static GameObject GetNamedChild(this GameObject go, string name)
Parameters
Type | Name | Description |
---|---|---|
GameObject | go | The parent object that is searched for a named child. |
String | name | Name of child to be found. |
Returns
Type | Description |
---|---|
GameObject | The returned child GameObject or null if no child is found. |
Instantiate(GameObject, Transform, Boolean)
Clones the GameObject original
and returns the clone.
This method also calls GameObjectInstantiated.
Declaration
public static GameObject Instantiate(GameObject original, Transform parent = null, bool worldPositionStays = true)
Parameters
Type | Name | Description |
---|---|---|
GameObject | original | An existing GameObject that you want to make a copy of |
Transform | parent | Parent that will be assigned to the new object |
Boolean | worldPositionStays | Whether the new object will stay at its original position, or use it as an offset from its parent |
Returns
Type | Description |
---|---|
GameObject | The instantiated clone |
Instantiate(GameObject, Transform, Vector3, Quaternion)
Clones the GameObject original
and returns the clone.
This method also calls GameObjectInstantiated.
Declaration
public static GameObject Instantiate(GameObject original, Transform parent, Vector3 position, Quaternion rotation)
Parameters
Type | Name | Description |
---|---|---|
GameObject | original | An existing GameObject that you want to make a copy of |
Transform | parent | Parent that will be assigned to the new object |
Vector3 | position | Position for the new object. |
Quaternion | rotation | Orientation of the new object. |
Returns
Type | Description |
---|---|
GameObject | The instantiated clone |
Instantiate(GameObject, Vector3, Quaternion)
Clones the GameObject original
and returns the clone.
This method also calls GameObjectInstantiated.
Declaration
public static GameObject Instantiate(GameObject original, Vector3 position, Quaternion rotation)
Parameters
Type | Name | Description |
---|---|---|
GameObject | original | An existing GameObject that you want to make a copy of |
Vector3 | position | Position for the new object. |
Quaternion | rotation | Orientation of the new object. |
Returns
Type | Description |
---|---|
GameObject | The instantiated clone |
Events
GameObjectInstantiated
Called when a GameObject has been instantiated through GameObjectUtils.Instantiate
Declaration
public static event Action<GameObject> GameObjectInstantiated
Event Type
Type | Description |
---|---|
Action<GameObject> |