KITASENJU DESIGN BLOG

memo, html, javascript, unity

EventDispatcher使い方

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using com.ootii.Messages;

public class EventDispatcher : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        MessageDispatcher.AddListener("MyEvent", MsgHandler);
        Invoke("hoge",1f);
    }

    void hoge(){

        //Object obj = new Object();
        //obj.color = 0;

        MessageDispatcher.SendMessage(
            gameObject, "MyEvent", "hogehoge", 0
        );

    }

    private void MsgHandler(IMessage incomingMessage)
    {
        Debug.Log("a " + incomingMessage.Data);

        // When a message of type "Color" is recieved, the message's data is expected to be a reference to a material.
        // Set this objects material to the material from incomingMessage.Data
        //gameObject.GetComponent<MeshRenderer>().material = (Material)incomingMessage.Data;
        //Debug.Log("Changed to " + (Material)incomingMessage.Data);

        // While not required, this is a good way to be tidy
        // and let others know that the message has been handled
        //incomingMessage.IsHandled = true;
    }


}

"FOOTER"