提交交互替换

This commit is contained in:
YXY
2026-03-05 18:52:29 +08:00
parent c7de21bb9a
commit eb9306ba2c
47 changed files with 3820 additions and 1743 deletions

View File

@@ -1,10 +1,8 @@
using UnityEngine;
using UnityEngine.XR.Interaction.Toolkit;
// 假设命名空间是 LevelUp.GrabInteractions
namespace LevelUp.GrabInteractions
{
// 基础类,负责事件监听和延迟调用
[RequireComponent(typeof(UnityEngine.XR.Interaction.Toolkit.Interactables.XRGrabInteractable))]
public class ResetObject : MonoBehaviour
{
@@ -12,24 +10,25 @@ namespace LevelUp.GrabInteractions
[Tooltip("物体返回原位之前的延迟时间 (秒)。")]
[SerializeField]
protected float resetDelayTime = 3f;
// 核心字段
protected UnityEngine.XR.Interaction.Toolkit.Interactables.XRGrabInteractable m_GrabInteractable;
protected Vector3 returnToPosition;
protected Quaternion returnToRotation;
protected Rigidbody m_Rigidbody;
[Tooltip("指示物体是否应该被重置。子类可用于逻辑判断。")]
public bool shouldReturnHome { get; protected set; } = true;
public AudioSource downAudio;
protected void Awake()
protected void Awake()
{
m_GrabInteractable = GetComponent<UnityEngine.XR.Interaction.Toolkit.Interactables.XRGrabInteractable>();
m_Rigidbody = GetComponent<Rigidbody>();
}
protected void Start()
protected void Start()
{
// 记录物体在场景中的初始位置
returnToPosition = this.transform.position;
returnToRotation = this.transform.rotation;
}
void OnEnable()
@@ -53,28 +52,31 @@ namespace LevelUp.GrabInteractions
protected virtual void OnSelect(SelectEnterEventArgs arg0)
{
// 抓取时取消任何待执行的 ReturnHome 调用
CancelInvoke(nameof(ReturnHome));
}
protected virtual void OnSelectExit(SelectExitEventArgs arg0)
{
// 松手时延迟调用 ReturnHome
Invoke(nameof(ReturnHome), resetDelayTime);
if (downAudio != null)
downAudio.Play();
downAudio.Play();
}
/// <summary>
/// 核心重置函数。设置为 virtual 以便子类重写。
/// 默认实现为瞬移。
/// </summary>
protected virtual void ReturnHome()
{
if (shouldReturnHome)
{
// 父类的默认实现是瞬移
if (m_Rigidbody != null && !m_Rigidbody.isKinematic)
{
m_Rigidbody.velocity = Vector3.zero;
m_Rigidbody.angularVelocity = Vector3.zero;
}
transform.position = returnToPosition;
transform.rotation = returnToRotation;
Debug.Log($"物体 '{gameObject.name}' 已瞬移重置到原点。", this);
}
}