Guides
Guides

VRTK_SharedMethods

Overview

The Shared Methods script is a collection of reusable static methods that are used across a range of different scripts.

Class Methods

GetBounds/3

public static Bounds GetBounds(Transform transform, Transform excludeRotation = null, Transform excludeTransform = null)

  • Parameters
    • Transform transform -
    • Transform excludeRotation - Resets the rotation of the transform temporarily to 0 to eliminate skewed bounds.
    • Transform excludeTransform - Does not consider the stated object when calculating the bounds.
  • Returns
    • Bounds - The bounds of the transform.

The GetBounds methods returns the bounds of the transform including all children in world space.

IsLowest/2

public static bool IsLowest(float value, float[] others)

  • Parameters
    • float value - The value to check to see if it is lowest.
    • float[] others - The array of values to check against.
  • Returns
    • bool - Returns true if the value is lower than all numbers in the given array, returns false if it is not the lowest.

The IsLowest method checks to see if the given value is the lowest number in the given array of values.

AddCameraFade/0

public static Transform AddCameraFade()

  • Parameters
    • none
  • Returns
    • Transform - The transform of the headset camera.

The AddCameraFade method finds the headset camera and adds a headset fade script to it.

CreateColliders/1

public static void CreateColliders(GameObject obj)

  • Parameters
    • GameObject obj - The game object to attempt to add the colliders to.
  • Returns
    • none

The CreateColliders method attempts to add box colliders to all child objects in the given object that have a renderer but no collider.

ColliderExclude/2

public static Collider[] ColliderExclude(Collider[] setA, Collider[] setB)

  • Parameters
    • Collider[] setA - The array that contains all of the relevant colliders.
    • Collider[] setB - The array that contains the colliders to remove from setA.
  • Returns
    • Collider[] - A Collider array that is a subset of setA that doesn't contain the colliders from setB.

The ColliderExclude method reduces the colliders in the setA array by those matched in the setB array.

GetCollidersInGameObjects/3

public static Collider[] GetCollidersInGameObjects(GameObject[] gameObjects, bool searchChildren, bool includeInactive)

  • Parameters
    • GameObject[] gameObjects - An array of GameObjects to get the colliders for.
    • bool searchChildren - If this is true then the given GameObjects will also have their child GameObjects searched for colliders.
    • bool includeInactive - If this is true then the inactive GameObjects in the array will also be checked for Colliders. Only relevant if searchChildren is true.
  • Returns
    • Collider[] - An array of Colliders that are found in the given GameObject array.

The GetCollidersInGameObjects method iterates through a GameObject array and returns all of the unique found colliders for all GameObejcts.

CloneComponent/3

public static Component CloneComponent(Component source, GameObject destination, bool copyProperties = false)

  • Parameters
    • Component source - The component to copy.
    • GameObject destination - The game object to copy the component to.
    • bool copyProperties - Determines whether the properties of the component as well as the fields should be copied.
  • Returns
    • Component - The component that has been cloned onto the given game object.

The CloneComponent method takes a source component and copies it to the given destination game object.

ColorDarken/2

public static Color ColorDarken(Color color, float percent)

  • Parameters
    • Color color - The source colour to apply the darken to.
    • float percent - The percent to darken the colour by.
  • Returns
    • Color - The new colour with the darken applied.

The ColorDarken method takes a given colour and darkens it by the given percentage.

RoundFloat/3

public static float RoundFloat(float givenFloat, int decimalPlaces, bool rawFidelity = false)

  • Parameters
    • float givenFloat - The float to round.
    • int decimalPlaces - The number of decimal places to round to.
    • bool rawFidelity - If this is true then the decimal places must be given in the decimal multiplier, e.g. 10 for 1dp, 100 for 2dp, etc.
  • Returns
    • float - The rounded float.

The RoundFloat method is used to round a given float to the given decimal places.

IsEditTime/0

public static bool IsEditTime()

  • Parameters
    • none
  • Returns
    • bool - Returns true if Unity is in the Unity Editor and not in play mode.

The IsEditTime method determines if the state of Unity is in the Unity Editor and the scene is not in play mode.

Mod/2

public static float Mod(float a, float b)

  • Parameters
    • float a - The dividend value.
    • float b - The divisor value.
  • Returns
    • float - The remainder value.

The Mod method is used to find the remainder of the sum a/b.

FindEvenInactiveGameObject/2

public static GameObject FindEvenInactiveGameObject<T>(string gameObjectName = null, bool searchAllScenes = false) where T : Component

  • Type Params
    • T - The component type that needs to be on an ancestor of the wanted GameObject. Must be a subclass of Component.
  • Parameters
    • string gameObjectName - The name of the wanted GameObject. If it contains a '/' character, this method traverses the hierarchy like a path name, beginning on the game object that has a component of type T.
    • bool searchAllScenes - If this is true, all loaded scenes will be searched. If this is false, only the active scene will be searched.
  • Returns
    • GameObject - The GameObject with name gameObjectName and an ancestor that has a T. If no such GameObject is found then null is returned.

Finds the first GameObject with a given name and an ancestor that has a specific component. This method returns active as well as inactive GameObjects in all scenes. It doesn't return assets. For performance reasons it is recommended to not use this function every frame. Cache the result in a member variable at startup instead.

FindEvenInactiveComponents/1

public static T[] FindEvenInactiveComponents<T>(bool searchAllScenes = false) where T : Component

  • Type Params
    • T - The component type to search for. Must be a subclass of Component.
  • Parameters
    • bool searchAllScenes - If this is true, all loaded scenes will be searched. If this is false, only the active scene will be searched.
  • Returns
    • T[] - All the found components. If no component is found an empty array is returned.

Finds all components of a given type. This method returns components from active as well as inactive GameObjects in all scenes. It doesn't return assets. For performance reasons it is recommended to not use this function every frame. Cache the result in a member variable at startup instead.

FindEvenInactiveComponent/1

public static T FindEvenInactiveComponent<T>(bool searchAllScenes = false) where T : Component

  • Type Params
    • T - The component type to search for. Must be a subclass of Component.
  • Parameters
    • bool searchAllScenes - If this is true, all loaded scenes will be searched. If this is false, only the active scene will be searched.
  • Returns
    • T - The found component. If no component is found null is returned.

Finds the first component of a given type. This method returns components from active as well as inactive GameObjects in all scenes. It doesn't return assets. For performance reasons it is recommended to not use this function every frame. Cache the result in a member variable at startup instead.

GenerateVRTKObjectName/2

public static string GenerateVRTKObjectName(bool autoGen, params object[] replacements)

  • Parameters
    • bool autoGen - An additiona [AUTOGEN] prefix will be added if this is true.
    • params object[] replacements - A collection of parameters to add to the generated name.
  • Returns
    • string - The generated name string.

The GenerateVRTKObjectName method is used to create a standard name string for any VRTK generated object.

GetGPUTimeLastFrame/0

public static float GetGPUTimeLastFrame()

  • Parameters
    • none
  • Returns
    • float - The total GPU time utilized last frame as measured by the VR subsystem.

The GetGPUTimeLastFrame retrieves the time spent by the GPU last frame, in seconds, as reported by the VR SDK.

Vector2ShallowCompare/3

public static bool Vector2ShallowCompare(Vector2 vectorA, Vector2 vectorB, int compareFidelity)

  • Parameters
    • Vector2 vectorA - The Vector2 to compare against.
    • Vector2 vectorB - The Vector2 to compare with
    • int compareFidelity - The number of decimal places to use when doing the comparison on the float elements within the Vector2.
  • Returns
    • bool - Returns true if the given Vector2 objects match based on the given fidelity.

The Vector2ShallowCompare method compares two given Vector2 objects based on the given fidelity, which is the equivalent of comparing rounded Vector2 elements to determine if the Vector2 elements are equal.

