SetActiveByPlayerInCollision.cs 774 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. using System;
  2. using UnityEngine;
  3. [RequireComponent(typeof(Collider2D))]
  4. public class SetActiveByPlayerInCollision : BaseBehaviour
  5. {
  6. private GameObject GameObject
  7. {
  8. get
  9. {
  10. return this._gameObject ?? base.gameObject;
  11. }
  12. }
  13. public void OnTriggerEnter2D(Collider2D collision)
  14. {
  15. if (!collision.CompareTag("Player"))
  16. {
  17. return;
  18. }
  19. this.GameObject.SetActive(!this._out);
  20. }
  21. public void OnTriggerExit2D(Collider2D collision)
  22. {
  23. if (!collision.CompareTag("Player"))
  24. {
  25. return;
  26. }
  27. this.GameObject.SetActive(this._out);
  28. }
  29. [Header("需要设置的 GameObject (默认为自身)")]
  30. [SerializeField]
  31. private GameObject _gameObject;
  32. [Header("离开碰撞区域激活")]
  33. [SerializeField]
  34. private bool _out;
  35. }