diff --git a/Assets/Scripts/OpenTS2/Audio/TSAudioSource.cs b/Assets/Scripts/OpenTS2/Audio/TSAudioSource.cs index 4b34a6d2..f98bf54a 100644 --- a/Assets/Scripts/OpenTS2/Audio/TSAudioSource.cs +++ b/Assets/Scripts/OpenTS2/Audio/TSAudioSource.cs @@ -6,6 +6,7 @@ using System.Collections; using System.Collections.Generic; using System.IO; +using System.Threading.Tasks; using UnityEngine; /// @@ -34,7 +35,7 @@ public AudioAsset Audio private float _timeAudioSourcePlaying = 0f; private bool _audioClipPlaying = false; private bool _paused = false; - private AsyncContentManager _asyncContentLoader; + private ContentManager _contentManager; public void Pause() { @@ -61,18 +62,13 @@ public void Play() public void PlayAsync(ResourceKey audioResourceKey) { CleanUp(); - StartCoroutine(PlayAsyncInternal(audioResourceKey)); - } - - private IEnumerator PlayAsyncInternal(ResourceKey key) - { - var audioRequest = _asyncContentLoader.RequestAsset(key); - while (!audioRequest.Finished) - yield return null; - if (audioRequest.Result == AsyncContentManager.Results.Success) - PlayInternal(audioRequest.Asset as AudioAsset); - else - Stop(); + Task.Run(() => + { + _audio = _contentManager.GetAsset(audioResourceKey); + }).ContinueWith(task => + { + PlayInternal(_audio); + }, TaskScheduler.FromCurrentSynchronizationContext()); } private void PlayInternal(AudioAsset asset) @@ -93,7 +89,7 @@ public void Stop() private void Awake() { - _asyncContentLoader = AsyncContentManager.Instance; + _contentManager = ContentManager.Instance; _audioSource = GetComponent(); if (_audioSource == null) { diff --git a/Assets/Scripts/OpenTS2/Content/AsyncContentManager.cs b/Assets/Scripts/OpenTS2/Content/AsyncContentManager.cs deleted file mode 100644 index 827bc433..00000000 --- a/Assets/Scripts/OpenTS2/Content/AsyncContentManager.cs +++ /dev/null @@ -1,72 +0,0 @@ -using OpenTS2.Common; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading; -using System.Threading.Tasks; - -namespace OpenTS2.Content -{ - public class AsyncContentManager - { - public class AssetRequest - { - public bool Finished = false; - public Results Result; - public AbstractAsset Asset; - } - public enum Results - { - Success, - Failed - } - public static AsyncContentManager Instance { get; private set; } - private Thread _thread; - private Queue _requests = new Queue(); - private ContentManager _contentManager; - public AsyncContentManager() - { - Instance = this; - _contentManager = ContentManager.Instance; - _thread = new Thread(new ThreadStart(ThreadLoop)); - _thread.IsBackground = true; - _thread.Name = "AsyncContentLoader"; - _thread.Start(); - } - - public AssetRequest RequestAsset(ResourceKey key) - { - var requestResult = new AssetRequest(); - _requests.Enqueue(() => - { - var asset = _contentManager.GetAsset(key); - requestResult.Asset = asset; - requestResult.Finished = true; - requestResult.Result = asset != null ? Results.Success : Results.Failed; - }); - _thread.Interrupt(); - return requestResult; - } - - private void ThreadLoop() - { - while (true) - { - if (_requests.Count > 0) - { - var request = _requests.Dequeue(); - request.Invoke(); - } - else - { - try - { - Thread.Sleep(Timeout.Infinite); - } - catch (ThreadInterruptedException) { } - } - } - } - } -} diff --git a/Assets/Scripts/OpenTS2/Content/AsyncContentManager.cs.meta b/Assets/Scripts/OpenTS2/Content/AsyncContentManager.cs.meta deleted file mode 100644 index e952418d..00000000 --- a/Assets/Scripts/OpenTS2/Content/AsyncContentManager.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: fe0727e0583480d4bb881ac3fc7c2b79 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/OpenTS2/Engine/Core.cs b/Assets/Scripts/OpenTS2/Engine/Core.cs index df6a1583..9e9be883 100644 --- a/Assets/Scripts/OpenTS2/Engine/Core.cs +++ b/Assets/Scripts/OpenTS2/Engine/Core.cs @@ -44,7 +44,6 @@ public static void InitializeCore() var nhoodManager = new NeighborhoodManager(); var casController = new CASManager(); var lotManger = new LotManager(); - var asyncContentLoader = new AsyncContentManager(); TerrainManager.Initialize(); MaterialManager.Initialize();