示例
抓到物体时播放音效,松手时停止或再播一段。
AeloriaInteractableObject 或 AeloriaPickupInteractable),拖进 Interactable;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()。