45 lines
1.3 KiB
C#
45 lines
1.3 KiB
C#
using UnityEngine;
|
|
using System;
|
|
|
|
namespace Fruitomation.Game
|
|
{
|
|
public enum TriggerType
|
|
{
|
|
Enter,
|
|
Stay,
|
|
Exit
|
|
}
|
|
|
|
public class TriggerDetector : MonoBehaviour
|
|
{
|
|
private Action<Collider2D> RegisteredActionStay;
|
|
private Action<Collider2D> RegisteredActionEnter;
|
|
private Action<Collider2D> RegisteredActionExit;
|
|
|
|
public void SetAction(Action<Collider2D> action, TriggerType type)
|
|
{
|
|
switch (type)
|
|
{
|
|
case TriggerType.Stay:
|
|
RegisteredActionStay = action;
|
|
return;
|
|
|
|
case TriggerType.Enter:
|
|
RegisteredActionEnter = action;
|
|
return;
|
|
|
|
case TriggerType.Exit:
|
|
RegisteredActionExit = action;
|
|
return;
|
|
|
|
default:
|
|
throw new ArgumentOutOfRangeException(nameof(type), type, null);
|
|
}
|
|
}
|
|
|
|
private void OnTriggerEnter2D(Collider2D other) => RegisteredActionEnter?.Invoke(other);
|
|
private void OnTriggerStay2D(Collider2D other) => RegisteredActionStay?.Invoke(other);
|
|
private void OnTriggerExit2D(Collider2D other) => RegisteredActionExit?.Invoke(other);
|
|
}
|
|
}
|