-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
async content loader, load music async
- Loading branch information
1 parent
f749586
commit 5396d7e
Showing
6 changed files
with
113 additions
and
7 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
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
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,64 @@ | ||
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 Request | ||
{ | ||
public bool Finished = false; | ||
public Results Result; | ||
public ResourceKey Key; | ||
public AbstractAsset Asset; | ||
} | ||
public enum Results | ||
{ | ||
Success, | ||
Failed | ||
} | ||
public static AsyncContentManager Instance { get; private set; } | ||
private Thread _thread; | ||
private Queue<Request> _requests = new Queue<Request>(); | ||
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 Request RequestAsset(ResourceKey key) | ||
{ | ||
var request = new Request(); | ||
request.Key = key; | ||
_requests.Enqueue(request); | ||
return request; | ||
} | ||
|
||
private void ThreadLoop() | ||
{ | ||
while (true) | ||
{ | ||
if (_requests.Count > 0) | ||
{ | ||
var request = _requests.Peek(); | ||
request.Asset = _contentManager.GetAsset(request.Key); | ||
request.Result = request.Asset != null ? Results.Success : Results.Failed; | ||
request.Finished = true; | ||
_requests.Dequeue(); | ||
} | ||
else | ||
Thread.Sleep(1); | ||
} | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
Assets/Scripts/OpenTS2/Content/AsyncContentManager.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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