Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.Events;
- namespace GameCore
- {
- [RequireComponent (typeof (Collider))]
- public class TriggerTagActions : MonoBehaviour
- {
- private Collider _collider = null;
- public new Collider collider { get { if (_collider == null) _collider = GetComponent<Collider> (); return _collider; } }
- private void Awake () { collider.isTrigger = true; }
- [SerializeField] List<TagAction> OnEnterTrigger;
- [SerializeField] List<TagAction> OnExitTrigger;
- public void AddOnEnterAction (string Tag, UnityEvent action)
- {
- TagAction tagAction = new TagAction ();
- tagAction.Tag = Tag;
- tagAction.Action = action;
- OnEnterTrigger.Add (tagAction);
- }
- public void AddOnExitAction (string Tag, UnityAction action)
- {
- var tagAction = OnExitTrigger.Find (ta => ta.Tag == Tag);
- if (tagAction == null) return;
- tagAction.Action.AddListener (action);
- }
- private void OnTriggerEnter (Collider other)
- {
- foreach (var trigger in OnEnterTrigger)
- trigger.Invoke (other.gameObject);
- }
- private void OnTriggerExit (Collider other)
- {
- foreach (var trigger in OnExitTrigger)
- trigger.Invoke (other.gameObject);
- }
- }
- [System.Serializable]
- public class TagAction
- {
- public string Tag;
- public UnityEvent Action;
- public void Invoke (GameObject go)
- {
- if (go.CompareTag (Tag))
- {
- Action.Invoke ();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment