通过脚本所挂载的物体名称来添加不同触发事件。
using UnityEngine;
/// <summary>
/// 碰撞体物体管理类
/// </summary>
public class ColliderManager_01 : MonoBehaviour
{
private delegate void TriggerEvent(Collider other);
private TriggerEvent enterEvent, stayEvent, exitEvent;
// Start is called before the first frame update
private void Awake()
{
enterEvent = null;
stayEvent = null;
exitEvent = null;
switch (transform.name)
{
case "NoseBoxCollider":
enterEvent = NoseBoxColliderEnter;
stayEvent = NoseBoxColliderStay;
exitEvent = NoseBoxColliderExit;
break;
case "PlayerCollider":
enterEvent = BodyColliderEnter;
stayEvent = BodyColliderStay;
exitEvent = BodyxColliderExit;
break;
default:
break;
}
}
#region 触发事件调用
private void OnTriggerEnter(Collider other)
{
if (enterEvent != null)
enterEvent(other);
}
private void OnTriggerStay(Collider other)
{
if (stayEvent != null)
stayEvent(other);
}
private void OnTriggerExit(Collider other)
{
if (exitEvent != null)
exitEvent(other);
}
#endregion
#region 鼻子上触发器的事件
private void NoseBoxColliderEnter(Collider other)
{
if (other.name.Equals("HandTowel"))
{
//手中的毛巾触碰到鼻子
}
}
private void NoseBoxColliderStay(Collider other)
{
}
private void NoseBoxColliderExit(Collider other)
{
}
#endregion
#region 身体上触发器的事件
private void BodyColliderEnter(Collider other)
{
if (other.tag.Equals("Stone"))
{
Debug.LogError("石头碰撞人了!!");
}
}
private void BodyColliderStay(Collider other)
{
}
private void BodyxColliderExit(Collider other)
{
}
#endregion
}
|