title: In Unity, how does Unity magically call all "Interfaces"?
body: <p>Unity has an &quot;interface&quot;:</p>
  <p><code>IPointerDownHandler</code> (<a href="http://docs.unity3d.com/ScriptReference/EventSystems.IPointerDownHandler.html" rel="noreferrer">doco</a>)</p>
  <p>You simply implement <a href="http://docs.unity3d.com/ScriptReference/UI.Selectable.OnPointerDown.html" rel="noreferrer">OnPointerDown</a> ...</p>
  <pre><code>public class Whoa:MonoBehaviour,IPointerDownHandler
      {
      public void OnPointerDown (PointerEventData data)
          { Debug.Log(&quot;whoa!&quot;); }
      }
  </code></pre>
  <p>and Unity will &quot;magically&quot; call the <code>OnPointerDown</code> in any such MonoBehavior.</p>
  <p><strong>You do NOT have to register them, set an event, nor do anything else</strong>.</p>
  <p>All you do syntactically is add &quot;IPointerDownHandler&quot; and &quot;public void OnPointerDown&quot; to a class, and you can get those messages magically.</p>
  <p>(If you're not a Unity dev - it even works if you suddenly add one in the Editor while the game is running!)</p>
  <h1>How the hell do they do that, and how can I do it?</h1>
  <p>So, I want to do this:</p>
  <pre><code>public interface IGetNews
   {
   void SomeNews(string s);
   }
  </code></pre>
  <p>and then I can add <code>SomeNews</code> <em>to any MonoBehavior</em>.</p>
  <h1>The alternate solutions are obvious, I want to know specifically how Unity achieve that &quot;magic&quot; behavior.</h1>
  <p>(BTW: I feel they should not have called these &quot;interfaces&quot;, since, it's basically <em>nothing at all like an interface - it's sort of the opposite!</em> You could say they magically made a way to inherit from more than one abstract class, I guess.)</p>
  <hr />
  <p>Aside:</p>
  <p>if you've not used Unity before, the conventional way to do this - since we don't have access to Unity magic - is just add a UnityEvent to your daemon which will be sending the message in question:</p>
  <pre><code>public class BlahDaemon:MonoBehaviour
    {
    public UnityEvent onBlah;
    
      ...
      onBlah.Invoke();
  </code></pre>
  <p>Say you have classes Aaa, Bbb, Ccc which want to get the message.  Simply connect the Unity event (either by dragging in the editor or in code), example:</p>
  <pre><code>public class Aaa:MonoBehaviour
    {
    void Awake()
      {
      BlahDaemon b = Object.FindObjectOfType&lt;BlahDaemon&gt;();
      b.onBlah.AddListener(OnBlah);
      }
    
    public void OnBlah()
      {
      Debug.Log(&quot;almost as good as Unity's&quot;);
      }
    }
  </code></pre>
  <p>You're basically &quot;registering&quot; your call in <code>Awake</code>, you are indeed piggybacking on the magic Unity use - whatever it is.  But I want to use the magic directly.</p>