一、回顾前面写的关于Delegate的消息传送优化文章,http://blog.csdn.net/chiuan/article/details/7883449
下面的代码是做一个简单的Delegate和SendMessage之间的优化性能差距测试:
using UnityEngine;
using System.Collections;
/// <summary>
/// Delegate basic.
/// just test Delegate && SendMessage ..
///
/// By Chiuan 2012.8
/// </summary>
public class DelegateBasic : MonoBehaviour {
//define my delegate statement.
public delegate void MyDelegate(string arg1);
//create my delegate object
public MyDelegate myDelegate;
//need some values to debug time spent.
bool isStart;
float timeStart;
int count;
bool isStartSendMessage;
// Use this for initialization
void Start () {
myDelegate += myFunciton1;
//myDelegate += myFunciton2;
}
// Update is called once per frame
void Update () {
if(isStart )
{
isStart = false;
cou

本文回顾了Delegate消息传递的优化,通过测试对比了Delegate和SendMessage在性能上的差异。在多线程环境下使用Delegate需要注意,Unity3D的MonoBehaviour快捷API只能在主线程调用,尝试在其他线程启动Coroutine会导致错误。此外,多线程中Delegate响应可能引发对未初始化的MonoBehaviour对象的空引用错误。
1877

被折叠的 条评论
为什么被折叠?



