63 lines
No EOL
1.8 KiB
C#
Executable file
63 lines
No EOL
1.8 KiB
C#
Executable file
using UnityEngine;
|
|
using UnityEngine.Audio;
|
|
using System.Collections;
|
|
|
|
public class SFX_Dohodyaga : MonoBehaviour
|
|
{
|
|
[Header("Audio Mixer Snapshots")]
|
|
[SerializeField] private AudioMixerSnapshot defaultSnapshot;
|
|
[SerializeField] private AudioMixerSnapshot deathSnapshot;
|
|
[SerializeField] private float transitionDuration = 1.0f;
|
|
|
|
[Header("Visuals")]
|
|
[SerializeField] private Sprite deadSprite;
|
|
[SerializeField] private float fadeDuration = 1.5f;
|
|
|
|
[Header("SFX")]
|
|
[SerializeField] private AudioClip deathSound;
|
|
|
|
private SpriteRenderer _spriteRenderer;
|
|
private AudioSource _sfxSource;
|
|
|
|
private void Awake()
|
|
{
|
|
_spriteRenderer = GetComponent<SpriteRenderer>();
|
|
_sfxSource = GetComponent<AudioSource>() ?? gameObject.AddComponent<AudioSource>();
|
|
|
|
}
|
|
|
|
public void StartFadeOut()
|
|
{
|
|
StartCoroutine(DeathSequence());
|
|
}
|
|
|
|
private IEnumerator DeathSequence()
|
|
{
|
|
if (deathSnapshot != null)
|
|
deathSnapshot.TransitionTo(transitionDuration);
|
|
|
|
if (deadSprite) _spriteRenderer.sprite = deadSprite;
|
|
if (deathSound) _sfxSource.PlayOneShot(deathSound);
|
|
|
|
float elapsed = 0;
|
|
Color startColor = _spriteRenderer.color;
|
|
while (elapsed < fadeDuration)
|
|
{
|
|
elapsed += Time.deltaTime;
|
|
_spriteRenderer.color = new Color(startColor.r, startColor.g, startColor.b, 1f - (elapsed / fadeDuration));
|
|
yield return null;
|
|
}
|
|
|
|
yield return new WaitForSeconds(0.5f);
|
|
|
|
|
|
if (defaultSnapshot != null)
|
|
defaultSnapshot.TransitionTo(transitionDuration);
|
|
|
|
|
|
GameObject objToDestroy = (transform.parent != null && transform.parent.name.Contains("Dialogue"))
|
|
? transform.parent.gameObject : gameObject;
|
|
|
|
Destroy(objToDestroy);
|
|
}
|
|
} |