예제 #1
0
    void UnloadAssetsFromBuffer()
    {
        float unloadedClipsSize = 0;

        List <string> activeClipNames = SoundsManager.GetUsedAudioClipNames();

        BufferItem[] cachedClipsItems = new BufferItem[cachedClips.Values.Count];
        cachedClips.Values.CopyTo(cachedClipsItems, 0);

        List <BufferItem> bufferItems = new List <BufferItem>(cachedClipsItems);

        bufferItems.Sort((a, b) => a.LastLoadTime.CompareTo(b.LastLoadTime));

        List <string> audioClipNamesToUnload = new List <string>();

        for (int i = 0; i < bufferItems.Count; i++)
        {
            BufferItem item = bufferItems[i];

            if (!activeClipNames.Contains(item.Clip.name))
            {
                audioClipNamesToUnload.Add(item.Clip.name);
                unloadedClipsSize += item.MemorySize;

                if (unloadedClipsSize > CLIPS_SIZE_TO_UNLOAD)
                {
                    break;
                }
            }
        }

        cachedClipsItems = null;
        bufferItems.Clear();

        float delay = 0;

        foreach (string clipName in audioClipNamesToUnload)
        {
            UnloadClip(clipName, delay);
            delay += DELAY_TO_UNLOAD_CLIP;
        }

        IsBufferUnloadingAssets = false;
    }