rabidus-test/Assets/SoundEmitter.cs

45 lines
910 B
C#
Raw Normal View History

2023-10-09 16:51:27 +03:00
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SoundEmitter : MonoBehaviour
{
public Sound Sound;
public SoundPlayer SoundPlayer;
2023-10-27 18:07:01 +03:00
public bool PlayOnlyOnce = false;
public bool MuteOtherSound = false;
private bool _played = false;
private void Awake()
{
if (Sound.PlayOnAwake)
{
Play();
}
}
2023-10-09 16:51:27 +03:00
[ContextMenu("debug")]
public void Play()
{
2023-10-27 18:07:01 +03:00
if (PlayOnlyOnce && _played)
return;
if (MuteOtherSound)
{
var otherSounds = FindObjectsOfType<SoundPlayer>();
foreach (var sound in otherSounds)
{
2023-10-31 14:38:42 +03:00
sound.Mute(Sound.Clip.length);
2023-10-27 18:07:01 +03:00
}
}
2023-10-09 16:51:27 +03:00
var s = Instantiate(SoundPlayer, transform.position, Quaternion.identity);
s.Play(Sound);
2023-10-27 18:07:01 +03:00
_played = true;
2023-10-09 16:51:27 +03:00
}
}