puerts
puerts copied to clipboard
[Unity] Scripting backend 从Mono改为 IL2CPP 安卓真机上报错
er
ror log | 日志或报错信息
context | 编译/运行环境
mono 是正常的 参考了 Android 使用 il2cpp 编译,启动时崩溃 不生效还是有问题
剪裁导致的。 加到生产列表或者在link.xml中配置
还是一样报错
In short, after appreciation of all the answers and comments, my realization about using GetAwaiter() is: don't do it.
If you need it, this is most likely an indication that your asynchronous design is in badly need of refactoring.
using System.Runtime.CompilerServices;
using System;
using UnityEngine;
using UnityEngine.UI;
using Puerts;
using System.Threading.Tasks;
namespace PuertsTest
{
public class JsCallCs : MonoBehaviour
{
JsEnv jsEnv;
public static async Task MakeATask()
{
long now = DateTimeOffset.Now.ToUnixTimeMilliseconds();
var task = new Task<int>(()=> {
while (true) {
if (DateTimeOffset.Now.ToUnixTimeMilliseconds() - now > 3000) {
break;
}
}
return 30;
});
task.Start();
await task;
}
void Start()
{
jsEnv = new JsEnv();
jsEnv.Eval(@"
const CS = require('csharp');
(async function asd() {
console.log(11);
await taskToPromise(CS.PuertsTest.JsCallCs.MakeATask());
console.log(22);
})();
function taskToPromise(task) {
return new Promise((resolve, reject) => {
task.GetAwaiter().OnCompleted(() => {
let t = task;
task = undefined;
if (t.IsFaulted) {
if (t.Exception) {
if (t.Exception.InnerException) {
reject(t.Exception.InnerException.Message);
} else {
reject(t.Exception.Message);
}
} else {
reject('unknow exception!');
}
} else {
resolve(t.Result);
}
});
});
}
");
}
void OnDestroy()
{
jsEnv.Dispose();
}
}
}
测试了如上代码
在往生成列表添加类时,要填写typeof(System.Runtime.CompilerServices.TaskAwaiter<int>)而不是typeof(System.Runtime.CompilerServices.TaskAwaiter)
测试可用。
另外群上有热心群众提供了此方法,据 @Justin-sky 测试可用。