// --------------------------------------------------------------------------------------------------------------------
//
// Copyright (c) VRMADA, All rights reserved.
//
// --------------------------------------------------------------------------------------------------------------------
using UltimateXR.Core.Components;
using UnityEngine;
namespace UltimateXR.Audio
{
///
/// Component that allows to make an start playing at a fixed time or a random time. It
/// can be used for example to scatter multiple environment audio sources around that share the same loop and make them
/// start playing at different times.
///
[RequireComponent(typeof(AudioSource))]
public class UxrAudioSourceStartPos : UxrComponent
{
#region Inspector Properties/Serialized Fields
[SerializeField] private bool _random;
[SerializeField] private float _startTime;
#endregion
#region Public Types & Data
///
/// Gets the target .
///
public AudioSource AudioSourceTarget => GetCachedComponent();
///
/// Gets or sets whether to start playing from a random position in the audio interval.
///
public bool UseRandomStartTime
{
get => _random;
set => _random = value;
}
///
/// Gets or sets the start time position to start playing from. needs to be false to
/// use it.
///
public float StartTime
{
get => _startTime;
set => _startTime = value;
}
#endregion
#region Unity
///
/// Sets the play time using the current parameters.
///
protected override void OnEnable()
{
base.OnEnable();
if (UseRandomStartTime)
{
AudioSourceTarget.time = Random.Range(0.0f, AudioSourceTarget.clip.length);
}
else
{
AudioSourceTarget.time = StartTime;
}
}
#endregion
}
}