纳金网

标题: Unity委托简单例子 [打印本页]

作者: 烟雨    时间: 2015-5-30 07:23
标题: Unity委托简单例子

  1. <div style="color: rgb(0, 0, 0); font-family: 'microsoft yahei'; line-height: 25.2000007629395px;">public class EventDispatcher : MonoBehaviour</div><div style="color: rgb(0, 0, 0); font-family: 'microsoft yahei'; line-height: 25.2000007629395px;">{</div><div style="color: rgb(0, 0, 0); font-family: 'microsoft yahei'; line-height: 25.2000007629395px;">    public delegate void EventHandler(GameObject e);//定义委托</div><div style="color: rgb(0, 0, 0); font-family: 'microsoft yahei'; line-height: 25.2000007629395px;">    public event EventHandler MouseOver;</div><div style="color: rgb(0, 0, 0); font-family: 'microsoft yahei'; line-height: 25.2000007629395px;">    void Start()</div><div style="color: rgb(0, 0, 0); font-family: 'microsoft yahei'; line-height: 25.2000007629395px;">    {</div><div style="color: rgb(0, 0, 0); font-family: 'microsoft yahei'; line-height: 25.2000007629395px;">        OnMouseOver();</div><div style="color: rgb(0, 0, 0); font-family: 'microsoft yahei'; line-height: 25.2000007629395px;">    }</div><div style="color: rgb(0, 0, 0); font-family: 'microsoft yahei'; line-height: 25.2000007629395px;">    void OnMouseOver()</div><div style="color: rgb(0, 0, 0); font-family: 'microsoft yahei'; line-height: 25.2000007629395px;">    {</div><div style="color: rgb(0, 0, 0); font-family: 'microsoft yahei'; line-height: 25.2000007629395px;">        if (MouseOver != null)</div><div style="color: rgb(0, 0, 0); font-family: 'microsoft yahei'; line-height: 25.2000007629395px;">        {</div><div style="color: rgb(0, 0, 0); font-family: 'microsoft yahei'; line-height: 25.2000007629395px;">            MouseOver(this.gameObject);//传递参数</div><div style="color: rgb(0, 0, 0); font-family: 'microsoft yahei'; line-height: 25.2000007629395px;">        }</div><div style="color: rgb(0, 0, 0); font-family: 'microsoft yahei'; line-height: 25.2000007629395px;">    }</div><div style="color: rgb(0, 0, 0); font-family: 'microsoft yahei'; line-height: 25.2000007629395px;">}</div><div style="color: rgb(0, 0, 0); font-family: 'microsoft yahei'; line-height: 25.2000007629395px;"><div>public class EventDis : MonoBehaviour {</div><div>
  2. </div><div>    public GameObject game;</div><div>    EventDispatcher EventHand;</div><div><span class="Apple-tab-span" style="white-space: pre;">        </span>void Start () {</div><div>        <span class="Apple-tab-span" style="white-space: pre;">        </span>EventHand = game.GetComponent<EventDispatcher>();</div><div><span class="Apple-tab-span" style="white-space: pre;">        </span>}</div><div>    void Listener(GameObject g) </div><div>    {</div><div>        Debug.Log(g.name);        </div><div>    }</div><div>    void OnMouseDown()</div><div>    {</div><div>        EventHand.MouseOver += Listener;//实现委托</div><div>    }</div><div>}</div></div>
复制代码

作者: xx232    时间: 2015-6-5 13:37

  1. public class EventDispatcher : MonoBehaviour{    public delegate void EventHandler(GameObject e);//定义委托    public event EventHandler MouseOver;    void Start()    {        OnMouseOver();    }    void OnMouseOver()    {        if (MouseOver != null)        {            MouseOver(this.gameObject);//传递参数        }    }}public class EventDis : MonoBehaviour {
  2.     public GameObject game;    EventDispatcher EventHand;        void Start () {                EventHand = game.GetComponent();        }    void Listener(GameObject g)     {        Debug.Log(g.name);            }    void OnMouseDown()    {        EventHand.MouseOver += Listener;//实现委托    }}
复制代码

作者: attach    时间: 2015-6-7 15:11
这个代码格式,能看明白的人也太神了吧?




欢迎光临 纳金网 (http://go.narkii.com/club/) Powered by Discuz! X2.5