Last active
July 5, 2023 16:20
-
-
Save Dubiy/a761662f95341bc530574dec9f29e02a to your computer and use it in GitHub Desktop.
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
using Jint; | |
using UnityEngine; | |
using System; | |
using System.Threading.Tasks; | |
public class RunJs : MonoBehaviour | |
{ | |
private static Engine engine; | |
void Start() | |
{ | |
engine = new Engine(); | |
engine.SetValue("console",typeof(Debug)); | |
engine.SetValue("setTimeout", new Action<Action<int>, int>(setTimeout)); | |
engine.Execute(@" | |
function delayResolve(delay) { | |
return new Promise(function(resolve) { | |
setTimeout(function() { | |
console.log('onTimeout'); | |
resolve('success'); | |
}, delay); | |
// resolve('works, if not called from C# inoked function') | |
}); | |
} | |
delayResolve(1000) | |
.then(res => { | |
console.log('resolved: ' + res); | |
}) | |
.catch(res => { | |
console.log('rejected: ' + res); | |
}) | |
; | |
"); | |
void setTimeout(Action<int> callback, int delay) | |
{ | |
Task.Delay(delay).ContinueWith(_ => callback(delay)); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment