-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Resolved: 1. Large-scale refactoring. Since int was used as the key for dictionaries, which can act as an array index, all dictionaries in the EazySoundManager class have been replaced with the AudioList class, this will avoid the overhead associated with dictionaries and gives potential scope for optimization. AudioList, in turn, is an implementation of the standard List<Audio>, but internally it uses a regular array, all elements of which are static and will never be moved, as a result of which it is guaranteed that one unique int type id corresponds to one element at the same time. 2. Now in the EazySoundSystem class, when deleting Audio from AudioList, the queue is not used as it was with the dictionary, this avoids unnecessary costs. 3. Fixed an error when the GetAudioFast() method in the EazySoundManager class did not return an element with index 0. 4. Fixed an error when the sound did not continue after changing the scene when the corresponding setting was enabled. 5. Fixed an error when in the Audio class a clip for AudioSource was assigned immediately when creating an instance of the class. 6. The READMI has been updated.
- Loading branch information
1 parent
e95ca66
commit 5756160
Showing
7 changed files
with
367 additions
and
118 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
261 changes: 261 additions & 0 deletions
261
Assets/Lineri/SoundSystem/Eazy Sound Manager/Scripts/AudioList.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,261 @@ | ||
using System; | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using UnityEngine; | ||
|
||
namespace Lineri.SoundSystem | ||
{ | ||
public class ListAudio : ICollection, IEnumerable<Audio> | ||
{ | ||
public int Count => _array.Length; | ||
public bool IsSynchronized => _array.IsSynchronized; | ||
public object SyncRoot => _array.SyncRoot; | ||
public bool IsFixedSize => false; | ||
public bool IsReadOnly => false; | ||
|
||
private int _capacity = 64; | ||
/// <summary> | ||
/// Do not specify less than 8. Workability is not guaranteed if the number is less than 8. | ||
/// </summary> | ||
public int Capacity | ||
{ | ||
get => _capacity; | ||
set | ||
{ | ||
_capacity = value; | ||
RecreateArray(); | ||
} | ||
} | ||
|
||
private Audio[] _array; | ||
private int _lastIndex = 0; | ||
|
||
#region Constructors | ||
public ListAudio() | ||
{ | ||
_array = new Audio[_capacity]; | ||
} | ||
|
||
public ListAudio(int capacity) | ||
{ | ||
_array = new Audio[capacity]; | ||
} | ||
|
||
public ListAudio(Audio[] array) | ||
{ | ||
_array = (Audio[])array.Clone(); | ||
Capacity = array.Length < Capacity ? Capacity : array.Length; | ||
} | ||
#endregion | ||
|
||
public Audio[] ToArray() | ||
{ | ||
return (Audio[])_array.Clone(); | ||
} | ||
|
||
public void CopyTo(Array array, int index) | ||
{ | ||
_array.CopyTo(array, index); | ||
} | ||
|
||
#region Enumerators | ||
IEnumerator IEnumerable.GetEnumerator() | ||
{ | ||
return _array.GetEnumerator(); | ||
} | ||
|
||
public IEnumerator<Audio> GetEnumerator() | ||
{ | ||
int length = _array.Length; | ||
for (int i = 0; i < length; i++) | ||
{ | ||
if (_array[i] != null) yield return _array[i]; | ||
} | ||
} | ||
#endregion | ||
|
||
public Audio this[int index] | ||
{ | ||
get | ||
{ | ||
return _array[index]; | ||
} | ||
set | ||
{ | ||
_array[index] = value; | ||
} | ||
} | ||
|
||
public void Remove(int index) | ||
{ | ||
_array[index] = null; | ||
} | ||
|
||
public void Clear() | ||
{ | ||
for (int i = _array.Length - 1; i >= 0; i--) | ||
{ | ||
_array[i] = null; | ||
} | ||
} | ||
|
||
public bool Contains(int index) | ||
{ | ||
if (index < 0 || index >= _array.Length) return false; | ||
return _array[index] != null; | ||
} | ||
|
||
#region IndexOf | ||
public int IndexOf(Audio value) | ||
{ | ||
for (int i = _array.Length - 1; i >= 7; i -= 8) | ||
{ | ||
if (_array[i] == value) return i; | ||
else if (_array[i - 1] == value) return i - 1; | ||
else if (_array[i - 2] == value) return i - 2; | ||
else if (_array[i - 3] == value) return i - 3; | ||
else if (_array[i - 4] == value) return i - 4; | ||
else if (_array[i - 5] == value) return i - 5; | ||
else if (_array[i - 6] == value) return i - 6; | ||
else if (_array[i - 7] == value) return i - 7; | ||
} | ||
|
||
if (_array[0] == value) return 0; | ||
else if (_array[1] == value) return 1; | ||
else if (_array[2] == value) return 2; | ||
else if (_array[3] == value) return 3; | ||
else if (_array[4] == value) return 4; | ||
else if (_array[5] == value) return 5; | ||
else if (_array[6] == value) return 6; | ||
else if (_array[7] == value) return 7; | ||
|
||
return -1; | ||
} | ||
|
||
public int IndexOf(Predicate<Audio> predicate) | ||
{ | ||
for (int i = _array.Length - 1; i >= 0; i--) | ||
{ | ||
if (predicate(_array[i])) return i; | ||
} | ||
|
||
return -1; | ||
} | ||
|
||
public int IndexOf(AudioClip value) | ||
{ | ||
for (int i = _array.Length - 1; i >= 0; i--) | ||
{ | ||
if (_array[i]?.Clip == value) return i; | ||
} | ||
|
||
return -1; | ||
} | ||
#endregion | ||
|
||
public int Add(Audio audio) | ||
{ | ||
int index = GetFreeIndex(); | ||
_array[index] = audio; | ||
return index; | ||
} | ||
|
||
public int GetFreeIndex(bool reset = true) | ||
{ | ||
int length = _array.Length; | ||
|
||
if (_lastIndex >= length - 1) | ||
{ | ||
if (reset) | ||
{ | ||
ResetIndex(); | ||
} | ||
else | ||
{ | ||
Capacity *= 2; | ||
} | ||
} | ||
|
||
if (_array[_lastIndex] == null) | ||
{ | ||
return _lastIndex; | ||
} | ||
else | ||
{ | ||
_lastIndex++; | ||
|
||
if (_array[_lastIndex] == null) | ||
{ | ||
return _lastIndex; | ||
} | ||
} | ||
|
||
for (int i = _lastIndex; i < length; i++) | ||
{ | ||
if (_array[i] != null) | ||
{ | ||
_lastIndex++; | ||
} | ||
else | ||
{ | ||
return i; | ||
} | ||
} | ||
|
||
return GetFreeIndex(false); | ||
} | ||
|
||
public int GetCountNoNull() | ||
{ | ||
int r0 = 0; | ||
int r1 = 0; | ||
int r2 = 0; | ||
int r3 = 0; | ||
int r4 = 0; | ||
int r5 = 0; | ||
int r6 = 0; | ||
int r7 = 0; | ||
|
||
int i = _array.Length - 1; | ||
for (; i >= 7; i -= 8) | ||
{ | ||
if (_array[i] != null) r0++; | ||
if (_array[i - 1] != null) r1++; | ||
if (_array[i - 2] != null) r2++; | ||
if (_array[i - 3] != null) r3++; | ||
if (_array[i - 4] != null) r4++; | ||
if (_array[i - 5] != null) r5++; | ||
if (_array[i - 6] != null) r6++; | ||
if (_array[i - 7] != null) r7++; | ||
} | ||
|
||
if (i > 0 && _array[0] != null) r0++; | ||
if (i > 1 && _array[1] != null) r1++; | ||
if (i > 2 && _array[2] != null) r2++; | ||
if (i > 3 && _array[3] != null) r3++; | ||
if (i > 4 && _array[4] != null) r4++; | ||
if (i > 5 && _array[5] != null) r5++; | ||
if (i > 6 && _array[6] != null) r6++; | ||
if (i > 7 && _array[7] != null) r7++; | ||
|
||
return r0+r1+r2+r3+r4+r5+r6+r7; | ||
} | ||
|
||
private void ResetIndex() | ||
{ | ||
_lastIndex = 0; | ||
} | ||
|
||
private void RecreateArray() | ||
{ | ||
Audio[] result = new Audio[_capacity]; | ||
|
||
for (int i = _array.Length - 1; i >= 0; i--) | ||
{ | ||
result[i] = _array[i]; | ||
} | ||
|
||
_array = result; | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
Assets/Lineri/SoundSystem/Eazy Sound Manager/Scripts/AudioList.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.