Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using UnityEngine.Events;
- using System;
- using System.Collections;
- using System.Collections.Generic;
- /// <summary>
- /// Represents a single collision event configuration with its associated actions and debug settings.
- /// </summary>
- [Serializable]
- public class CollisionEventEntry
- {
- /// <summary>
- /// Defines the type of collision detection to listen for.
- /// </summary>
- public enum CollisionType
- {
- /// <summary>3D physics collision enter event</summary>
- OnCollisionEnter3D,
- /// <summary>2D physics collision enter event</summary>
- OnCollisionEnter2D,
- /// <summary>3D trigger enter event</summary>
- OnTriggerEnter3D,
- /// <summary>2D trigger enter event</summary>
- OnTriggerEnter2D
- }
- /// <summary>
- /// Controls the level of debug logging for this collision event.
- /// </summary>
- public enum DebugMode
- {
- /// <summary>No debug logging</summary>
- None,
- /// <summary>Basic collision information</summary>
- Lite,
- /// <summary>Detailed collision information</summary>
- Verbose
- }
- [Tooltip("Friendly name for this collision event")]
- public string label = "Event";
- [Tooltip("Type of collision to detect")]
- public CollisionType collisionType;
- [Tooltip("Only trigger for objects with this tag (leave empty to trigger for all objects)")]
- public string compareTag;
- [Tooltip("Delay in seconds before triggering the delayed event")]
- [Min(0f)]
- public float delay = 0f;
- [Tooltip("Event triggered immediately upon collision")]
- public UnityEvent onCollision;
- [Tooltip("Event triggered after the specified delay")]
- public UnityEvent delayedEvent;
- [Tooltip("Level of debug information to log")]
- public DebugMode debug = DebugMode.Lite;
- /// <summary>
- /// Checks if the collision matches this event's criteria.
- /// </summary>
- /// <param name="other">The GameObject to check against</param>
- /// <param name="currentType">The type of collision that occurred</param>
- /// <returns>True if the collision matches the criteria</returns>
- public bool Matches(GameObject other, CollisionType currentType)
- {
- if (other == null) return false;
- if (collisionType != currentType) return false;
- if (!string.IsNullOrEmpty(compareTag) && !other.CompareTag(compareTag)) return false;
- return true;
- }
- /// <summary>
- /// Logs debug information based on the configured debug level.
- /// </summary>
- public void Log(string msg, DebugMode level)
- {
- if (debug == DebugMode.None) return;
- if (debug == DebugMode.Lite && level == DebugMode.Verbose) return;
- Debug.Log($"[CollisionEvent:{label}] {msg}");
- }
- }
- /// <summary>
- /// Manages multiple collision events with support for both 2D and 3D physics,
- /// delayed events, and configurable debug logging.
- /// </summary>
- public class OnCollisionEvents : MonoBehaviour
- {
- [Tooltip("List of collision event configurations")]
- public List<CollisionEventEntry> events = new();
- private void OnValidate()
- {
- // Ensure each event has a unique label for debugging
- var usedLabels = new HashSet<string>();
- int counter = 1;
- foreach (var entry in events)
- {
- if (string.IsNullOrEmpty(entry.label))
- entry.label = $"Event_{counter}";
- else if (usedLabels.Contains(entry.label))
- entry.label = $"{entry.label}_{counter}";
- usedLabels.Add(entry.label);
- counter++;
- }
- }
- #region 3D Events
- private void OnCollisionEnter(Collision collision)
- {
- if (collision != null)
- HandleEvent(collision.gameObject, CollisionEventEntry.CollisionType.OnCollisionEnter3D);
- }
- private void OnTriggerEnter(Collider other)
- {
- if (other != null)
- HandleEvent(other.gameObject, CollisionEventEntry.CollisionType.OnTriggerEnter3D);
- }
- #endregion
- #region 2D Events
- private void OnCollisionEnter2D(Collision2D collision)
- {
- if (collision != null)
- HandleEvent(collision.gameObject, CollisionEventEntry.CollisionType.OnCollisionEnter2D);
- }
- private void OnTriggerEnter2D(Collider2D other)
- {
- if (other != null)
- HandleEvent(other.gameObject, CollisionEventEntry.CollisionType.OnTriggerEnter2D);
- }
- #endregion
- private void HandleEvent(GameObject other, CollisionEventEntry.CollisionType type)
- {
- if (other == null) return;
- foreach (var entry in events)
- {
- if (!entry.Matches(other, type)) continue;
- entry.Log($"Triggered by {other.name} (tag: {other.tag})", CollisionEventEntry.DebugMode.Lite);
- try
- {
- entry.onCollision?.Invoke();
- }
- catch (Exception e)
- {
- Debug.LogError($"[CollisionEvent:{entry.label}] Error in onCollision event: {e}");
- }
- if (entry.delay > 0f)
- {
- entry.Log($"Scheduling delayedEvent in {entry.delay} seconds", CollisionEventEntry.DebugMode.Lite);
- StartCoroutine(DelayedCall(entry));
- }
- }
- }
- private IEnumerator DelayedCall(CollisionEventEntry entry)
- {
- yield return new WaitForSeconds(entry.delay);
- entry.Log("Invoking delayedEvent", CollisionEventEntry.DebugMode.Lite);
- try
- {
- entry.delayedEvent?.Invoke();
- }
- catch (Exception e)
- {
- Debug.LogError($"[CollisionEvent:{entry.label}] Error in delayedEvent: {e}");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment