微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

列出、排序音频源

如何解决列出、排序音频源

下面是我编写的脚本,用于填充自动武器上的敌人音频源列表,然后如果它们正在播放,则将它们放入另一个列表中,并按与玩家的距离对它们进行排序,以便只播放最接近的 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);
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。