CollisionBase
This is an abstract class that serves as a base for collision handling in Unity. It provides event hooks for OnCollisionEnter, OnCollisionStay, and OnCollisionExit events, and defines methods for mana
Members
Type Parameters:
TCollider
: The type of collider component.TPhysicScene
: The type representing the physics scene.
Inherited From:
MonoBehaviour
: Unity's base class for scripting behavior in Unity.IEditableForEditor
: An interface for indicating whether the object is editable in the Unity editor.
Events:
public void SetEditable(bool state)
Event triggered when a collider enters collision.
public event Action<TCollider> OnCollisionStay
Event triggered when a collider stays in collision.
public event Action<TCollider> OnCollisionExit
Event triggered when a collider exits collision.
Fields:
[HideInInspector] public bool Editable
Indicates whether the object is editable in the Unity editor.
[SerializeField] protected LayerMask m_layer = ~0
Serialized field to specify the layer mask for collision.
[SerializeField] public int GarbageColliderSize = 3
Serialized field to specify the size of the garbage collider array.
[SerializeField] public Vector3 Center
Serialized field to specify the center of collision calculations.
Protected Fields:
protected TPhysicScene m_physicsScene
Reference to the physics scene.
protected TCollider[] m_garbageColliders
Array to store unused colliders.
Private Fields:
private List<TCollider> _colliders
List to store active colliders.
Methods
public void SetEditable(bool state)
Sets the editable state of the object.
public void ChangeEditable()
Toggles the editable state of the object.
public void UpdatePhysicScene(TPhysicScene physicsScene)
Updates the reference to the physics scene.
public void SetLayer(LayerMask layerMask)
Sets the layer mask for collision.
protected abstract void CalculateCollision()
Abstract method to calculate collisions. Implementation is left to subclasses.
protected abstract void GetPhysicScene()
Abstract method to get the physics scene. Implementation is left to subclasses.
private void SetCollidersCapacity()
nitializes the collider arrays with the specified size.
private void HandleColliderCleaner()
Cleans up null entries in the collider list and resets the garbage collider array.
private void HandleNewCollisions(TCollider[] colliders)
Handles new collisions by adding colliders to the list and triggering OnCollisionEnter event.
private void HandleContinuedCollisions(TCollider[] colliders)
Handles continued collisions by triggering OnCollisionStay event for colliders in the list.
private void HandleCollisionsExit(TCollider[] colliders)
Handles collision exits by removing colliders from the list and triggering OnCollisionExit event.
private void HandleCollisionEnter(TCollider coll)
Invokes the OnCollisionEnter event for the specified collider.
private void HandleCollisionStay(TCollider coll)
Invokes the OnCollisionStay event for the specified collider.
private void HandleCollisionExit(TCollider coll)
Invokes the OnCollisionExit event for the specified collider
Last updated