VRTK_SDKObjectAlias.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // SDK Object Alias|Utilities|90140
  2. namespace VRTK
  3. {
  4. using UnityEngine;
  5. /// <summary>
  6. /// The GameObject that the SDK Object Alias script is applied to will become a child of the selected SDK Object.
  7. /// </summary>
  8. [AddComponentMenu("VRTK/Scripts/Utilities/VRTK_SDKObjectAlias")]
  9. public class VRTK_SDKObjectAlias : MonoBehaviour
  10. {
  11. /// <summary>
  12. /// Valid SDK Objects
  13. /// </summary>
  14. public enum SDKObject
  15. {
  16. /// <summary>
  17. /// The main camera rig/play area object that defines the player boundary.
  18. /// </summary>
  19. Boundary,
  20. /// <summary>
  21. /// The main headset camera defines the player head.
  22. /// </summary>
  23. Headset
  24. }
  25. [Tooltip("The specific SDK Object to child this GameObject to.")]
  26. public SDKObject sdkObject = SDKObject.Boundary;
  27. protected virtual void OnEnable()
  28. {
  29. VRTK_SDKManager.SubscribeLoadedSetupChanged(LoadedSetupChanged);
  30. ChildToSDKObject();
  31. }
  32. protected virtual void OnDisable()
  33. {
  34. if (!gameObject.activeSelf)
  35. {
  36. VRTK_SDKManager.UnsubscribeLoadedSetupChanged(LoadedSetupChanged);
  37. }
  38. }
  39. protected virtual void LoadedSetupChanged(VRTK_SDKManager sender, VRTK_SDKManager.LoadedSetupChangeEventArgs e)
  40. {
  41. if (VRTK_SDKManager.ValidInstance() && gameObject.activeInHierarchy)
  42. {
  43. ChildToSDKObject();
  44. }
  45. }
  46. protected virtual void ChildToSDKObject()
  47. {
  48. Vector3 currentPosition = transform.localPosition;
  49. Quaternion currentRotation = transform.localRotation;
  50. Vector3 currentScale = transform.localScale;
  51. Transform newParent = null;
  52. switch (sdkObject)
  53. {
  54. case SDKObject.Boundary:
  55. newParent = VRTK_DeviceFinder.PlayAreaTransform();
  56. break;
  57. case SDKObject.Headset:
  58. newParent = VRTK_DeviceFinder.HeadsetTransform();
  59. break;
  60. }
  61. transform.SetParent(newParent);
  62. transform.localPosition = currentPosition;
  63. transform.localRotation = currentRotation;
  64. transform.localScale = currentScale;
  65. }
  66. }
  67. }