Unity Event&Delegate 例子
2016-04-05 17:08
483 查看
Publish :
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));
}
}
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));
}
}
相关文章推荐
- Cacti详解
- unity3d显示圆形或异形背景
- iTween基础之Color(变换颜色)
- Unity Shader 效果学习
- 使用Mali Graphics Debugger调优Unity程序(Killer示例)
- Unity3D 烘焙 对游戏包的影响
- Unity 设置图片和屏幕的分辨率
- 联机Unity Profile技巧--mac
- Unity 5.3 官方VR教程(二) 创建第一个VR项目
- Unity 如何让using resolution为真正的分辨率
- 承接Unity3D外包公司 — 技术分享
- Unity轻量数据保存
- Unity Animator的使用技巧
- unity animator 基本属性
- UnityVS(Visual Studio Tools For Unity)的下载、安装与使用
- MICROSOFT VISUAL STUDIO COMMUNITY 2015软件许可条款
- unity5与android通信报错
- Unity初级工程师面试指导
- Unity3D中级开发工程师面试指导
- [转] Unity Mathf 数学运算(C#)