查看: 968|回复: 2
打印 上一主题 下一主题

[其他] Unity委托简单例子

[复制链接]

9903

主题

126

听众

7万

积分

首席设计师

Rank: 8Rank: 8

纳金币
53488
精华
316

最佳新人 热心会员 灌水之王 活跃会员 突出贡献 荣誉管理 论坛元老

跳转到指定楼层
楼主
发表于 2015-5-30 07:23:34 |只看该作者 |倒序浏览

  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>
复制代码
分享到: QQ好友和群QQ好友和群 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
转播转播0 分享淘帖0 收藏收藏0 支持支持0 反对反对0
回复

使用道具 举报

xx232    

17

主题

9

听众

3505

积分

中级设计师

Rank: 5Rank: 5

纳金币
865
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

沙发
发表于 2015-6-5 13:37:34 |只看该作者

  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;//实现委托    }}
复制代码
回复

使用道具 举报

1

主题

1

听众

145

积分

设计实习生

Rank: 2

纳金币
313
精华
0

最佳新人

板凳
发表于 2015-6-7 15:11:09 |只看该作者
这个代码格式,能看明白的人也太神了吧?
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

手机版|纳金网 ( 闽ICP备2021016425号-2/3

GMT+8, 2025-7-20 22:33 , Processed in 0.060548 second(s), 28 queries .

Powered by Discuz!-创意设计 X2.5

© 2008-2019 Narkii Inc.

回顶部