- 最后登录
- 2021-7-6
- 注册时间
- 2012-12-27
- 阅读权限
- 90
- 积分
- 76145
 
- 纳金币
- 53488
- 精华
- 316
|
- <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>
- </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>
复制代码 |
|