Assignment for RMIT Mixed Reality in 2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

83 lines
3.3 KiB

  1. //====================================================================================
  2. //
  3. // Purpose: Provide a way of tagging game objects as player specific objects to
  4. // allow other scripts to identify these specific objects without needing to use tags
  5. // or without needing to append the name of the game object.
  6. //
  7. //====================================================================================
  8. namespace VRTK
  9. {
  10. using UnityEngine;
  11. public sealed class VRTK_PlayerObject : MonoBehaviour
  12. {
  13. /// <summary>
  14. /// The type of object associated to the player.
  15. /// </summary>
  16. public enum ObjectTypes
  17. {
  18. /// <summary>
  19. /// No defined object.
  20. /// </summary>
  21. Null,
  22. /// <summary>
  23. /// The object that represents the VR camera rig.
  24. /// </summary>
  25. CameraRig,
  26. /// <summary>
  27. /// The object that represents the VR headset.
  28. /// </summary>
  29. Headset,
  30. /// <summary>
  31. /// An object that represents a VR controller.
  32. /// </summary>
  33. Controller,
  34. /// <summary>
  35. /// An object that represents a player generated pointer.
  36. /// </summary>
  37. Pointer,
  38. /// <summary>
  39. /// An object that represents a player generated highlighter.
  40. /// </summary>
  41. Highlighter,
  42. /// <summary>
  43. /// An object that represents a player collider.
  44. /// </summary>
  45. Collider
  46. }
  47. public ObjectTypes objectType;
  48. /// <summary>
  49. /// The SetPlayerObject method tags the given game object with a special player object class for easier identification.
  50. /// </summary>
  51. /// <param name="obj">The game object to add the player object class to.</param>
  52. /// <param name="objType">The type of player object that is to be assigned.</param>
  53. public static void SetPlayerObject(GameObject obj, ObjectTypes objType)
  54. {
  55. VRTK_PlayerObject currentPlayerObject = obj.GetComponent<VRTK_PlayerObject>();
  56. if (currentPlayerObject == null)
  57. {
  58. currentPlayerObject = obj.AddComponent<VRTK_PlayerObject>();
  59. }
  60. currentPlayerObject.objectType = objType;
  61. }
  62. /// <summary>
  63. /// The IsPlayerObject method determines if the given game object is a player object and can also check if it's of a specific type.
  64. /// </summary>
  65. /// <param name="obj">The GameObjet to check if it's a player object.</param>
  66. /// <param name="ofType">An optional ObjectType to check if the given GameObject is of a specific player object.</param>
  67. /// <returns>Returns true if the object is a player object with the optional given type.</returns>
  68. public static bool IsPlayerObject(GameObject obj, ObjectTypes ofType = ObjectTypes.Null)
  69. {
  70. VRTK_PlayerObject[] playerObjects = obj.GetComponentsInParent<VRTK_PlayerObject>(true);
  71. for (int i = 0; i < playerObjects.Length; i++)
  72. {
  73. if (ofType == ObjectTypes.Null || ofType == playerObjects[i].objectType)
  74. {
  75. return true;
  76. }
  77. }
  78. return false;
  79. }
  80. }
  81. }