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.

95 lines
3.4 KiB

  1. using System;
  2. using UnityEngine;
  3. using Object = UnityEngine.Object;
  4. namespace UnityStandardAssets.Utility
  5. {
  6. public class ActivateTrigger : MonoBehaviour
  7. {
  8. // A multi-purpose script which causes an action to occur when
  9. // a trigger collider is entered.
  10. public enum Mode
  11. {
  12. Trigger = 0, // Just broadcast the action on to the target
  13. Replace = 1, // replace target with source
  14. Activate = 2, // Activate the target GameObject
  15. Enable = 3, // Enable a component
  16. Animate = 4, // Start animation on target
  17. Deactivate = 5 // Decativate target GameObject
  18. }
  19. public Mode action = Mode.Activate; // The action to accomplish
  20. public Object target; // The game object to affect. If none, the trigger work on this game object
  21. public GameObject source;
  22. public int triggerCount = 1;
  23. public bool repeatTrigger = false;
  24. private void DoActivateTrigger()
  25. {
  26. triggerCount--;
  27. if (triggerCount == 0 || repeatTrigger)
  28. {
  29. Object currentTarget = target ?? gameObject;
  30. Behaviour targetBehaviour = currentTarget as Behaviour;
  31. GameObject targetGameObject = currentTarget as GameObject;
  32. if (targetBehaviour != null)
  33. {
  34. targetGameObject = targetBehaviour.gameObject;
  35. }
  36. switch (action)
  37. {
  38. case Mode.Trigger:
  39. if (targetGameObject != null)
  40. {
  41. targetGameObject.BroadcastMessage("DoActivateTrigger");
  42. }
  43. break;
  44. case Mode.Replace:
  45. if (source != null)
  46. {
  47. if (targetGameObject != null)
  48. {
  49. Instantiate(source, targetGameObject.transform.position,
  50. targetGameObject.transform.rotation);
  51. DestroyObject(targetGameObject);
  52. }
  53. }
  54. break;
  55. case Mode.Activate:
  56. if (targetGameObject != null)
  57. {
  58. targetGameObject.SetActive(true);
  59. }
  60. break;
  61. case Mode.Enable:
  62. if (targetBehaviour != null)
  63. {
  64. targetBehaviour.enabled = true;
  65. }
  66. break;
  67. case Mode.Animate:
  68. if (targetGameObject != null)
  69. {
  70. targetGameObject.GetComponent<Animation>().Play();
  71. }
  72. break;
  73. case Mode.Deactivate:
  74. if (targetGameObject != null)
  75. {
  76. targetGameObject.SetActive(false);
  77. }
  78. break;
  79. }
  80. }
  81. }
  82. private void OnTriggerEnter(Collider other)
  83. {
  84. DoActivateTrigger();
  85. }
  86. }
  87. }