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.

65 lines
2.0 KiB

  1. namespace VRTK.Examples
  2. {
  3. using UnityEngine;
  4. using System.Collections.Generic;
  5. public class RendererOffOnDash : MonoBehaviour
  6. {
  7. private bool wasSwitchedOff = false;
  8. private List<VRTK_DashTeleport> dashTeleporters = new List<VRTK_DashTeleport>();
  9. private void OnEnable()
  10. {
  11. foreach (var teleporter in VRTK_ObjectCache.registeredTeleporters)
  12. {
  13. var dashTeleporter = teleporter.GetComponent<VRTK_DashTeleport>();
  14. if (dashTeleporter)
  15. {
  16. dashTeleporters.Add(dashTeleporter);
  17. }
  18. }
  19. foreach (var dashTeleport in dashTeleporters)
  20. {
  21. dashTeleport.WillDashThruObjects += new DashTeleportEventHandler(RendererOff);
  22. dashTeleport.DashedThruObjects += new DashTeleportEventHandler(RendererOn);
  23. }
  24. }
  25. private void OnDisable()
  26. {
  27. foreach (var dashTeleport in dashTeleporters)
  28. {
  29. dashTeleport.WillDashThruObjects -= new DashTeleportEventHandler(RendererOff);
  30. dashTeleport.DashedThruObjects -= new DashTeleportEventHandler(RendererOn);
  31. }
  32. }
  33. private void RendererOff(object sender, DashTeleportEventArgs e)
  34. {
  35. GameObject go = this.transform.gameObject;
  36. foreach (RaycastHit hit in e.hits)
  37. {
  38. if (hit.collider.gameObject == go)
  39. {
  40. SwitchRenderer(go, false);
  41. break;
  42. }
  43. }
  44. }
  45. private void RendererOn(object sender, DashTeleportEventArgs e)
  46. {
  47. GameObject go = this.transform.gameObject;
  48. if (wasSwitchedOff)
  49. {
  50. SwitchRenderer(go, true);
  51. }
  52. }
  53. private void SwitchRenderer(GameObject go, bool enable)
  54. {
  55. go.GetComponent<Renderer>().enabled = enable;
  56. wasSwitchedOff = !enable;
  57. }
  58. }
  59. }