Publisher

Publisher

using UnityEngine;
using System.Collections;

public class Publisher : MonoBehaviour {

    public delegate void EventHandler (bool messageSent);
    public static event EventHandler sendMessage;

    void OnGUI()
    {
        if(GUI.Button(new Rect(Screen.width / 8,50,150,100), "Scale Up"))
        {
        if(sendMessage != null)
            {
            sendMessage(true);
            }
        }
        if (GUI.Button(new Rect(Screen.width / 1.5f,50,150,100), "Scale Down"))
            {
            sendMessage(false);
            }
    }
}

Subscriber

using UnityEngine;
using System.Collections;

public class Subscriber : MonoBehaviour {

    public float currentScale = .5f;

    // Use this for initialization
    void Start () 
    {
        Publisher.sendMessage += this.SendAMessage;
    }
    
    // Update is called once per frame
    void OnDisable () 
    {
        Publisher.sendMessage -= this.SendAMessage;
    }

    void SendAMessage(bool sendAMessage)
    {
        if(sendAMessage)
        {
            transform.localScale = new Vector3(currentScale + .5f, currentScale + .5f, currentScale + .5f);
            currentScale += .5f;
            Debug.Log ("Scaled!");
        }
        else if (!sendAMessage && currentScale >= 1)
        {
            transform.localScale = new Vector3(currentScale - .5f, currentScale - .5f, currentScale - .5f);
            currentScale -= .5f;

        }
    }
}

Leave a Reply