Skip to content

Commit fd26c84

Browse files
Merge pull request #1 from ChartBoost/release/Mediation/4.9.0
Chartboost Unity Threading 1.0.0 into Main
2 parents 3d459f3 + 2d3d548 commit fd26c84

32 files changed

Lines changed: 621 additions & 1 deletion

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Changelog
2+
All notable changes to this project will be documented in this file using the standards as defined at [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0).
3+
4+
### Version 1.0.0 *(2024-01-26)*
5+
Improvements:
6+
- Added `MainThreadDispatcher` class.
7+
- Added `TaskExtensionTest` class.
8+
- Structured project as a UPM compatible package.

CHANGELOG.md.meta

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<package>
3+
<metadata>
4+
<id>Chartboost.CSharp.Threading.Unity</id>
5+
<version>1.0.0</version>
6+
<title>Chartboost Threading Utilities for Unity</title>
7+
<description>Reusable Threading Utilities for Chartboost's Unity Projects</description>
8+
<authors>Chartboost</authors>
9+
<owners>Chartboost</owners>
10+
<license type="file">LICENSE.md</license>
11+
<readme>README.md</readme>
12+
<copyright>Copyright 2024</copyright>
13+
<tags>Chartboost, Ads, Mediation, Unity, cs</tags>
14+
<repository type="git" url="https://github.com/ChartBoost/chartboost-unity-threading"/>
15+
<dependencies>
16+
<dependency id="Chartboost.CSharp.Utilities.Unity" version="1.0.0" />
17+
</dependencies>
18+
</metadata>
19+
</package>

Chartboost.CSharp.Threading.Unity.nuspec.meta

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

LICENSE.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
MIT License
2+
3+
Copyright 2023 Chartboost Inc.
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6+
7+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8+
9+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

LICENSE.md.meta

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

README.md

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,66 @@
1-
# chartboost-unity-threading
1+
# Chartboost MainThreadDispatcher Plugin for Unity
2+
A way of dispatching functions to the main thread in Unity projects. Useful for functions that Unity limits to the main thread from different threads.
3+
4+
# Installation
5+
This package is meant to be a dependency for other Chartboost Packages;however, if you wish to use it by itself, it can be installed through UPM & NuGet as follows:
6+
7+
```json
8+
"dependencies": {
9+
"com.chartboost.unity.threading": "1.0.0",
10+
...
11+
},
12+
"scopedRegistries": [
13+
{
14+
"name": "NpmJS",
15+
"url": "https://registry.npmjs.org",
16+
"scopes": [
17+
"com.chartboost"
18+
]
19+
}
20+
]
21+
```
22+
23+
# Usage
24+
25+
## Simple Actions
26+
Utilize the following methods to execute calls on the main thread:
27+
28+
```csharp
29+
void TestAction(object state){
30+
//Execute logic on main thread
31+
Debug.Log("This is called in the main thread")
32+
}
33+
34+
// Synchronous; blocks until the callback completes
35+
MainThreadDispatcher.Send(TestAction);
36+
37+
// Asynchronous; send and forget
38+
MainThreadDispatcher.Post(TestAction)
39+
```
40+
41+
## Tasks
42+
Taks can be utilized in Unity. However, if they contain code that must run on the Unity main thread, the Task too should also be run in the main thread. Use the following:
43+
44+
```csharp
45+
MainThreadDispatcher.MainThreadTask(async () =>{
46+
// Mostly useful when calling task initially from outside of the Unity environment
47+
await myTask();
48+
});
49+
```
50+
51+
## Task Continuations
52+
Task continuations are useful when trying to call asynchronous code from a synchronous environment. The following examples represent the same logic.
53+
54+
```csharp
55+
private void MySyncrhonousMethod(){
56+
MyAsynchornousTask().ContinueWithOnMainThread(taskContinuationResultTask => {
57+
// perform any continuation logic here.
58+
Debug.Log("My task finished!")
59+
});
60+
}
61+
62+
private async void MyAsyncrhonousMethod(){
63+
var taskResult = awat MyAsynchronousTask();
64+
Debug.Log("My task finished!")
65+
}
66+
```

README.md.meta

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

Runtime.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
using System;
2+
using System.Runtime.CompilerServices;
3+
using System.Threading.Tasks;
4+
using UnityEngine;
5+
6+
namespace Chartboost
7+
{
8+
public class AwaitableAndroidJavaProxy<TResult> : AndroidJavaProxy
9+
{
10+
public TaskAwaiter<TResult> GetAwaiter()
11+
{
12+
if (_taskCompletionSource != null)
13+
return _taskCompletionSource.Task.GetAwaiter();
14+
15+
_taskCompletionSource = new TaskCompletionSource<TResult>();
16+
17+
if (_isComplete)
18+
_setResult();
19+
else
20+
DidComplete += result => _setResult();
21+
22+
return _taskCompletionSource.Task.GetAwaiter();
23+
}
24+
25+
protected AwaitableAndroidJavaProxy(string nativeInterface) : base(nativeInterface) { }
26+
27+
protected void _complete(TResult result)
28+
{
29+
if (_isComplete)
30+
return;
31+
32+
_result = result;
33+
var toComplete = DidComplete;
34+
DidComplete = null;
35+
_isComplete = true;
36+
toComplete?.Invoke(_result);
37+
}
38+
39+
private void _setResult()
40+
{
41+
try
42+
{
43+
_taskCompletionSource.TrySetResult(_result);
44+
}
45+
catch (ObjectDisposedException e)
46+
{
47+
Debug.Log(e.Message);
48+
}
49+
}
50+
51+
private TaskCompletionSource<TResult> _taskCompletionSource;
52+
private event Action<TResult> DidComplete;
53+
private TResult _result;
54+
private bool _isComplete;
55+
}
56+
}

0 commit comments

Comments
 (0)