下面是我为填充自动武器上的敌人音频源列表而编写的脚本,然后如果他们正在播放,则将它们放入另一个列表并按与玩家的距离对它们进行排序,因此只有最近的 xx 播放,以免发出声音太可怕了,我想知道这段代码是好的还是昂贵的开销?谢谢。
[Header("found when the scene is loaded")]
[SerializeField] public Transform m_Cam;
[Header("maximum sound sources to play at once")]
public int _maxWeaponSoundsources;
public int _maxFootstepsSoundsources;
[Header("colours")]
public Color _redColor;
public Color _greenColor;
public Color _whiteColor;
[Header("List of AudioSource's")]
[SerializeField] public List<AudioSource> _audioSources = new List<AudioSource>();
[Header("List of automaitic weapon sound effects playing at the same time")]
[SerializeField] public List<AudioSource> _automaticWeaponAudioSources = new List<AudioSource>();
[Header("List of footstep sound effects playing at the same time")]
[SerializeField] public List<AudioSource> _footStepAudioSources = new List<AudioSource>();
private int MaxWeaponSoundsources;
// -----------------------------------------------------------------
// Name : OnEnable
// Desc :
// -----------------------------------------------------------------
private void OnEnable()
{
MaxWeaponSoundsources = _maxWeaponSoundsources - 1;
if (GameObject.Find("AR Camera") != null)
{
m_Cam = GameObject.Find("AR Camera").GetComponent<Transform>();
return;
}
if (GameObject.Find("Temp Camera") != null)
m_Cam = GameObject.Find("Temp Camera").GetComponent<Transform>();
else if (GameObject.Find("UI Camera") != null)
m_Cam = GameObject.Find("UI Camera").GetComponent<Transform>();
}
// -----------------------------------------------------------------
// Name : Update
// Desc :
// -----------------------------------------------------------------
private void Update()
{
if (AudioManager.instance == null)
return;
if (_audioSources.Count < MaxWeaponSoundsources)
return;
if (_automaticWeaponAudioSources.Count > MaxWeaponSoundsources)
_automaticWeaponAudioSources.Sort(ByDistance);
if (_audioSources.Count > MaxWeaponSoundsources)
{
for (int i = 0; i < _audioSources.Count; i++)
{
if (!_audioSources[i].isPlaying)
_audioSources[i].gameObject.GetComponent<Renderer>().material.color = _whiteColor;
if (_audioSources[i].isPlaying)
{
if (!_automaticWeaponAudioSources.Contains(_audioSources[i]))
_automaticWeaponAudioSources.Add(_audioSources[i]);
for (int l = 0; l < _automaticWeaponAudioSources.Count; l++)
{
_automaticWeaponAudioSources[l].gameObject.GetComponent<Renderer>().material.color = _greenColor;
if (l > MaxWeaponSoundsources)
{
// MUTE the sound source so the weapon can NOT be heard
_automaticWeaponAudioSources[l].mute = true;
_automaticWeaponAudioSources[l].gameObject.GetComponent<Renderer>().material.color = _redColor;
}
else
{
// DONT mute the sound source so the weapon CAN be heard
_automaticWeaponAudioSources[l].mute = false;
_automaticWeaponAudioSources[l].gameObject.GetComponent<Renderer>().material.color = _greenColor;
}
// this MUST be done last in this for() loop, if NOT 'isPlaying' anymore remove from list
if (!_automaticWeaponAudioSources[l].isPlaying)
{
_automaticWeaponAudioSources[l].mute = false;
_automaticWeaponAudioSources.Remove(_automaticWeaponAudioSources[l]);
}
}
}
}
}
}
// -----------------------------------------------------------------
// Name : ByDistance
// Desc :
// -----------------------------------------------------------------
private int ByDistance(AudioSource a, AudioSource b)
{
if (GameSceneManager.instance.player == null)
return 0;
float dstToA = Vector3.Distance(GameSceneManager.instance.player.position, a.transform.position);
float dstToB = Vector3.Distance(GameSceneManager.instance.player.position, b.transform.position);
return dstToA.CompareTo(dstToB);
}