我會使用這個類的是因為我當前的項目的戰斗要單獨移植出來,作為戰斗服。現在為了還原Unity的生命周期里的Update,就使用了這個Timer類。
使用很簡單,實例化一個Timer,而參數就是每次調用的間隔時間,可以作為每次update的時間,也就是幀率。Timer timer = new Timer(100);參數的單位是毫秒。
然后綁定方法即可,timer.Elapsed += new System.Timers.ElapsedEventHandler(Update); 並且設置一直循環調用,timer.AutoReset = true;如果設為false,則只
調用一次。然后在適當的時候啟動,timer.Start();
簡單代碼總結:
timer.Elapsed += new System.Timers.ElapsedEventHandler(Update);
timer.AutoReset = true;
timer.Start();