抓取音效

抓到物体时播放音效,松手时停止或再播一段。

准备

  1. 物体上加 AudioSource 并设好 AudioClip;
  2. 物体上加可交互组件(AeloriaInteractableObject 或 AeloriaPickupInteractable),拖进 Interactable;
  3. 挂上脚本。
using UnityEngine;
using Cilbox;
using Aeloria.Scripts.AeloriaSdk.Interactions;
using Aeloria.Scripts.Device_Management.Devices;

[Cilboxable]
public class GrabSound : MonoBehaviour
{
    public AeloriaInteractableObject interactable;
    AudioSource source;

    void Start()
    {
        source = GetComponent<AudioSource>();
        interactable.OnInteractStartEvent.AddListener(OnGrab);
        interactable.OnInteractEndEvent.AddListener(OnRelease);
    }

    void OnDestroy()
    {
        interactable.OnInteractStartEvent.RemoveAllListeners();
        interactable.OnInteractEndEvent.RemoveAllListeners();
    }

    void OnGrab(AeloriaInput input) { source.Play(); }
    void OnRelease(AeloriaInput input) { source.Stop(); }
}

想”抓一次只响一下”就用 PlayOneShot(clip);想一直循环响就勾上 AudioSource 的 Loop,然后在松手时 Stop()。