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.

145 lines
5.6 KiB

  1. // SDK Object State|Utilities|90160
  2. namespace VRTK
  3. {
  4. using UnityEngine;
  5. using System.Collections;
  6. using System.Reflection;
  7. /// <summary>
  8. /// The SDK Object State script can be used to set the enable/active state of a GameObject or Component based on SDK information.
  9. /// </summary>
  10. /// <remarks>
  11. /// The state can be determined by:
  12. /// * The current loaded SDK setup.
  13. /// * The current attached Headset type.
  14. /// * The current attached Controller type.
  15. /// </remarks>
  16. [AddComponentMenu("VRTK/Scripts/Utilities/VRTK_SDKObjectState")]
  17. public class VRTK_SDKObjectState : VRTK_SDKControllerReady
  18. {
  19. [Header("Target Settings")]
  20. [Tooltip("The GameObject or Component that is the target of the enable/disable action. If this is left blank then the GameObject that the script is attached to will be used as the `Target`.")]
  21. public Object target = null;
  22. [Tooltip("The state to set the `Target` to when this script is enabled. Checking this box will enable/activate the `Target`, unchecking will disable/deactivate the `Target`.")]
  23. public bool objectState = false;
  24. [Tooltip("If the currently loaded SDK Setup matches the one provided here then the `Target` state will be set to the desired `Object State`.")]
  25. public VRTK_SDKSetup loadedSDKSetup = null;
  26. [Tooltip("If the attached headset type matches the selected headset then the `Target` state will be set to the desired `Object State`.")]
  27. public SDK_BaseHeadset.HeadsetType headsetType = SDK_BaseHeadset.HeadsetType.Undefined;
  28. [Tooltip("If the current controller type matches the selected controller type then the `Target` state will be set to the desired `Object State`.")]
  29. public SDK_BaseController.ControllerType controllerType = SDK_BaseController.ControllerType.Undefined;
  30. protected Coroutine checkToggleRoutine;
  31. /// <summary>
  32. /// The SetStateByControllerReference method sets the object state based on the controller type of the given controller reference.
  33. /// </summary>
  34. /// <param name="controllerReference">A controller reference to check for the controller type of.</param>
  35. public virtual void SetStateByControllerReference(VRTK_ControllerReference controllerReference)
  36. {
  37. if (VRTK_ControllerReference.IsValid(controllerReference))
  38. {
  39. SDK_BaseController.ControllerType foundControllerType = VRTK_DeviceFinder.GetCurrentControllerType(controllerReference);
  40. if (foundControllerType != SDK_BaseController.ControllerType.Undefined && controllerType == foundControllerType)
  41. {
  42. ToggleObject();
  43. }
  44. }
  45. }
  46. protected override void OnEnable()
  47. {
  48. target = (target != null ? target : gameObject);
  49. base.OnEnable();
  50. checkToggleRoutine = StartCoroutine(CheckToggleAtEndOfFrame());
  51. }
  52. protected override void OnDisable()
  53. {
  54. base.OnDisable();
  55. if (checkToggleRoutine != null)
  56. {
  57. StopCoroutine(checkToggleRoutine);
  58. }
  59. }
  60. protected override void ControllerReady(VRTK_ControllerReference controllerReference)
  61. {
  62. ToggleOnController(controllerReference);
  63. }
  64. protected virtual IEnumerator CheckToggleAtEndOfFrame()
  65. {
  66. yield return new WaitForEndOfFrame();
  67. CheckToggle();
  68. }
  69. protected virtual void CheckToggle()
  70. {
  71. ToggleOnSDK();
  72. ToggleOnHeadset();
  73. }
  74. protected virtual void ToggleOnSDK()
  75. {
  76. if (loadedSDKSetup != null && loadedSDKSetup == VRTK_SDKManager.GetLoadedSDKSetup())
  77. {
  78. ToggleObject();
  79. }
  80. }
  81. protected virtual void ToggleOnHeadset()
  82. {
  83. if (headsetType != SDK_BaseHeadset.HeadsetType.Undefined && headsetType == VRTK_DeviceFinder.GetHeadsetType())
  84. {
  85. ToggleObject();
  86. }
  87. }
  88. protected virtual void ToggleOnController(VRTK_ControllerReference controllerReference)
  89. {
  90. if (controllerType != SDK_BaseController.ControllerType.Undefined)
  91. {
  92. SDK_BaseController.ControllerType foundControllerType = VRTK_DeviceFinder.GetCurrentControllerType(controllerReference);
  93. if (foundControllerType != SDK_BaseController.ControllerType.Undefined && controllerType == foundControllerType)
  94. {
  95. ToggleObject();
  96. }
  97. }
  98. }
  99. protected virtual void ToggleObject()
  100. {
  101. if (target is GameObject)
  102. {
  103. ToggleGameObject();
  104. }
  105. else if (VRTK_SharedMethods.IsTypeSubclassOf(target.GetType(), typeof(Component)))
  106. {
  107. ToggleComponent();
  108. }
  109. }
  110. protected virtual void ToggleGameObject()
  111. {
  112. if (target != null)
  113. {
  114. GameObject toggleTarget = (GameObject)target;
  115. toggleTarget.SetActive(objectState);
  116. }
  117. }
  118. protected virtual void ToggleComponent()
  119. {
  120. if (target != null)
  121. {
  122. Component toggleTarget = (Component)target;
  123. PropertyInfo property = toggleTarget.GetType().GetProperty("enabled");
  124. if (property != null)
  125. {
  126. property.SetValue(toggleTarget, objectState, null);
  127. }
  128. }
  129. }
  130. }
  131. }