219 lines
8.0 KiB
C#
219 lines
8.0 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
namespace Stary.Evo.RKTools
|
|
{
|
|
public static class RKAddInteractionExtension
|
|
{
|
|
#region 触摸
|
|
|
|
/// <summary>
|
|
/// 添加触摸事件
|
|
/// </summary>
|
|
/// <param name="Touchedobject">被触碰物体</param>
|
|
/// <param name="TouchEvent">触碰后事件</param>
|
|
public static void ObjectAddTouchEvent(this GameObject Touchedobject, System.Action<GameObject> TouchEvent,
|
|
float TouchScale = 1.2f)
|
|
{
|
|
Collider ObjectCollider = Touchedobject.GetComponent<Collider>();
|
|
|
|
// 原有的组件设置优先级最高
|
|
if (ObjectCollider == null)
|
|
{
|
|
ObjectCollider = Touchedobject.AddComponent<BoxCollider>();
|
|
}
|
|
#if Evo_Rokid
|
|
ObjectCollider.isTrigger = true;
|
|
var GrabInteractable = Touchedobject.GetComponent<Rokid.UXR.Interaction.GrabInteractable>();
|
|
|
|
if (GrabInteractable == null)
|
|
{
|
|
GrabInteractable = Touchedobject.AddComponent<Rokid.UXR.Interaction.GrabInteractable>();
|
|
GrabInteractable.rate = TouchScale;
|
|
}
|
|
GrabInteractable.OnHoverBegin.AddListener(() => TouchEvent?.Invoke(Touchedobject));
|
|
#elif Evo_Xreal
|
|
ObjectCollider.isTrigger = false;
|
|
var GrabInteractable =
|
|
Touchedobject.GetComponent<UnityEngine.XR.Interaction.Toolkit.Interactables.XRGrabInteractable>();
|
|
|
|
if (GrabInteractable == null)
|
|
{
|
|
GrabInteractable =
|
|
Touchedobject.AddComponent<UnityEngine.XR.Interaction.Toolkit.Interactables.XRGrabInteractable>();
|
|
GrabInteractable.smoothScale = true;
|
|
GrabInteractable.smoothScaleAmount = TouchScale; // 低数值 = 高惯性
|
|
GrabInteractable.tightenScale = 0.1f; // 强平滑效果
|
|
}
|
|
GrabInteractable.throwOnDetach = false;
|
|
GrabInteractable.GetComponent<Rigidbody>().useGravity = false;
|
|
GrabInteractable.hoverEntered.AddListener((a) => TouchEvent?.Invoke(Touchedobject));
|
|
|
|
ObjectCollider.enabled = true;
|
|
#endif
|
|
}
|
|
|
|
/// <summary>
|
|
/// 暂停触摸事件
|
|
/// </summary>
|
|
/// <param name="Touchedobject">被触碰物体</param>
|
|
public static void ObjectPauseTouchEvent(this GameObject Touchedobject)
|
|
{
|
|
Collider ObjectCollider = Touchedobject.GetComponent<Collider>();
|
|
if (ObjectCollider != null)
|
|
{
|
|
ObjectCollider.enabled = false;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 恢复触摸事件
|
|
/// </summary>
|
|
/// <param name="Touchedobject">被触碰物体</param>
|
|
public static void ObjectResumeTouchEvent(this GameObject Touchedobject)
|
|
{
|
|
Collider ObjectCollider = Touchedobject.GetComponent<Collider>();
|
|
if (ObjectCollider != null)
|
|
{
|
|
ObjectCollider.enabled = true;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 移除触摸事件
|
|
/// </summary>
|
|
/// <param name="Touchedobject">被触碰物体</param>
|
|
public static void ObjectRemoveTouchEvent(this GameObject Touchedobject)
|
|
{
|
|
#if Evo_Rokid
|
|
Rokid.UXR.Interaction.GrabInteractable GrabInteractable =
|
|
Touchedobject.GetComponent<Rokid.UXR.Interaction.GrabInteractable>();
|
|
|
|
if (GrabInteractable != null)
|
|
{
|
|
GrabInteractable.OnHoverBegin.RemoveAllListeners();
|
|
Object.Destroy(GrabInteractable);
|
|
}
|
|
#elif Evo_Xreal
|
|
var GrabInteractable =
|
|
Touchedobject.GetComponent<UnityEngine.XR.Interaction.Toolkit.Interactables.XRGrabInteractable>();
|
|
|
|
if (GrabInteractable != null)
|
|
{
|
|
GrabInteractable.hoverEntered.RemoveAllListeners();
|
|
Object.Destroy(GrabInteractable);
|
|
}
|
|
#endif
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region 拖动
|
|
|
|
/// <summary>
|
|
/// 添加拖动事件
|
|
/// </summary>
|
|
/// <param name="Dragedobject">被拖动物体</param>
|
|
/// <param name="DragingEvent">拖动时执行的方法</param>
|
|
public static void objectAddDrag(this GameObject Dragedobject, System.Action<GameObject> DragingEvent = null,
|
|
float DragScale = 1.2f)
|
|
{
|
|
#if Evo_Rokid
|
|
Collider ObjectCollider = Dragedobject.GetComponent<Collider>();
|
|
Rokid.UXR.Interaction.GrabInteractable GrabInteractable =
|
|
Dragedobject.GetComponent<Rokid.UXR.Interaction.GrabInteractable>();
|
|
Rigidbody Rigidbody = Dragedobject.GetComponent<Rigidbody>();
|
|
Rokid.UXR.Interaction.Throwable Throwable = Dragedobject.GetComponent<Rokid.UXR.Interaction.Throwable>();
|
|
|
|
// 原有的组件设置优先级最高
|
|
if (ObjectCollider == null)
|
|
{
|
|
ObjectCollider = Dragedobject.AddComponent<BoxCollider>();
|
|
ObjectCollider.isTrigger = true;
|
|
}
|
|
|
|
if (GrabInteractable == null)
|
|
{
|
|
GrabInteractable = Dragedobject.AddComponent<Rokid.UXR.Interaction.GrabInteractable>();
|
|
GrabInteractable.rate = DragScale;
|
|
}
|
|
|
|
if (Rigidbody == null)
|
|
{
|
|
Rigidbody = Dragedobject.AddComponent<Rigidbody>();
|
|
Rigidbody.useGravity = false;
|
|
}
|
|
|
|
if (Throwable == null)
|
|
{
|
|
Throwable = Dragedobject.AddComponent<Rokid.UXR.Interaction.Throwable>();
|
|
}
|
|
|
|
GrabInteractable.OnHeldUpdate.AddListener(() => DragingEvent?.Invoke(Dragedobject));
|
|
#elif Evo_Xreal
|
|
var Throwable =
|
|
Dragedobject.GetComponent<UnityEngine.XR.Interaction.Toolkit.Interactables.XRGrabInteractable>();
|
|
|
|
if (Throwable == null)
|
|
{
|
|
Throwable =
|
|
Dragedobject.AddComponent<UnityEngine.XR.Interaction.Toolkit.Interactables.XRGrabInteractable>();
|
|
Throwable.throwOnDetach = true;
|
|
Throwable.throwVelocityScale = DragScale; // 低数值 = 高惯性
|
|
}
|
|
|
|
Throwable.GetComponent<Rigidbody>().useGravity = false;
|
|
Throwable.selectEntered.AddListener((a) => DragingEvent?.Invoke(Dragedobject));
|
|
|
|
Throwable.enabled = true;
|
|
#endif
|
|
}
|
|
|
|
/// <summary>
|
|
/// 暂停拖动
|
|
/// </summary>
|
|
/// <param name="Dragedobject"></param>
|
|
public static void objectPauseDrag(this GameObject Dragedobject)
|
|
{
|
|
Collider ObjectCollider = Dragedobject.GetComponent<Collider>();
|
|
if (ObjectCollider != null) ObjectCollider.enabled = false;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 恢复拖动
|
|
/// </summary>
|
|
/// <param name="Dragedobject"></param>
|
|
public static void objectResumeDrag(this GameObject Dragedobject)
|
|
{
|
|
Collider ObjectCollider = Dragedobject.GetComponent<Collider>();
|
|
if (ObjectCollider != null) ObjectCollider.enabled = true;
|
|
}
|
|
|
|
public static void objectRemoveDrag(this GameObject Dragedobject)
|
|
{
|
|
#if Evo_Rokid
|
|
Rokid.UXR.Interaction.Throwable Throwable = Dragedobject.GetComponent<Rokid.UXR.Interaction.Throwable>();
|
|
Rokid.UXR.Interaction.GrabInteractable GrabInteractable =
|
|
Dragedobject.GetComponent<Rokid.UXR.Interaction.GrabInteractable>();
|
|
if (Throwable != null) Object.Destroy(Throwable);
|
|
if (GrabInteractable != null)
|
|
{
|
|
GrabInteractable.OnHeldUpdate.RemoveAllListeners();
|
|
Object.Destroy(GrabInteractable);
|
|
}
|
|
#elif Evo_Xreal
|
|
var Throwable =
|
|
Dragedobject.GetComponent<UnityEngine.XR.Interaction.Toolkit.Interactables.XRGrabInteractable>();
|
|
|
|
if (Throwable != null)
|
|
{
|
|
Throwable.selectEntered.RemoveAllListeners();
|
|
Object.Destroy(Throwable);
|
|
}
|
|
#endif
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
} |