您的位置:首页 > 移动开发 > Unity3D

Unity Event&Delegate 例子

2016-04-05 17:08 483 查看
Publish :
using UnityEngine;
using System.Collections;

public class PublisherBall : MonoBehaviour {

public delegate void BallEventHandler(string s);
public event BallEventHandler BallDropEvent;
public string s;

void Start () {
BallDropEvent += MethodHandleEventForPublisher;
}

void Update () {

}

void OnCollisionEnter(Collision c)
{
if (BallDropEvent != null)
{
BallDropEvent(s);
}
}

void MethodHandleEventForPublisher(string s)
{
s = this.s;
Debug.Log (s);
}
}

Receive:

using UnityEngine;
using System.Collections;

public class ReceiverBall : MonoBehaviour {

public GameObject publisherBall;
public string s;
public float jumpForce;

void Start () {
publisherBall.GetComponent<PublisherBall> ().BallDropEvent += MethodHandleEvent;
}

void Update () {

}

void MethodHandleEvent(string s){
s = this.s;
Debug.Log (s);
GetComponent<Rigidbody> ().AddForce (new Vector3 (0f, jumpForce, 0f));
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: