主要問題:
不確定后臺發生了什么,但顯然每當await Task.Delay(1000);
呼叫該線路時,Unity 都會變得無回應/凍結。
錯誤訊息: 嘗試檢查來自統一分析器的回呼,但每當觸發相關行時,統一會立即變得無回應并且不允許任何互動(也沒有給出回呼/錯誤訊息)。
目標:
這里的目標是簡單地呼叫tryActivities[0]
then,等待 1 秒然后回傳 true,并列印“Fish successful!”,沒有任何崩潰。
懷疑:
我有一個懷疑是這條線tryActivities.Add(new Func<bool>(() => fs(currentSlot.basicData.typeID).Result));
需要某種等待?雖然我也不確定如何實作它。
庫存盤點;(庫存.cs)
List<Func<bool>> tryActivities = new List<Func<bool>>();
public delegate Task<bool> Fish(int ID); Fish fs;
void Start()
{
fs = new Fish(activities.Fish);
tryActivities.Add(new Func<bool>(() => fs(currentSlot.basicData.typeID).Result));//await? how?
}
public void Interact()//called when a button is pressed
{
if (TryPlaceOrUse()) print("Fished successfully!");
else print("Fishing failed");
}
bool TryPlaceOrUse()
{
if (tryActivities[(int)currentSlot.basicData.myActivity]())//tryActivities[0]
return true;
return false;
}
活動活動;(活動.cs)
public async Task<bool> Fish(int ID)
{
await Task.Delay(1000);//crashes here
return true;
}
uj5u.com熱心網友回復:
這可能會做你想要的。這就是你使用async
Func
s 串列的方式,它實際上只是一個Func
回傳Task
s 的s 串列bool
。
public class Program
{
public delegate Task<bool> Fish(int ID);
Fish fs;
List<Func<Task<bool>>> tryActivities = new List<Func<Task<bool>>>();
public async static Task Main()
{
Console.WriteLine("Hello, World!");
Program program = new Program();
program.Start();
await program.Interact();
}
void Start()
{
fs = new Fish(Activities.Fish);
tryActivities.Add(new Func<Task<bool>>(async () => await fs(4)));//await? how? like this :)
}
public async Task Interact() //called when a button is pressed
{
if (await TryPlaceOrUse())
Console.WriteLine("Fished successfully!");
else
Console.WriteLine("Fishing failed");
}
async Task<bool> TryPlaceOrUse()
{
if (await tryActivities[0]()) //tryActivities[0]
return true;
return false;
}
}
public class Activities
{
public static async Task<bool> Fish(int ID)
{
await Task.Delay(1000);//does not crash here
return true;
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/471741.html