//====================================================================================
//
// 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
{
public enum ObjectTypes
{
Null,
CameraRig,
Headset,
Controller,
Pointer,
Highlighter,
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)
{
var 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)
{
foreach (var playerObject in obj.GetComponentsInParent(true))
{
if (ofType == ObjectTypes.Null || ofType == playerObject.objectType)
{
return true;
}
}
return false;
}
}
}