Vector3ShallowCompare/3

public static bool Vector3ShallowCompare(Vector3 vectorA, Vector3 vectorB, float threshold)

  • Parameters
    • Vector3 vectorA - The Vector3 to compare against.
    • Vector3 vectorB - The Vector3 to compare with
    • float threshold - The distance in which the two Vector3 objects can be within to be considered true
  • Returns
    • bool - Returns true if the given Vector3 objects are within the given threshold distance.

The Vector3ShallowCompare method compares two given Vector3 objects based on the given threshold, which is the equavelent of checking the distance between two Vector3 objects are above the threshold distance.

NumberPercent/2

public static float NumberPercent(float value, float percent)

  • Parameters
    • float value - The value to determine the percentage from
    • float percent - The percentage to find within the given value.
  • Returns
    • float - A float containing the percentage value based on the given input.

The NumberPercent method is used to determine the percentage of a given value.

SetGlobalScale/2

public static void SetGlobalScale(this Transform transform, Vector3 globalScale)

  • Parameters
    • this Transform transform - The reference to the transform to scale.
    • Vector3 globalScale - A Vector3 of a global scale to apply to the given transform.
  • Returns
    • none

The SetGlobalScale method is used to set a transform scale based on a global scale instead of a local scale.

VectorHeading/2

public static Vector3 VectorHeading(Vector3 originPosition, Vector3 targetPosition)

  • Parameters
    • Vector3 originPosition - The point to use as the originating position for the heading calculation.
    • Vector3 targetPosition - The point to use as the target position for the heading calculation.
  • Returns
    • Vector3 - A Vector3 containing the heading changes of the target position in relation to the origin position.

The VectorHeading method calculates the current heading of the target position in relation to the origin position.

VectorDirection/2

public static Vector3 VectorDirection(Vector3 originPosition, Vector3 targetPosition)

  • Parameters
    • Vector3 originPosition - The point to use as the originating position for the direction calculation.
    • Vector3 targetPosition - The point to use as the target position for the direction calculation.
  • Returns
    • Vector3 - A Vector3 containing the direction of the target position in relation to the origin position.

The VectorDirection method calculates the direction the target position is in relation to the origin position.

DividerToMultiplier/1

public static float DividerToMultiplier(float value)

  • Parameters
    • float value - The number to convert into the multplier value.
  • Returns
    • float - The calculated number that can replace the divider number in a multiplication sum.

The DividerToMultiplier method takes a number to be used in a division and converts it to be used for multiplication. (e.g. 2 / 2 becomes 2 * 0.5)

NormalizeValue/4

public static float NormalizeValue(float value, float minValue, float maxValue, float threshold = 0f)

  • Parameters
    • float value - The actual value to normalize.
    • float minValue - The minimum value the actual value can be.
    • float maxValue - The maximum value the actual value can be.
    • float threshold - The threshold to force to the minimum or maximum value if the normalized value is within the threhold limits.
  • Returns
    • float -

The NormalizeValue method takes a given value between a specified range and returns the normalized value between 0f and 1f.

AxisDirection/2

public static Vector3 AxisDirection(int axisIndex, Transform givenTransform = null)

  • Parameters
    • int axisIndex - The axis index of the axis. 0 = x 1 = y 2 = z
    • Transform givenTransform - An optional Transform to get the Axis Direction for. If this is null then the World directions will be used.
  • Returns
    • Vector3 - The direction Vector3 based on the given axis index.

The AxisDirection method returns the relevant direction Vector3 based on the axis index in relation to x,y,z.

AddListValue/3

public static bool AddListValue<TValue>(List<TValue> list, TValue value, bool preventDuplicates = false)

  • Type Params
    • TValue - The datatype for the list value.
  • Parameters
    • List<TValue> list - The list to retrieve the value from.
    • TValue value - The value to attempt to add to the list.
    • bool preventDuplicates - If this is false then the value provided will always be appended to the list. If this is true the value provided will only be added to the list if it doesn't already exist.
  • Returns
    • bool - Returns true if the given value was successfully added to the list. Returns false if the given value already existed in the list and preventDuplicates is true.

The AddListValue method adds the given value to the given list. If preventDuplicates is true then the given value will only be added if it doesn't already exist in the given list.

GetDictionaryValue<TKey, TValue>/4

public static TValue GetDictionaryValue<TKey, TValue>(Dictionary<TKey, TValue> dictionary, TKey key, TValue defaultValue = default(TValue), bool setMissingKey = false)

  • Type Params
    • TKey - The datatype for the dictionary key.
    • TValue - The datatype for the dictionary value.
  • Parameters
    • Dictionary<TKey, TValue> dictionary - The dictionary to retrieve the value from.
    • TKey key - The key to retrieve the value for.
    • TValue defaultValue - The value to utilise when either setting the missing key (if setMissingKey is true) or the default value to return when no key is found (if setMissingKey is false).
    • bool setMissingKey - If this is true and the given key is not present, then the dictionary value for the given key will be set to the defaultValue parameter. If this is false and the given key is not present then the defaultValue parameter will be returned as the value.
  • Returns
    • TValue - The found value for the given key in the given dictionary, or the default value if no key is found.

The GetDictionaryValue method attempts to retrieve a value from a given dictionary for the given key. It removes the need for a double dictionary lookup to ensure the key is valid and has the option of also setting the missing key value to ensure the dictionary entry is valid.

GetDictionaryValue<TKey, TValue>/5

public static TValue GetDictionaryValue<TKey, TValue>(Dictionary<TKey, TValue> dictionary, TKey key, out bool keyExists, TValue defaultValue = default(TValue), bool setMissingKey = false)

  • Type Params
    • TKey - The datatype for the dictionary key.
    • TValue - The datatype for the dictionary value.
  • Parameters
    • Dictionary<TKey, TValue> dictionary - The dictionary to retrieve the value from.
    • TKey key - The key to retrieve the value for.
    • out bool keyExists - Sets the given parameter to true if the key exists in the given dictionary or sets to false if the key didn't existing in the given dictionary.
    • TValue defaultValue - The value to utilise when either setting the missing key (if setMissingKey is true) or the default value to return when no key is found (if setMissingKey is false).
    • bool setMissingKey - If this is true and the given key is not present, then the dictionary value for the given key will be set to the defaultValue parameter. If this is false and the given key is not present then the defaultValue parameter will be returned as the value.
  • Returns
    • TValue - The found value for the given key in the given dictionary, or the default value if no key is found.

The GetDictionaryValue method attempts to retrieve a value from a given dictionary for the given key. It removes the need for a double dictionary lookup to ensure the key is valid and has the option of also setting the missing key value to ensure the dictionary entry is valid.

AddDictionaryValue<TKey, TValue>/4

public static bool AddDictionaryValue<TKey, TValue>(Dictionary<TKey, TValue> dictionary, TKey key, TValue value, bool overwriteExisting = false)

  • Type Params
    • TKey - The datatype for the dictionary key.
    • TValue - The datatype for the dictionary value.
  • Parameters
    • Dictionary<TKey, TValue> dictionary - The dictionary to set the value for.
    • TKey key - The key to set the value for.
    • TValue value - The value to set at the given key in the given dictionary.
    • bool overwriteExisting - If this is true then the value for the given key will always be set to the provided value. If this is false then the value for the given key will only be set if the given key is not found in the given dictionary.
  • Returns
    • bool - Returns true if the given value was successfully added to the dictionary at the given key. Returns false if the given key already existed in the dictionary and overwriteExisting is false.

The AddDictionaryValue method attempts to add a value for the given key in the given dictionary if the key does not already exist. If overwriteExisting is true then it always set the value even if they key exists.

