> For the complete documentation index, see [llms.txt](https://kinematicsoup.gitbook.io/scene-fusion/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://kinematicsoup.gitbook.io/scene-fusion/api-reference/ks.scenefusion.client.unity.editor/ks.scenefusion.client.unity.editor.sfunityutils.md).

# Class sfUnityUtils

Namespace: [KS.SceneFusion.Client.Unity.Editor](/scene-fusion/api-reference/ks.scenefusion.client.unity.editor.md)\
Assembly: KSUnity-Source.dll

Unity utility functions.

```csharp
public static class sfUnityUtils
```

#### Inheritance

object ← [sfUnityUtils](/scene-fusion/api-reference/ks.scenefusion.client.unity.editor/ks.scenefusion.client.unity.editor.sfunityutils.md)

## Fields

### NO\_ID <a href="#ks_scenefusion_client_unity_editor_sfunityutils_no_id" id="ks_scenefusion_client_unity_editor_sfunityutils_no_id"></a>

Zero intance id int constant. In Unity 6.3+, this constant is instead a zero entity id. Use this in functions that take an entity id in 6.3+ and an int instance id in older version.

```csharp
public const int NO_ID = 0
```

#### Field Value

int

## Methods

### AddFlags(GameObject, HideFlags, HashSet\<Component>) <a href="#ks_scenefusion_client_unity_editor_sfunityutils_addflags_unityengine_gameobject_unityengine_hideflag" id="ks_scenefusion_client_unity_editor_sfunityutils_addflags_unityengine_gameobject_unityengine_hideflag"></a>

Adds hide flags to a game object and its components.

```csharp
public static void AddFlags(GameObject gameObject, HideFlags flags, HashSet<Component> alreadyHasFlagSet = null)
```

#### Parameters

`gameObject` GameObject

gameObject to set hide flags on.

`flags` HideFlags

flags to add.

`alreadyHasFlagSet` HashSet\<Component>

if not null, components that already have the flags set will be added to this set.

### FindPrefabStage(string) <a href="#ks_scenefusion_client_unity_editor_sfunityutils_findprefabstage_system_string" id="ks_scenefusion_client_unity_editor_sfunityutils_findprefabstage_system_string"></a>

Checks if there is a prefab stage open for the prefab at the given path and returns the prefab stage if found.

```csharp
public static PrefabStage FindPrefabStage(string prefabPath)
```

#### Parameters

`prefabPath` string

prefabPath to find prefab stage.

#### Returns

PrefabStage

prefab stage for the given prefab path, or null if none was found.

### ForEachDescendant(GameObject, ForEachCallback) <a href="#ks_scenefusion_client_unity_editor_sfunityutils_foreachdescendant_unityengine_gameobject_ks_scenefus" id="ks_scenefusion_client_unity_editor_sfunityutils_foreachdescendant_unityengine_gameobject_ks_scenefus"></a>

Calls a callback on all descendants of a game object.

```csharp
public static void ForEachDescendant(GameObject gameObject, sfUnityUtils.ForEachCallback callback)
```

#### Parameters

`gameObject` GameObject

gameObject with descendants to call the callback on.

`callback` [sfUnityUtils](/scene-fusion/api-reference/ks.scenefusion.client.unity.editor/ks.scenefusion.client.unity.editor.sfunityutils.md).[ForEachCallback](/scene-fusion/api-reference/ks.scenefusion.client.unity.editor/ks.scenefusion.client.unity.editor.sfunityutils/ks.scenefusion.client.unity.editor.sfunityutils.foreachcallback.md)

callback to call. If the callback returns false for an object, the callback will not be called on descendants of that object.

### ForEachGameObject(ForEachCallback) <a href="#ks_scenefusion_client_unity_editor_sfunityutils_foreachgameobject_ks_scenefusion_client_unity_editor" id="ks_scenefusion_client_unity_editor_sfunityutils_foreachgameobject_ks_scenefusion_client_unity_editor"></a>

Calls a callback on all game objects in all loaded scenes.

```csharp
public static void ForEachGameObject(sfUnityUtils.ForEachCallback callback)
```

#### Parameters

`callback` [sfUnityUtils](/scene-fusion/api-reference/ks.scenefusion.client.unity.editor/ks.scenefusion.client.unity.editor.sfunityutils.md).[ForEachCallback](/scene-fusion/api-reference/ks.scenefusion.client.unity.editor/ks.scenefusion.client.unity.editor.sfunityutils/ks.scenefusion.client.unity.editor.sfunityutils.foreachcallback.md)

callback to call. If the callback returns false for an object, the callback will not be called on descendants of that object.

### ForEachInPrefabInstance(GameObject, ForEachCallback) <a href="#ks_scenefusion_client_unity_editor_sfunityutils_foreachinprefabinstance_unityengine_gameobject_ks_sc" id="ks_scenefusion_client_unity_editor_sfunityutils_foreachinprefabinstance_unityengine_gameobject_ks_sc"></a>

Calls a callback on all game objects that are part of the same prefab instance. If the game object is not a prefab instance, the callback is called only on the game object this is called with.

```csharp
public static void ForEachInPrefabInstance(GameObject gameObject, sfUnityUtils.ForEachCallback callback)
```

#### Parameters

`gameObject` GameObject

the callback is called on all game objects belonging to the same prefab instance as this object.

`callback` [sfUnityUtils](/scene-fusion/api-reference/ks.scenefusion.client.unity.editor/ks.scenefusion.client.unity.editor.sfunityutils.md).[ForEachCallback](/scene-fusion/api-reference/ks.scenefusion.client.unity.editor/ks.scenefusion.client.unity.editor.sfunityutils/ks.scenefusion.client.unity.editor.sfunityutils.foreachcallback.md)

callback to call on each game object in the prefab instance. If it returns false for an object, it will not be called on descendants of that object.

### ForSelfAndDescendants(GameObject, ForEachCallback) <a href="#ks_scenefusion_client_unity_editor_sfunityutils_forselfanddescendants_unityengine_gameobject_ks_scen" id="ks_scenefusion_client_unity_editor_sfunityutils_forselfanddescendants_unityengine_gameobject_ks_scen"></a>

Calls a function on a game object and all its descendants.

```csharp
public static void ForSelfAndDescendants(GameObject gameObject, sfUnityUtils.ForEachCallback callback)
```

#### Parameters

`gameObject` GameObject

gameObject to iterate.

`callback` [sfUnityUtils](/scene-fusion/api-reference/ks.scenefusion.client.unity.editor/ks.scenefusion.client.unity.editor.sfunityutils.md).[ForEachCallback](/scene-fusion/api-reference/ks.scenefusion.client.unity.editor/ks.scenefusion.client.unity.editor.sfunityutils/ks.scenefusion.client.unity.editor.sfunityutils.foreachcallback.md)

callback to call on objects. If it returns false for a game object, that object's descendants will not be iterated.

### GetAssetPath(ulong) <a href="#ks_scenefusion_client_unity_editor_sfunityutils_getassetpath_system_uint64" id="ks_scenefusion_client_unity_editor_sfunityutils_getassetpath_system_uint64"></a>

Gets the path of an asset by its id as a ulong. The is is the entity id in Unity 6.3+, and the instance id in older versions.

```csharp
public static string GetAssetPath(ulong uobjectId)
```

#### Parameters

`uobjectId` ulong

Entity or instance id.

#### Returns

string

Asset path, or empty string if none was found.

### GetGameObject(Object) <a href="#ks_scenefusion_client_unity_editor_sfunityutils_getgameobject_unityengine_object" id="ks_scenefusion_client_unity_editor_sfunityutils_getgameobject_unityengine_object"></a>

Attempts to get a game object from a uobject by casting it first to a game object and then to a component and returning the component's game object.

```csharp
public static GameObject GetGameObject(Object uobj)
```

#### Parameters

`uobj` Object

uobj to get game object from.

#### Returns

GameObject

game object, or null if uobj was not a game object or component.

### GetPrefabRootInstanceFromHandle(Object) <a href="#ks_scenefusion_client_unity_editor_sfunityutils_getprefabrootinstancefromhandle_unityengine_object" id="ks_scenefusion_client_unity_editor_sfunityutils_getprefabrootinstancefromhandle_unityengine_object"></a>

Gets the prefab root instance from a prefab instance handle (the object containing the overrides).

```csharp
public static GameObject GetPrefabRootInstanceFromHandle(Object prefabInstanceHandle)
```

#### Parameters

`prefabInstanceHandle` Object

prefabInstanceHandle to get prefab root instance from. This object does not have a C# class so the class is UObject.

#### Returns

GameObject

prefab root instance, or null if it couldn't be found.

### GetUObject\<T>(ulong) <a href="#ks_scenefusion_client_unity_editor_sfunityutils_getuobject__1_system_uint64" id="ks_scenefusion_client_unity_editor_sfunityutils_getuobject__1_system_uint64"></a>

Gets a uobject by its ulong id. In Unity 6.3+ the ulong is converted to an entity id, and in older versions it is converted to an int instance id.

```csharp
public static T GetUObject<T>(ulong id) where T : Object
```

#### Parameters

`id` ulong

Entity or instance id

#### Returns

T

The uobject with the given id, or null if none was found or it was not a type `T`.

#### Type Parameters

`T`

Type to cast uobject to. Returns null if the uobject is a different type.

### GetUObject(ulong) <a href="#ks_scenefusion_client_unity_editor_sfunityutils_getuobject_system_uint64" id="ks_scenefusion_client_unity_editor_sfunityutils_getuobject_system_uint64"></a>

Gets a uobject by its ulong id. In Unity 6.3+ the ulong is converted to an entity id, and in older versions it is converted to an int instance id.

```csharp
public static Object GetUObject(ulong id)
```

#### Parameters

`id` ulong

Entity or instance id

#### Returns

Object

The uobject with the given id, or null if none was found.

### GetUObject\<T>(int) <a href="#ks_scenefusion_client_unity_editor_sfunityutils_getuobject__1_system_int32" id="ks_scenefusion_client_unity_editor_sfunityutils_getuobject__1_system_int32"></a>

Gets a uobject by its instance id. In Unity 6.3+, this function instead takes an entity id.

```csharp
public static T GetUObject<T>(int id) where T : Object
```

#### Parameters

`id` int

Instance id

#### Returns

T

The uobject with the given id, or null if none was found or it was not a type `T`.

#### Type Parameters

`T`

Type to cast uobject to.Returns null if the uobject is a different type.

### GetUObject(int) <a href="#ks_scenefusion_client_unity_editor_sfunityutils_getuobject_system_int32" id="ks_scenefusion_client_unity_editor_sfunityutils_getuobject_system_int32"></a>

Gets a uobject by its instance id. In Unity 6.3+, this function instead takes an entity id.

```csharp
public static Object GetUObject(int id)
```

#### Parameters

`id` int

Instance id

#### Returns

Object

The uobject with the given id, or null if none was found.

### GetUObjectId(Object) <a href="#ks_scenefusion_client_unity_editor_sfunityutils_getuobjectid_unityengine_object" id="ks_scenefusion_client_unity_editor_sfunityutils_getuobjectid_unityengine_object"></a>

Gets a uobject's id as a ulong. In Unity 6.3+ the id is the entity id, and in older versions it is the instance id.

```csharp
public static ulong GetUObjectId(Object uobj)
```

#### Parameters

`uobj` Object

UObject to get id for.

#### Returns

ulong

Entity or instance id.

### GetUnityId(Object) <a href="#ks_scenefusion_client_unity_editor_sfunityutils_getunityid_unityengine_object" id="ks_scenefusion_client_unity_editor_sfunityutils_getunityid_unityengine_object"></a>

Gets a uobject's instance id. In Unity 6.3+ this function returns the entity id instead.

```csharp
public static int GetUnityId(Object uobj)
```

#### Parameters

`uobj` Object

Object to get id for.

#### Returns

int

Instance id

### InstantiatePrefab(Scene, string) <a href="#ks_scenefusion_client_unity_editor_sfunityutils_instantiateprefab_unityengine_scenemanagement_scene" id="ks_scenefusion_client_unity_editor_sfunityutils_instantiateprefab_unityengine_scenemanagement_scene"></a>

Instantiates a prefab.

```csharp
public static GameObject InstantiatePrefab(Scene scene, string prefabPath)
```

#### Parameters

`scene` Scene

scene to instantiate the prefab in.

`prefabPath` string

#### Returns

GameObject

instantiated prefab, or null if the prefab could not be instantiated.

### IsDestroyed(Object) <a href="#ks_scenefusion_client_unity_editor_sfunityutils_isdestroyed_unityengine_object" id="ks_scenefusion_client_unity_editor_sfunityutils_isdestroyed_unityengine_object"></a>

Extension method to check if a UObject is destroyed (is it fake null instead of real null?).

```csharp
public static bool IsDestroyed(this Object uobj)
```

#### Parameters

`uobj` Object

#### Returns

bool

### IsOpenInPrefabStage(GameObject) <a href="#ks_scenefusion_client_unity_editor_sfunityutils_isopeninprefabstage_unityengine_gameobject" id="ks_scenefusion_client_unity_editor_sfunityutils_isopeninprefabstage_unityengine_gameobject"></a>

Checks if a game object is part of a prefab that is open in the current prefab stage.

```csharp
public static bool IsOpenInPrefabStage(GameObject gameObject)
```

#### Parameters

`gameObject` GameObject

gameObject to check if is part of a prefab opened in the current prefab stage.

#### Returns

bool

true if the game object is part of a prefab that is open in the current prefab stage.

### IsPrefabAssetRoot(GameObject) <a href="#ks_scenefusion_client_unity_editor_sfunityutils_isprefabassetroot_unityengine_gameobject" id="ks_scenefusion_client_unity_editor_sfunityutils_isprefabassetroot_unityengine_gameobject"></a>

Checks if a game object is the root of a prefab asset.

```csharp
public static bool IsPrefabAssetRoot(GameObject gameObject)
```

#### Parameters

`gameObject` GameObject

gameObject to check.

#### Returns

bool

true if the game object is the root of a prefab.

### IterateGameObjects() <a href="#ks_scenefusion_client_unity_editor_sfunityutils_iterategameobjects" id="ks_scenefusion_client_unity_editor_sfunityutils_iterategameobjects"></a>

Iterates all game objects in all loaded scenes.

```csharp
public static IEnumerable<GameObject> IterateGameObjects()
```

#### Returns

IEnumerable\<GameObject>

### IterateGameObjects(Scene) <a href="#ks_scenefusion_client_unity_editor_sfunityutils_iterategameobjects_unityengine_scenemanagement_scene" id="ks_scenefusion_client_unity_editor_sfunityutils_iterategameobjects_unityengine_scenemanagement_scene"></a>

Iterates all game objects in a scene if the scene is loaded.

```csharp
public static IEnumerable<GameObject> IterateGameObjects(Scene scene)
```

#### Parameters

`scene` Scene

scene to iterate game objects for.

#### Returns

IEnumerable\<GameObject>

### IterateSelfAndDescendants(GameObject) <a href="#ks_scenefusion_client_unity_editor_sfunityutils_iterateselfanddescendants_unityengine_gameobject" id="ks_scenefusion_client_unity_editor_sfunityutils_iterateselfanddescendants_unityengine_gameobject"></a>

Iterates self and all descendants.

```csharp
public static IEnumerable<GameObject> IterateSelfAndDescendants(GameObject root)
```

#### Parameters

`root` GameObject

#### Returns

IEnumerable\<GameObject>

### RemoveFlags(GameObject, HideFlags, HashSet\<Component>) <a href="#ks_scenefusion_client_unity_editor_sfunityutils_removeflags_unityengine_gameobject_unityengine_hidef" id="ks_scenefusion_client_unity_editor_sfunityutils_removeflags_unityengine_gameobject_unityengine_hidef"></a>

Removes hide flags from a game object and its components.

```csharp
public static void RemoveFlags(GameObject gameObject, HideFlags flags, HashSet<Component> blacklist = null)
```

#### Parameters

`gameObject` GameObject

gameObject to clear hide flags on.

`flags` HideFlags

flags to remove.

`blacklist` HashSet\<Component>

if not null, components in this set will not have the flag removed.

### WithActiveScene(Scene, WithActiveSceneCallback) <a href="#ks_scenefusion_client_unity_editor_sfunityutils_withactivescene_unityengine_scenemanagement_scene_ks" id="ks_scenefusion_client_unity_editor_sfunityutils_withactivescene_unityengine_scenemanagement_scene_ks"></a>

Sets the active scene to the given scene if it isn't the active scene before calling the callback, then restores the active scene to what it was before. The callback is not called if the scene is not loaded.

```csharp
public static void WithActiveScene(Scene scene, sfUnityUtils.WithActiveSceneCallback callback)
```

#### Parameters

`scene` Scene

scene to set as the active scene while calling the callback.

`callback` [sfUnityUtils](/scene-fusion/api-reference/ks.scenefusion.client.unity.editor/ks.scenefusion.client.unity.editor.sfunityutils.md).[WithActiveSceneCallback](/scene-fusion/api-reference/ks.scenefusion.client.unity.editor/ks.scenefusion.client.unity.editor.sfunityutils/ks.scenefusion.client.unity.editor.sfunityutils.withactivescenecallback.md)

callback to call.
