//==================================================================================== // // Purpose: Provide a way of tagging game objects as player specific objects to // allow other scripts to identify these specific objects without needing to use tags // or without needing to append the name of the game object. // //==================================================================================== namespace VRTK { using UnityEngine; public sealed class VRTK_PlayerObject : MonoBehaviour { /// /// The type of object associated to the player. /// public enum ObjectTypes { /// /// No defined object. /// Null, /// /// The object that represents the VR camera rig. /// CameraRig, /// /// The object that represents the VR headset. /// Headset, /// /// An object that represents a VR controller. /// Controller, /// /// An object that represents a player generated pointer. /// Pointer, /// /// An object that represents a player generated highlighter. /// Highlighter, /// /// An object that represents a player collider. /// Collider } public ObjectTypes objectType; /// /// The SetPlayerObject method tags the given game object with a special player object class for easier identification. /// /// The game object to add the player object class to. /// The type of player object that is to be assigned. public static void SetPlayerObject(GameObject obj, ObjectTypes objType) { VRTK_PlayerObject currentPlayerObject = obj.GetComponent(); if (currentPlayerObject == null) { currentPlayerObject = obj.AddComponent(); } currentPlayerObject.objectType = objType; } /// /// The IsPlayerObject method determines if the given game object is a player object and can also check if it's of a specific type. /// /// The GameObjet to check if it's a player object. /// An optional ObjectType to check if the given GameObject is of a specific player object. /// Returns true if the object is a player object with the optional given type. public static bool IsPlayerObject(GameObject obj, ObjectTypes ofType = ObjectTypes.Null) { VRTK_PlayerObject[] playerObjects = obj.GetComponentsInParent(true); for (int i = 0; i < playerObjects.Length; i++) { if (ofType == ObjectTypes.Null || ofType == playerObjects[i].objectType) { return true; } } return false; } } }