【Unity技巧】统一管理回调函数——观察者模式
这次的内容有点类似设计模式里的观察者模式。但是和常规意义上的观察者模式也不是完全一致,所以各位就不要咬文嚼字啦!咦?设计模式?!不懂!没关系,说不定你以前就用过。
using UnityEngine;using System.Collections;public class TimerSample : TimerObserverOrSubject {private TimerController m_TimerCtr = null;private bool m_IsCanDisplay = false;private string m_DisplayContent = "Hello, candycat!";// Use this for initializationvoid Start () {m_TimerCtr = Singleton.getInstance("TimerController") as TimerController;//m_TimerCtr.SetTimer(this, Display, m_DisplayContent, 5);m_TimerCtr.SetTimer(this, Display, null, this, IsCanDisplay, null);StartCoroutine(DelayDisplay());}void Display(object arg) {if (arg == null) {Debug.Log(m_DisplayContent);} else {string content = arg as string;Debug.Log(content);}}bool IsCanDisplay(object arg) {return m_IsCanDisplay;}IEnumerator DelayDisplay() {yield return new WaitForSeconds(5.0f);m_IsCanDisplay = true;}// Update is called once per framevoid Update () {}}