revival/game/Assets/Scripts/Util/Events/GameEventTrigger.cs

82 lines
1.8 KiB
C#
Raw Normal View History

2021-04-15 15:44:06 +02:00
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
#if UNITY_EDITOR
using UnityEditor;
#endif
2021-04-15 15:44:06 +02:00
namespace Ktyl.Util
{
[RequireComponent(typeof(BoxCollider))]
public partial class GameEventTrigger : MonoBehaviour
2021-04-15 15:44:06 +02:00
{
2021-05-01 03:01:57 +02:00
[Serializable]
private struct Event
{
public GameEvent game;
public UnityEvent unity;
public void Raise()
{
if (game) game.Raise();
unity.Invoke();
}
}
2021-04-15 15:44:06 +02:00
2021-05-01 03:01:57 +02:00
[SerializeField] private Event _onEnter;
[SerializeField] private Event _onExit;
2021-04-15 15:44:06 +02:00
private void Start()
{
GetComponent<BoxCollider>().isTrigger = true;
}
private void OnTriggerEnter(Collider other)
{
if (!other.CompareTag("Player")) return;
Trigger();
2021-05-01 03:01:57 +02:00
}
private void Trigger()
{
_onEnter.Raise();
}
2021-05-01 03:01:57 +02:00
private void OnTriggerExit(Collider other)
{
_onExit.Raise();
2021-04-15 15:44:06 +02:00
}
}
#if UNITY_EDITOR
public partial class GameEventTrigger
{
public void EDITOR_Trigger() => Trigger();
}
[CustomEditor(typeof(GameEventListener))]
public class GameEventTriggerEditor : Editor
{
private GameEventTrigger _data;
private void OnEnable()
{
_data = target as GameEventTrigger;
}
public override void OnInspectorGUI()
{
base.OnInspectorGUI();
if (GUILayout.Button("Trigger"))
{
_data.EDITOR_Trigger();
}
}
}
#endif
2021-04-15 15:44:06 +02:00
}