近期热门
粉丝3
关注 0
获赞 2
Unity3D教程之事件、委托机制在unity3d中的使用

[U3D] Unity3D教程之事件、委托机制在unity3d中的使用

[复制链接]
1663 3 0 0 6年前 举报

关于Delegate作用我就不多说了,不懂的童鞋可以丝路教程网看具体的讲解,Unity中可以直接使用EventHandler实现事件委托开始今天的Unity 3D教程讲解吧。

  一、场景物体移动结束后事件监听

  假如PlayerControl,移动结束后触发MoveComplete事件。

  using UnityEngine;

  using System.Collections;

  using System;

  public class PlayerControl : MonoBehaviour {

  public event EventHandler MoveComplete;

  // Use this for initialization

  void Start () {

  }

  // Update is called once per frame

  void Update () {

  if (Input.GetMouseButtonUp(0)) {

  // Test logic for PlayerMoveComplete

  PlayerMoveComplete();

  }

  }

  void PlayerMoveComplete()

  {

  if (MoveComplete != null) {

  MoveComplete(this, EventArgs.Empty);

  }

  }

  }

  事件接收处理

  using UnityEngine;

  using System.Collections;

  using System;

  public class GameManager : MonoBehaviour {

  public static GameManager Instance;

  public PlayerControl playerControl;

  void Awake ()

  {

  // check there isn't more than one instance of the GameManager in the scene

  if(Instance != null){

  Debug.LogError("More than one GameManager found in the scene");

  return;

  }

  // set the global instance

  Instance = this;

  }

  // Use this for initialization

  void Start () {

  playerControl.MoveComplete += HandleMoveComplete;

  }

  void HandleMoveComplete (object sender, EventArgs e)

  {

  Debug.Log("MoveComplete:");

  }

  // Update is called once per frame

  void Update () {

  }

  }

  这里由于使用的EventHandler实现,所以在事件中无法传递自定义参数。

  二、自定义Event,事件中传递自定义参数

  1、自定义EventArgs

  using UnityEngine;

  using System.Collections;

  using System;

  public class PlayerMoveEventArgs : EventArgs {

  private string message;

  public PlayerMoveEventArgs(string message)

  {

  this.message = message;

  }

  public string Message

  {

  get{return message;}

  }

  }

  public delegate void MoveCompleteHandle(object sender, PlayerMoveEventArgs e);

  那么我们修改下PlayerControl

  using UnityEngine;

  using System.Collections;

  using System;

  public class PlayerControl : MonoBehaviour {

  public event EventHandler MoveComplete;

  public event MoveCompleteHandle CustomMoveComplete;

  // Use this for initialization

  void Start () {

  }

  // Update is called once per frame

  void Update () {

  if (Input.GetMouseButtonUp(0)) {

  // Test logic for PlayerMoveComplete

  PlayerMoveComplete();

  }

  }

  void PlayerMoveComplete()

  {

  if (MoveComplete != null) {

  MoveComplete(this, EventArgs.Empty);

  }

  if (CustomMoveComplete != null) {

  CustomMoveComplete(this, new PlayerMoveEventArgs("Move:" + this.name));

  }

  }

  }

  处理事件的我们也修改下

  using UnityEngine;

  using System.Collections;

  using System;

  public class GameManager : MonoBehaviour {

  public static GameManager Instance;

  public PlayerControl playerControl;

  void Awake ()

  {

  // check there isn't more than one instance of the GameManager in the scene

  if(Instance != null){

  Debug.LogError("More than one GameManager found in the scene");

  return;

  }

  // set the global instance

  Instance = this;

  }

  // Use this for initialization

  void Start () {

  playerControl.MoveComplete += HandleMoveComplete;

  playerControl.CustomMoveComplete += HandleCustomMoveComplete;

  }

  void HandleCustomMoveComplete (object sender, PlayerMoveEventArgs e)

  {

  Debug.Log("HandleCustomMoveComplete:" + e.Message);

  }

  void HandleMoveComplete (object sender, EventArgs e)

  {

  Debug.Log("MoveComplete:");

  }

  // Update is called once per frame

  void Update () {

  }

  }

       原文链接:http://blog.silucg.com/unity/1316.html



0
点赞
0
打赏
0
添加到收藏夹

0

点击复制链接

使用微信扫码分享
一次扣10个券
全部评论3
您需要登录后才可以回帖 登录

啥都看不到呀!!!!怎么搞???
5年前
回复

使用道具 举报

厉害了我的哥!
6年前
回复

使用道具 举报

谢谢楼主分享~
6年前
回复

使用道具 举报

您当前使用的浏览器IE内核版本过低会导致网站显示错误

请使用高速内核浏览器或其他浏览器