35 lines
720 B
C#
35 lines
720 B
C#
using UnityEngine;
|
|
|
|
public class OpAudioPlayer : MonoBehaviour
|
|
{
|
|
public AudioSource audioSource;
|
|
|
|
void Start()
|
|
{
|
|
if (audioSource == null)
|
|
{
|
|
audioSource = GetComponent<AudioSource>();
|
|
|
|
if (audioSource == null)
|
|
{
|
|
Debug.LogError("OpAudioPlayer: 找不到 AudioSource 组件。请确保组件已添加或在 Inspector 中设置。");
|
|
return;
|
|
}
|
|
}
|
|
|
|
audioSource.loop = true;
|
|
|
|
if (!audioSource.isPlaying)
|
|
{
|
|
audioSource.Play();
|
|
}
|
|
}
|
|
|
|
private void OnDestroy()
|
|
{
|
|
if (audioSource != null && audioSource.isPlaying)
|
|
{
|
|
audioSource.Stop();
|
|
}
|
|
}
|
|
} |