Simple game control logic Clock and logic clock
Source: http://blog.csdn.net/u010019717
Author: Sun Guangdong time:
In unity, coroutine can provide latency functions. But most of the time we don't want to use it, so we can control the time in Update.
So I encapsulated this class.
To use this clock, first instantiate it, call the Reset function to set the correct time value, and call Update each frame for Update.
Any class to be notified of events must implement the IClockListener interface,
And use the AddListener method to subscribe to events. You can use RemoveListener to remove the listener (very powerful !)
The clock can be paused using the Pause method independently of Time. timeScale (and resume with Unpause)
Using System. collections. generic; namespace Gamelogic {public class Clock {private float time; private int timeInSeconds; private readonly List <IClockListener> listeners; // listener List # regionpublic bool IsPaused {get; private set ;} public bool IsDone {get; private set;} public float Time {get {return time ;}} public int TimeInSeconds {get {return timeInSeconds ;}} # endregion // constructor public Clock () {listeners = new List <IClockListener> (); IsPaused = true; Reset (0);} public void AddClockListener (IClockListener listener) {listeners. add (listener);} public void RemoveClockListener (IClockListener listener) {listeners. remove (listener);} public void Reset (float startTime) {time = startTime; IsDone = false; CheckIfTimeInSecondsChanged ();} public void Unpause () {IsPaused = false ;} public void Pause () {IsPaused = true;} // time public void Update () {if (IsPaused) return; if (IsDone) return; time-= UnityEngine. time. deltaTime; CheckIfTimeInSecondsChanged (); if (time <= 0) {time = 0; IsDone = true; for (int I = 0; I <listeners. count; I ++) {listeners [I]. onTimeOut () ;}}// determines whether a second change has occurred. private void CheckIfTimeInSecondsChanged () {var newTimeInSeonds = (int) time; if (newTimeInSeonds = timeInSeconds) return; timeInSeconds = newTimeInSeonds; for (int I = 0; I <listeners. count; I ++) {listeners [I]. onSecondsChanged (timeInSeconds) ;}}// interface of the clock listener type public interface IClockListener {void OnSecondsChanged (int seconds); void OnTimeOut ();}}
Then I tested it in unity4.6. Countdown:
Using UnityEngine. UI; namespace Gamelogic. examples {public class ClockTest: IClockListener {public Text clockText; public Text messageText; private Clock clock; // clock object public void Start () {Clock = new clock (); Clock. addClockListener (this); // listen to the Reset ();} public void Update () {clock. update ();} public void Pause () {clock. pause ();} public void Unpause () {clock. unpause ();} public void Reset () {clock. reset (5); clock. unpause () ;}# region IClockListener methods // method of implementing the interface public void OnSecondsChanged (int seconds) {clockText. text = clock. timeInSeconds. toString ();} public void OnTimeOut () {messageText. gameObject. setActive (true) ;}# endregion }}
Not bad!