Skip to content

Commit 660550b

Browse files
committed
Changes towards supporting multiple threads properly
1 parent 3c82f8c commit 660550b

14 files changed

+705
-282
lines changed

UnityProject/Assets/AsyncUtil/Source/AsyncMonoBehaviourEvents.cs

Lines changed: 0 additions & 32 deletions
This file was deleted.

UnityProject/Assets/AsyncUtil/Source/IEnumeratorAwaitExtensions.cs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Runtime.CompilerServices;
55
using System.Threading.Tasks;
66
using UnityEngine;
7+
using UnityAsyncAwaitUtil;
78

89
// We could just add a generic GetAwaiter to YieldInstruction and CustomYieldInstruction
910
// but instead we add specific methods to each derived class to allow for return values
@@ -20,6 +21,11 @@ public static TaskAwaiter<object> GetAwaiter(this WaitForSeconds instruction)
2021
return GetAwaiterReturnNull(instruction);
2122
}
2223

24+
public static TaskAwaiter<object> GetAwaiter(this WaitForUpdate instruction)
25+
{
26+
return GetAwaiterReturnNull(instruction);
27+
}
28+
2329
public static TaskAwaiter<object> GetAwaiter(this WaitForEndOfFrame instruction)
2430
{
2531
return GetAwaiterReturnNull(instruction);
@@ -48,7 +54,7 @@ public static TaskAwaiter<object> GetAwaiter(this WaitWhile instruction)
4854
public static TaskAwaiter<UnityEngine.Object> GetAwaiter(this ResourceRequest instruction)
4955
{
5056
var tcs = new TaskCompletionSource<UnityEngine.Object>();
51-
AsyncMonoBehaviourEvents.Instance.StartCoroutine(
57+
AsyncCoroutineRunner.Instance.StartCoroutine(
5258
InstructionWrappers.ResourceRequest(tcs, instruction));
5359
return tcs.Task.GetAwaiter();
5460
}
@@ -67,15 +73,15 @@ public static TaskAwaiter<WWW> GetAwaiter(this WWW instruction)
6773
public static TaskAwaiter<AssetBundle> GetAwaiter(this AssetBundleCreateRequest instruction)
6874
{
6975
var tcs = new TaskCompletionSource<AssetBundle>();
70-
AsyncMonoBehaviourEvents.Instance.StartCoroutine(
76+
AsyncCoroutineRunner.Instance.StartCoroutine(
7177
InstructionWrappers.AssetBundleCreateRequest(tcs, instruction));
7278
return tcs.Task.GetAwaiter();
7379
}
7480

7581
public static TaskAwaiter<UnityEngine.Object> GetAwaiter(this AssetBundleRequest instruction)
7682
{
7783
var tcs = new TaskCompletionSource<UnityEngine.Object>();
78-
AsyncMonoBehaviourEvents.Instance.StartCoroutine(
84+
AsyncCoroutineRunner.Instance.StartCoroutine(
7985
InstructionWrappers.AssetBundleRequest(tcs, instruction));
8086
return tcs.Task.GetAwaiter();
8187
}
@@ -84,7 +90,7 @@ public static TaskAwaiter<object> GetAwaiter(this IEnumerator coroutine)
8490
{
8591
var tcs = new TaskCompletionSource<object>();
8692
var wrapper = new CoroutineWrapper(coroutine, tcs);
87-
AsyncMonoBehaviourEvents.Instance.StartCoroutine(wrapper.Run());
93+
AsyncCoroutineRunner.Instance.StartCoroutine(wrapper.Run());
8894
return tcs.Task.GetAwaiter();
8995
}
9096

@@ -95,15 +101,15 @@ public static TaskAwaiter<object> GetAwaiter(this IEnumerator coroutine)
95101
static TaskAwaiter<object> GetAwaiterReturnNull(object instruction)
96102
{
97103
var tcs = new TaskCompletionSource<object>();
98-
AsyncMonoBehaviourEvents.Instance.StartCoroutine(
104+
AsyncCoroutineRunner.Instance.StartCoroutine(
99105
InstructionWrappers.ReturnNullValue(tcs, instruction));
100106
return tcs.Task.GetAwaiter();
101107
}
102108

103109
static TaskAwaiter<T> GetAwaiterReturnSelf<T>(T instruction)
104110
{
105111
var tcs = new TaskCompletionSource<T>();
106-
AsyncMonoBehaviourEvents.Instance.StartCoroutine(
112+
AsyncCoroutineRunner.Instance.StartCoroutine(
107113
InstructionWrappers.ReturnSelf(tcs, instruction));
108114
return tcs.Task.GetAwaiter();
109115
}

UnityProject/Assets/AsyncUtil/Source/Internal.meta

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using System;
2+
using System.Collections;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using UnityEngine;
6+
7+
namespace UnityAsyncAwaitUtil
8+
{
9+
public class AsyncCoroutineRunner : MonoBehaviour
10+
{
11+
static AsyncCoroutineRunner _instance;
12+
13+
public static AsyncCoroutineRunner Instance
14+
{
15+
get
16+
{
17+
if (_instance == null)
18+
{
19+
_instance = new GameObject("AsyncCoroutineRunner")
20+
.AddComponent<AsyncCoroutineRunner>();
21+
}
22+
23+
return _instance;
24+
}
25+
}
26+
27+
void Awake()
28+
{
29+
// Don't show in scene hierarchy
30+
gameObject.hideFlags = HideFlags.HideAndDontSave;
31+
32+
DontDestroyOnLoad(gameObject);
33+
}
34+
}
35+
}

UnityProject/Assets/AsyncUtil/Source/AsyncMonoBehaviourEvents.cs.meta renamed to UnityProject/Assets/AsyncUtil/Source/Internal/AsyncCoroutineRunner.cs.meta

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Diagnostics;
4+
using System.Runtime.CompilerServices;
5+
using System.Threading.Tasks;
6+
using UnityEngine;
7+
8+
// This can be used as a way to return to the main unity thread when using multiple threads
9+
// with async methods
10+
public class WaitForUpdate : CustomYieldInstruction
11+
{
12+
public override bool keepWaiting
13+
{
14+
get { return false; }
15+
}
16+
}

UnityProject/Assets/AsyncUtil/Source/WaitForUpdate.cs.meta

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

UnityProject/Assets/AsyncUtil/Temp.meta

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using System;
2+
using System.Collections;
3+
using System.Collections.Generic;
4+
using System.Threading;
5+
using System.Threading.Tasks;
6+
using UnityEngine;
7+
8+
public class AsyncExample : MonoBehaviour
9+
{
10+
async void Start()
11+
{
12+
Debug.Log("Thread: " + Thread.CurrentThread.ManagedThreadId);
13+
14+
await Task.Delay(TimeSpan.FromSeconds(1.0f)).ConfigureAwait(false);
15+
16+
Debug.Log("Thread: " + Thread.CurrentThread.ManagedThreadId);
17+
}
18+
}

UnityProject/Assets/AsyncUtil/Temp/AsyncExample.cs.meta

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)