添加缺失插件
This commit is contained in:
79
Assets/YOMOV Access/Scripts/ResetObject.cs
Normal file
79
Assets/YOMOV Access/Scripts/ResetObject.cs
Normal file
@@ -0,0 +1,79 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.XR.Interaction.Toolkit;
|
||||
|
||||
// 假设命名空间是 LevelUp.GrabInteractions
|
||||
namespace LevelUp.GrabInteractions
|
||||
{
|
||||
// 基础类,负责事件监听和延迟调用
|
||||
[RequireComponent(typeof(UnityEngine.XR.Interaction.Toolkit.Interactables.XRGrabInteractable))]
|
||||
public class ResetObject : MonoBehaviour
|
||||
{
|
||||
[Header("基础重置设置")]
|
||||
[Tooltip("物体返回原位之前的延迟时间 (秒)。")]
|
||||
[SerializeField]
|
||||
protected float resetDelayTime = 3f;
|
||||
|
||||
// 核心字段
|
||||
protected UnityEngine.XR.Interaction.Toolkit.Interactables.XRGrabInteractable m_GrabInteractable;
|
||||
protected Vector3 returnToPosition;
|
||||
|
||||
[Tooltip("指示物体是否应该被重置。子类可用于逻辑判断。")]
|
||||
public bool shouldReturnHome { get; protected set; } = true;
|
||||
|
||||
protected void Awake()
|
||||
{
|
||||
m_GrabInteractable = GetComponent<UnityEngine.XR.Interaction.Toolkit.Interactables.XRGrabInteractable>();
|
||||
}
|
||||
|
||||
protected void Start()
|
||||
{
|
||||
// 记录物体在场景中的初始位置
|
||||
returnToPosition = this.transform.position;
|
||||
}
|
||||
|
||||
void OnEnable()
|
||||
{
|
||||
if (m_GrabInteractable != null)
|
||||
{
|
||||
m_GrabInteractable.selectExited.AddListener(OnSelectExit);
|
||||
m_GrabInteractable.selectEntered.AddListener(OnSelect);
|
||||
}
|
||||
}
|
||||
|
||||
void OnDisable()
|
||||
{
|
||||
if (m_GrabInteractable != null)
|
||||
{
|
||||
m_GrabInteractable.selectExited.RemoveListener(OnSelectExit);
|
||||
m_GrabInteractable.selectEntered.RemoveListener(OnSelect);
|
||||
}
|
||||
CancelInvoke(nameof(ReturnHome));
|
||||
}
|
||||
|
||||
protected virtual void OnSelect(SelectEnterEventArgs arg0)
|
||||
{
|
||||
// 抓取时取消任何待执行的 ReturnHome 调用
|
||||
CancelInvoke(nameof(ReturnHome));
|
||||
}
|
||||
|
||||
protected virtual void OnSelectExit(SelectExitEventArgs arg0)
|
||||
{
|
||||
// 松手时延迟调用 ReturnHome
|
||||
Invoke(nameof(ReturnHome), resetDelayTime);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 核心重置函数。设置为 virtual 以便子类重写。
|
||||
/// 默认实现为瞬移。
|
||||
/// </summary>
|
||||
protected virtual void ReturnHome()
|
||||
{
|
||||
if (shouldReturnHome)
|
||||
{
|
||||
// 父类的默认实现是瞬移
|
||||
transform.position = returnToPosition;
|
||||
Debug.Log($"物体 '{gameObject.name}' 已瞬移重置到原点。", this);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user