Skip to content

ObjectExtensions

Namespace: SideXP.Core.EditorOnly

public static class ObjectExtensions

Miscellaneous extension functions for Object and assets.

Methods

GetGUID(Object)

public static string GetGUID(Object obj)

Gets the GUID string of the given Object. Note that GUIDs are assigned only to assets, not to scene objects. So if this Object is a scene object or an object loaded only in memory, this function will return null.

Parameters

  • obj: The Object whose GUID you want to get.

Returns

Returns the GUID string of this Object, or null if this Object is not an asset (meaning it's a scene object or an object loaded only in memory).

IsAsset(Object)

public static bool IsAsset(Object obj)

Checks if the given Object is an asset (and not a scene object).

Parameters

  • obj: The Object you want to check.

Returns

Returns true if the given Object is an asset.

GetUniqueAssetName(Object, string)

public static string GetUniqueAssetName(Object asset, string expectedName)

Gets the unique name of an asset to create in the same directory than the given one.

Parameters

  • asset: The asset you want to process.
  • expectedName: The name you want to set on the asset.

Returns

Returns the processed unique asset name.

GetAssetPath(Object)

public static string GetAssetPath(Object asset)

Gets the relative path to the given asset from the root directory of the current Unity project.

Parameters

  • asset: The asset of which you want to get the path.

Returns

Returns the found relative asset path, or null if the given asset is not an asset.

GetAbsoluteAssetPath(Object)

public static string GetAbsoluteAssetPath(Object asset)

Gets the absolute path to the given asset.

Returns

Returns the found absolute asset path, or null if the asset is not an asset.

FindSubassetOfType(Object, Type, bool)

public static Object FindSubassetOfType(Object asset, Type type, bool includeMainAsset = false)

Gets the first subasset of the given type in the given main asset.

Parameters

  • asset: The main asset of which you want to get the subasset.
  • type: The expected type of the subasset.
  • includeMainAsset: If enabled, if the main asset has the expected type, it's the one being returned.

Returns

Returns the found subasset with the expected type.

FindSubassetOfType<T>(Object, bool)

public static T FindSubassetOfType<T>(Object asset, bool includeMainAsset = false)

Type parameters

  • T: The expected type of the subasset.

FindAllSubassetsOfType(Object, Type, bool)

public static Object[] FindAllSubassetsOfType(Object asset, Type type, bool includeMainAsset = false)

Gets the subassets of the given type in the given main asset.

Parameters

  • asset: The main asset of which you want to get the subassets.
  • type: The expected type of the subassets.
  • includeMainAsset: If enabled, if the main asset has the expected type, it's included in the returned array.

Returns

Returns the found subassets with the expected type.

FindAllSubassetsOfType<T>(Object, bool)

public static T[] FindAllSubassetsOfType<T>(Object asset, bool includeMainAsset = false)

Type parameters

  • T: The expected type of the subassets.

GetMainAsset(Object)

public static Object GetMainAsset(Object asset)

Gets the main asset of a given one.

Parameters

  • asset: The asset from which you want to get the main assset.

Returns

Returns the found main asset, or the given asset itself if it's not a subasset.

FindAssetInHierarchy(Object, Type)

public static Object FindAssetInHierarchy(Object asset, Type assetType)

Finds an asset of a given type in the hierarchy of a given asset, whether it's the main asset or a subasset.

Parameters

  • asset: The asset of which you want to inspect the hierarchy.
  • assetType: The type of the asset to find.

Returns

Returns the found asset.

FindAssetInHierarchy(Object, Type, Object)

public static bool FindAssetInHierarchy(Object asset, Type assetType, out Object foundAsset)

Parameters

  • foundAsset: Outputs the found asset.

Returns

Returns true if an asset has been found.

FindAssetInHierarchy<T>(Object)

public static T FindAssetInHierarchy<T>(Object asset)

Type parameters

  • T: The type of the asset to find.

FindAssetInHierarchy<T>(Object, T)

public static bool FindAssetInHierarchy<T>(Object asset, out T foundAsset)

Inherited documentation.

AttachObject(Object, Object, bool)

public static bool AttachObject(Object asset, Object objectToAttach, bool skipReimport = false)

Attaches an object to an existing asset, and perform the appropriate operations to avoid inconsistent states and refresh views accordingly.

Parameters

  • asset: The asset to which you want to attach the object.
  • objectToAttach: The object to attach to the asset. Note that this object must not be an asset of the project, only an instance yet not serialized.
  • skipReimport: By default, the modified asset is reimported to save changes on disk. If disabled, you must save and reimport the asset by yourself.

Returns

Returns true if the object has been attached successfully to the asset.

CreateSubasset(Object, Type, string)

public static ScriptableObject CreateSubasset(Object asset, Type subAssetType, string name = null)

Creates and attaches a subasset to an existing asset.

Parameters

  • subAssetType: The type of the subasset to create.
  • asset: The asset to which the created subasset will be attached.
  • name: The name of the subasset to create. If undefined, uses New[subasset type name].

Returns

Returns the created subasset.

CreateSubasset(Object, Type, ScriptableObject, string)

public static bool CreateSubasset(Object asset, Type subAssetType, out ScriptableObject subAsset, string name = null)

Parameters

  • subAsset: Outputs the created subasset.

Returns

Returns true if the subasset has been created and attached successfully.

CreateSubasset<TSubasset>(Object, string)

public static TSubasset CreateSubasset<TSubasset>(Object asset, string name = null)

Type parameters

  • TSubasset: The type of the subasset to create.

CreateSubasset<TSubasset>(Object, TSubasset, string)

public static bool CreateSubasset<TSubasset>(Object asset, out TSubasset subAsset, string name = null)

Inherited documentation.

IsSubasset(Object)

public static bool IsSubasset(Object obj)

Checks if a given object is a sub-asset.

Parameters

  • obj: The object you want to check.

Returns

Returns true if the given object is a sub-asset.

IsSubassetOf(Object, Object)

public static bool IsSubassetOf(Object obj, Object container)

Checks if a given object is a sub-asset of a given asset.

Parameters

  • container: The expected main asset.

Returns

Returns true if the given object is a sub-asset of the given container.

SaveAndReimport(Object)

public static bool SaveAndReimport(Object asset)

Reimports the given asset. You should call this function after adding, updating or removing a subasset.

Parameters

  • asset: The asset you want to save and reimport.

Returns

Returns true if the given object is truly an asset in the project.

Rename(Object, string, bool)

public static void Rename(Object obj, string name, bool skipReimport = false)

Renames a given asset.

Parameters

  • obj: The asset to rename. If the given object is not an asset saved on disk, this function still renames it but it won't save it on disk.
  • name: The new name of the asset.

Remarks

This function also works fine with subassets, unlike RenameAsset, which only works with main assets.