我有個問題。我確實有 5 個表對象,它需要 5 個計時器,因此每個表有 1 個計時器我創建了一個名為的腳本 Table.cs表格.cspublic UILabel info_timer = null;我有我的主要腳本讓我們稱之為 Main.cs主文件const float gap = 20.0f;Table[] script_table;bool start_timer = false;int t_no;public static string Ctimer = "";void Update(){ int i = returnTableNo(t_no); if (i != -1 && script_tables != null && script_tables[i] != null) { gap -= Time.deltaTime(); if(gap <= 0.0f) { script_table[i].info_timer = Ctimer = "[808080]0[-]"; } else { script_table[i].info_timer.text = gap.ToString("F0"); } } } //someone will call this to make start_timer to true; void ActivateTimer(){ t.tableno = t_no; start_timer = true; }//this line of code is for the table no == table noint returnTableNo(int table_no){ //table is equavalent to 5 for now for (int i = 0; i < table.Count; i++) { if (t.table_no == table_no) { return i; } } return -1;}現在我很困惑如何將每個計時器放在我擁有的每張桌子上。因為我遇到的情況是,例如表 1 正常運行,只有表 1 計時器將啟動計時器,但發生的情況是,即使我的某些表沒有運行,所有表計時器都在運行。當他的命令被調用時他們正在運行GCommand.start_:這是一張圖紙,以便您可以形象化我正在嘗試做的事情。
2 回答

慕的地6264312
TA貢獻1817條經驗 獲得超6個贊
我為一個創建了一個 Clock 類和一個 ClockManager 類(單例)。這樣我就可以創建計時器的實例,然后這些實例會自動添加到 ClockManager 的列表中。它使用 MonoBehavior 更新(只有一個用于所有事情!這對性能也更好。)為所有計時器調用“滴答”,每個計時器都可以有自己的觸發間隔。無論是 0.0 秒(意味著每次更新)、每 0.1 秒、每 2.0 秒等。當它們被觸發時,它們調用給定的 System.Action,它們將間隔值或它的倍數傳遞給它(如果多個間隔有在一個更新刻度內到達)。
用法很簡單:
Clock idleClock = new Clock (this, DoIdleCheck, 0.5f);
// Parameters: 1. Reference to the object for automatic destruction.
// 2. The target method to invoke. 3. Interval.
代碼太多,無法復制粘貼到這里,因為它與我的其他代碼交織在一起。但是通過這種方式,您可以將任意數量的計時器用于您想要的任何內容,并根據需要暫?;騿铀鼈?。當然,您也可以通過簡單的方式實現它。
- 2 回答
- 0 關注
- 241 瀏覽
添加回答
舉報
0/150
提交
取消