Skip to content

ObjectUtility

Namespace: SideXP.Core.EditorOnly

public static class ObjectUtility

Miscellaneous utility functions for working with Object instances and assets.

Methods

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.

Parameters

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

Returns

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

FindAssets(string, Type, bool)

public static Object[] FindAssets(string name, Type type, bool querySubassets = false)

Finds all the assets in the project that match the given name and type.

Parameters

  • name: The expected name of the assets. Words separated by spaces are considered as separate filters. These are used as search strings, so typing just "image" will query all assets that include this word in their name.
  • type: The expected type of the assets.
  • querySubassets: If enabled, this function will also search in the assets attached to main ones.

Returns

Returns all the found assets that match the given informations.

See also

  • FindAssets

FindAssets<T>(string, bool)

public static T[] FindAssets<T>(string name, bool querySubassets = false)

Finds all the assets in the project that match the given name and type.

Type parameters

  • T: The expected type of the assets.

Parameters

  • name: The expected name of the assets. Words separated by spaces are considered as separate filters. These are used as search strings, so typing just "image" will query all assets that include this word in their name.
  • querySubassets: If enabled, this function will also search in the assets attached to main ones.

Returns

Returns all the found assets that match the given informations.

See also

  • FindAssets

FindAssets(Type, bool)

public static Object[] FindAssets(Type type, bool querySubassets = false)

Finds all the assets in the project that match the given name and type.

Parameters

  • type: The expected type of the assets.
  • querySubassets: If enabled, this function will also search in the assets attached to main ones.

Returns

Returns all the found assets that match the given informations.

See also

  • FindAssets

FindAssets<T>(bool)

public static T[] FindAssets<T>(bool querySubassets = false)

Type parameters

  • T: The expected type of the assets.

FindAssets(string, bool)

public static Object[] FindAssets(string name, bool querySubassets = false)

Finds all the assets in the project that match the given name and type.

Parameters

  • name: The expected name of the assets. Words separated by spaces are considered as separate filters. These are used as search strings, so typing just "image" will query all assets that include this word in their name.
  • querySubassets: If enabled, this function will also search in the assets attached to main ones.

Returns

Returns all the found assets that match the given informations.

See also

  • FindAssets

FindAsset(string, Type, bool)

public static Object FindAsset(string name, Type type, bool querySubassets = false)

Find an asset in the project that match the given name and type.

Parameters

  • name: The expected name of the asset. Words separated by spaces are considered as separate filters. These are used as search strings, so typing just "image" will query all assets that include this word in their name.
  • type: The expected type of the asset.
  • querySubassets: If enabled, this function will also search in the assets attached to main ones.

Returns

Returns the first found asset that match the given filters.

See also

  • FindAssets

FindAsset<T>(string, bool)

public static T FindAsset<T>(string name, bool querySubassets = false)

Find an asset in the project that match the given name and type.

Type parameters

  • T: The expected type of the asset.

Parameters

  • name: The expected name of the asset. Words separated by spaces are considered as separate filters. These are used as search strings, so typing just "image" will query all assets that include this word in their name.

Returns

Returns the first found asset that match the given filters.

FindAsset(Type, bool)

public static Object FindAsset(Type type, bool querySubassets = false)

Find an asset in the project that match the given name and type.

Parameters

  • type: The expected type of the asset.

Returns

Returns the first found asset that match the given filters.

FindAsset<T>(bool)

public static T FindAsset<T>(bool querySubassets = false)

Type parameters

  • T: The expected type of the asset.

FindAsset(string, bool)

public static Object FindAsset(string name, bool querySubassets = false)

Find an asset in the project that match the given name and type.

Parameters

  • name: The expected name of the asset. Words separated by spaces are considered as separate filters. These are used as search strings, so typing just "image" will query all assets that include this word in their name.

Returns

Returns the first found asset that match the given filters.

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)

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

Type parameters

  • T: The expected type of the subasset.

Parameters

  • asset: The main asset of which you want to get 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.

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)

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

Type parameters

  • T: The expected type of the subassets.

Parameters

  • asset: The main asset of which you want to get 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.

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)

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

Parameters

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

Returns

Returns true if an asset has been found.

FindAssetInHierarchy<T>(Object)

public static T FindAssetInHierarchy<T>(Object asset)

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

Type parameters

  • T: The type of the asset to find.

Parameters

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

Returns

Returns the found asset.

FindAssetInHierarchy<T>(Object, T)

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

Parameters

  • foundAsset: Outputs the found asset.

Returns

Returns true if an asset has been found.

FindObjectFrom(object, Type, string, FFindObjectStrategy)

public static Object FindObjectFrom(object origin, Type type, string name = null, FFindObjectStrategy strategy = None)

Finds an Object.

Parameters

  • origin: The object from which the expected one is queried.
  • type: The type of the expected object.
  • name: If provided, this function will consider only the objects with that exact name.
  • strategy: The strategy to use to find the expected object.

Returns

Returns the found object.

Remarks

By default the strategy None is used. This means the reference is queried using the following rules. If this attribute is used inside a MonoBehaviour script: - If the decorated property is of type GameObject or Component, the reference will be queried in the following order: this game object, its direct children, its direct parent, or the whole scene. - Else, in any other case, the reference will be queried from the whole scene. If this attribute is used inside a ScriptableObject script, the reference will be queried from subassets, or the loaded resources. This default behavior is equivalent as using the value DirectHierarchy|Scene. Special case: if type is GameObject or Component and this function tries to find the object in the scene (using Scene, the name is in fact the expected tag name of the objects. This is counter-intuitive, but using tags in Unity is the expected way to "group" game objects, instead of naming them the same.

FindObjectFrom(object, Type, Object, string, FFindObjectStrategy)

public static bool FindObjectFrom(object origin, Type type, out Object obj, string name = null, FFindObjectStrategy strategy = None)

Parameters

  • obj: Outputs the found object.

Returns

Returns true if an object has been found.

FindObjectFrom<T>(object, string, FFindObjectStrategy)

public static T FindObjectFrom<T>(object origin, string name = null, FFindObjectStrategy strategy = None)

Type parameters

  • T: The type of the expected object.

FindObjectFrom<T>(object, T, string, FFindObjectStrategy)

public static bool FindObjectFrom<T>(object origin, out T obj, string name = null, FFindObjectStrategy strategy = None)

Parameters

  • obj: Outputs the found object.

Returns

Returns true if an object has been found.

CreateSubasset(Type, Object, string)

public static ScriptableObject CreateSubasset(Type subAssetType, Object asset, 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(Type, Object, ScriptableObject, string)

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

Creates and attaches a subasset to an existing asset.

Parameters

  • subAsset: Outputs the created subasset.
  • 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 true if the subasset has been created and attached successfully.

CreateSubasset<TSubasset>(Object, string)

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

Creates and attaches a subasset to an existing asset.

Type parameters

  • TSubasset: The type of the subasset to create.

Parameters

  • 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<TSubasset>(Object, TSubasset, string)

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

Parameters

  • subAsset: Outputs the created subasset.

Returns

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

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.
  • obj: The object you want to check.

Returns

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

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.

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.

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.

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.
  • 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.

Remarks

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