可拾取道具

一把可拾取的工具:拿在手上、按下使用键出光束、松手收起。适合做”手电筒/枪/喷壶”这类东西。

准备

  1. 道具物体上加 Aeloria Pickup Interactable(可拾取);
  2. 准备一个”光束”子物体并默认关掉,拖进 Beam;
  3. 把脚本挂上去,把拾取组件拖进 Pickup。
using UnityEngine;
using Cilbox;
using Aeloria.Scripts.AeloriaSdk.Interactions;
using Aeloria.Scripts.Device_Management.Devices;

[Cilboxable]
public class PickupTool : MonoBehaviour
{
    public AeloriaPickupInteractable pickup;
    public GameObject beam;

    void Start()
    {
        pickup.OnPickupUse.AddListener(OnUse);
        pickup.OnInteractStartEvent.AddListener(OnGrab);
        pickup.OnInteractEndEvent.AddListener(OnRelease);
    }

    void OnDestroy()
    {
        pickup.OnPickupUse.RemoveAllListeners();
        pickup.OnInteractStartEvent.RemoveAllListeners();
        pickup.OnInteractEndEvent.RemoveAllListeners();
    }

    void OnGrab(AeloriaInput input) { }

    void OnRelease(AeloriaInput input) { beam.SetActive(false); }

    void OnUse(AeloriaPickUpUseMode mode)
    {
        if (mode == AeloriaPickUpUseMode.OnPickUpUseDown) beam.SetActive(true);
        else if (mode == AeloriaPickUpUseMode.OnPickUpUseUp) beam.SetActive(false);
    }
}
  • OnPickupUse 会给出三种模式:按下 OnPickUpUseDown、松开 OnPickUpUseUp、持续按住 OnPickUpStillDown;
  • 交互开始/结束用 OnInteractStartEvent / OnInteractEndEvent,回调参数是 AeloriaInput;
  • 松手时记得把特效关掉,避免掉落在地上还在发光。