GetTypeUnknownAssembly/1

public static Type GetTypeUnknownAssembly(string typeName)

  • Parameters
    • string typeName - The name of the type to get.
  • Returns
    • Type - The Type, or null if none is found.

The GetTypeUnknownAssembly method is used to find a Type without knowing the exact assembly it is in.

GetEyeTextureResolutionScale/0

public static float GetEyeTextureResolutionScale()

  • Parameters
    • none
  • Returns
    • float - Returns a float with the render scale for the resolution.

The GetEyeTextureResolutionScale method returns the render scale for the resolution.

SetEyeTextureResolutionScale/1

public static void SetEyeTextureResolutionScale(float value)

  • Parameters
    • float value - The value to set the render scale to.
  • Returns
    • none

The SetEyeTextureResolutionScale method sets the render scale for the resolution.

IsTypeSubclassOf/2

public static bool IsTypeSubclassOf(Type givenType, Type givenBaseType)

  • Parameters
    • Type givenType - The Type to check.
    • Type givenBaseType - The base Type to check.
  • Returns
    • bool - Returns true if the given type is a subclass of the given base type.

The IsTypeSubclassOf checks if a given Type is a subclass of another given Type.

GetTypeCustomAttributes/3

public static object[] GetTypeCustomAttributes(Type givenType, Type attributeType, bool inherit)

  • Parameters
    • Type givenType - The type to get the custom attributes for.
    • Type attributeType - The attribute type.
    • bool inherit - Whether to inherit attributes.
  • Returns
    • object[] - Returns an object array of custom attributes.

The GetTypeCustomAttributes method gets the custom attributes of a given type.

GetBaseType/1

public static Type GetBaseType(Type givenType)

  • Parameters
    • Type givenType - The type to return the base Type for.
  • Returns
    • Type - Returns the base Type.

The GetBaseType method returns the base Type for the given Type.

IsTypeAssignableFrom/2

public static bool IsTypeAssignableFrom(Type givenType, Type sourceType)

  • Parameters
    • Type givenType - The Type to check on.
    • Type sourceType - The Type to check if the given Type is assignable from.
  • Returns
    • bool - Returns true if the given Type is assignable from the source Type.

The IsTypeAssignableFrom method determines if the given Type is assignable from the source Type.

GetNestedType/2

public static Type GetNestedType(Type givenType, string name)

  • Parameters
    • Type givenType - The Type to check on.
    • string name - The name of the nested Type.
  • Returns
    • Type - Returns the nested Type.

The GetNestedType method returns the nested Type of the given Type.

GetPropertyFirstName/0

public static string GetPropertyFirstName<T>()

  • Type Params
    • T - The type to check the first property on.
  • Parameters
    • none
  • Returns
    • string - Returns a string representation of the first property name for the given Type.

The GetPropertyFirstName method returns the string name of the first property on a given Type.

GetCommandLineArguements/0

public static string[] GetCommandLineArguements()

  • Parameters
    • none
  • Returns
    • string[] - Returns an array of command line arguements as strings.

The GetCommandLineArguements method returns the command line arguements for the environment.

GetTypesOfType/1

public static Type[] GetTypesOfType(Type givenType)

  • Parameters
    • Type givenType - The Type to check on.
  • Returns
    • Type[] - An array of Types found.

The GetTypesOfType method returns an array of Types for the given Type.

GetExportedTypesOfType/1

public static Type[] GetExportedTypesOfType(Type givenType)

  • Parameters
    • Type givenType - The Type to check on.
  • Returns
    • Type[] - An array of Exported Types found.

The GetExportedTypesOfType method returns an array of Exported Types for the given Type.

IsTypeAbstract/1

public static bool IsTypeAbstract(Type givenType)

  • Parameters
    • Type givenType - The Type to check on.
  • Returns
    • bool - Returns true if the given type is abstract.

The IsTypeAbstract method determines if a given Type is abstract.