Exemplo n.º 1
0
 public void Log(KZNotice notice)
 {
     WriteLine(notice.data);
 }
Exemplo n.º 2
0
    private void PostNotice(KZNotice aNotice)
    {
        if(aNotice.name == null || aNotice.name == "")
        {
            Debug.Log("Null name sent to PostNotice.");
            return;
        }

        List<Object> notifyList = null;
        if(registrations.ContainsKey(aNotice.name)) {
            notifyList = registrations[aNotice.name];
        }

        if(notifyList == null)
        {
            Debug.Log("No observer found for notice \""+aNotice.name+"\"");
            return;
        }

        List<Object> observersToRemove = new List<Object>();
        List<Object> receiver = new List<Object>();

        for(int i=0; i<notifyList.Count; i++)
        {
            Object observer=notifyList[i];
            if(observer == null) {
                observersToRemove.Add(observer);
                //since the observer may be destroyed after subscription
            } else {
                if(DEBUG) receiver.Add(observer);
                KZUtil.Call(observer, aNotice.name, aNotice);
                //Since the target type of SendMessage() is GameObject,
                //when multiple scripts attached to the same GameObject
                //listen to the same event, the event would be sent
                //to the same GameObject several times and produce
                //undesirable results. For fixing the problem, we bypass
                //GameObject, and use reflection to send the event directly
                //to the observing Component.
            }
        }
        if(DEBUG) {
            string list = KZUtil.Join(receiver,
                    o => o.GetType() +
                    ((o.GetType() == typeof(MonoBehaviour))
                    ?
                    "(" + ((MonoBehaviour)o).GetInstanceID() +
                    ") of \"" + ((MonoBehaviour)o).gameObject.name + "\""
                    :
                    "")
                    ,
                    ", ");
            string msg="Sent \""+aNotice.name+"\" to ";
            Debug.Log(msg + list);
        }

        foreach(Object observer in observersToRemove) {
            notifyList.Remove(observer);
        }
    }