Unity 之 C# 利用回调函数实现C++匿名函数

做C++开发的都用过匿名函数非常好用,但是C#开发怎么实现呢?前几天做一个拍照功能的时候,我偶然发现某个函数如果是C++的话,用匿名函数太好了,于是开始研究C#的回调,代理,委托等,最后总算是实现了我想要的吧,不知道到底算什么调用。有大神的话可以给出评价。

参考文章:

详解C#委托,事件与回调函数
Unity3D研究院之利用C#实现代理模式(四十)


直接上代码吧,不废话了。A类是委托的定义类,B类是调用委托(匿名函数)的类。

using UnityEngine;
using System.Collections;

public class AClass{

	public delegate void onComplete(Sprite sprite);

	public IEnumerator GetTexture(onComplete callback)
	{
		//save
		Texture2D t=new Texture2D(Screen.width,(int)(Screen.width*aspect));
		t.ReadPixels(new Rect(0,0,Screen.width,(int)(Screen.width*aspect)),0,0,false);
		t.Apply();
		byte[] byt = t.EncodeToPNG();
		m_photoName = Time.time+".png";
		m_photoPath = Globe.persistentDataUrl+m_photoName;

		Debug.Log("System.IO++++++++Start WritePng");
		System.IO.File.WriteAllBytes(m_photoPath.Replace("file://",""),byt);
		
		Debug.Log("m_photoPath="+m_photoPath);
		//load image
		WWW www = new WWW(m_photoPath);
		yield return www;

		//回调
		callback(sprite);
	}
}

using UnityEngine;
using System.Collections;

public class BClass{

	public void ExecuteCallBack(){

		StartCoroutine(m_webCamera.GetTexture(delegate(Sprite sp)
		{
			watermark.gameObject.SetActive(false);
			photoImg.sprite=sp;
		}));
	}
}

A类中定义了一个用于回调的Delegate,参数是要传递的精灵类,GetTexture方法是用于截屏的方法。
B类中是调用方式,当B类调用A类的截屏方法时,可以直接通过Delegate(Sprite sp) 匿名函数来获取到sp得图片,应为A类中截屏方法中,使用了WWW的异步加载,所以B类中的给PhotoImg的精灵赋值也是异步的。

**注意事项,一般情况下呢,不需要异步的情况下,是不需要写StartCoroutine()的,那么就把代码简单的改一下就OK了

using UnityEngine;
using System.Collections;

public class BClass{

	public void ExecuteCallBack(){

		//StartCoroutine();
		m_webCamera.GetTexture(delegate(Sprite sp)
		{
			watermark.gameObject.SetActive(false);
			photoImg.sprite=sp;
		});
	}
}

但是,一般情况下使用匿名函数,多数人会想使用异步,这个时候StartCoroutine() 必须在调用的使用,即在B类中调用。如果在B类中不使用StartCortine() 而在A类中使用
即,在B类中调用A类的截屏方法

	public void ExecuteCallBack(){

		//StartCoroutine();
		
		m_webCamera.GetTexture(delegate(Sprite sp);

		m_webCamera.GetTexture(delegate(Sprite sp)
		{
			watermark.gameObject.SetActive(false);
			photoImg.sprite=sp;
		});
	}

而在A类的截屏当中使用StartCoroutine() 方法启用异步,则会出现空指针异常,这是我在使用过程中碰到的问题,困扰了我有一会儿呢,希望大家注意!!!


using UnityEngine;
using System.Collections;

public class AClass{

	public delegate void onComplete(Sprite sprite);

	public void GetTexture(onComplete callback){
	
		StartCoroutine(ScreenCapture(callback));
	}

	public IEnumerator ScreenCapture(onComplete callback)
	{
		//save
		Texture2D t=new Texture2D(Screen.width,(int)(Screen.width*aspect));
		t.ReadPixels(new Rect(0,0,Screen.width,(int)(Screen.width*aspect)),0,0,false);
		t.Apply();
		byte[] byt = t.EncodeToPNG();
		m_photoName = Time.time+".png";
		m_photoPath = Globe.persistentDataUrl+m_photoName;

		Debug.Log("System.IO++++++++Start WritePng");
		System.IO.File.WriteAllBytes(m_photoPath.Replace("file://",""),byt);
		
		Debug.Log("m_photoPath="+m_photoPath);
		//load image
		WWW www = new WWW(m_photoPath);
		yield return www;

		//回调

		callback(sprite);
	}
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

TxNet.Ltd.

你的赞同是对我的鼓励

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值