Skip to content

RuntimeObjectUtility

Namespace: SideXP.Core

public static class RuntimeObjectUtility

Miscellaneous functions for working with Objects and assets at runtime.

Methods

GetTransform(Object)

public static Transform GetTransform(Object obj)

Gets the Transform component of an object that can be placed in a scene.

Parameters

  • obj: The object from which you want to get the Transform component.

Returns

Returns the found Transform component.

GetTransform(Object, Transform)

public static bool GetTransform(Object obj, out Transform transform)

Gets the Transform component of an object that can be placed in a scene.

Parameters

  • transform: Outputs the found Transform component.
  • obj: The object from which you want to get the Transform component.

Returns

Returns true if the given object has a Transform component.

GetGameObject(Object)

public static GameObject GetGameObject(Object obj)

Gets the GameObject to which the given object is attached.

Parameters

  • obj: The object of which you want to get the attached GameObject.

Returns

Returns the found attached GameObject.

GetGameObject(Object, GameObject)

public static bool GetGameObject(Object obj, out GameObject gameObject)

Gets the GameObject to which the given object is attached.

Parameters

  • gameObject: Outputs the found attached GameObject.
  • obj: The object of which you want to get the attached GameObject.

Returns

Returns true if the given object is attached to a GameObject.

IsPrefab(GameObject)

public static bool IsPrefab(GameObject gameObject)

Checks if a GameObject is a prefab at runtime.

Parameters

  • gameObject: The GameObject to check.

Returns

Returns true if the given object is a prefab.

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)

Finds an Object.

Parameters

  • obj: Outputs the found object.
  • 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 true if an object has been found.

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<T>(object, string, FFindObjectStrategy)

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

Finds an Object.

Type parameters

  • T: The type of the expected object.

Parameters

  • origin: The object from which the expected one is queried.
  • 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<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.

TryOpenAsset(int, int)

public static bool TryOpenAsset(int instanceId, int line)

Invokes registered OpenAssetDelegate handlers to check if one of them can open the asset. WARNING: Since this function resolves the asset by using InstanceIDToObject, it won't do anything outside the editor context.

Parameters

  • instanceId: The instance id of the asset being opened.
  • line: The target line in the file being opened.

Returns

Returns true if the asset can be opened.

Remarks

This is mostly meant to use OnOpenAssetAttribute without having to implement editor code in your assets, and allowing the custom editors to handle this event easily.

AddOpenAssetHandler(OpenAssetDelegate)

public static void AddOpenAssetHandler(OpenAssetDelegate handler)

Registers a handler for an "open asset" operation.

Parameters

  • handler: The handler of this operation.