Unity's C # uses callback functions to implement anonymous functions for C + +

Source: Internet
Author: User

The use of anonymous functions for C + + development is very useful, but how can C # development be implemented? A few days ago to do a photo function, I stumbled across a function if it is C + +, with anonymous function too good, so began to study C # callback, Proxy, commissioned, and finally is to achieve what I want it, do not know what to call. The words of the great God can be evaluated. Reference article: Detailed C # delegate, event and callback functions
Unity3d Research Institute using C # to implement proxy mode (40)
Directly on the code bar, no nonsense. Class A is the definition class for a delegate, and Class B is a class that invokes a delegate (an anonymous function).
Using unityengine;using system.collections;public class aclass{public delegate void OnComplete (Sprite sprite);p ublic IEnumerator GetTexture (OnComplete callback) {//savetexture2d 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);D Ebug. Log ("m_photopath=" +m_photopath)//load imagewww www = new www (m_photopath); yield return www;//callback callback (sprite);}}

Using unityengine;using system.collections;public class bclass{public void Executecallback () {Startcoroutine (m_ Webcamera.gettexture (Delegate (Sprite sp) {watermark.gameObject.SetActive (false);p hotoimg.sprite=sp;}));}}

A class defines a delegate that is used for callbacks, which is the Sprite class to pass, and the GetTexture method is used to intercept the screen.
Class B is called, when Class B calls the Class A screenshot method, you can directly through the delegate (Sprite SP) anonymous function to obtain the image of the SP, should be a class in the screenshot method, using the WWW asynchronous loading, so the photoimg in class B to the wizard assignment is also asynchronous. * * Note, under normal circumstances, do not need to write the Startcoroutine (), then the code is simple to change it OK
Using unityengine;using system.collections;public class bclass{public void Executecallback () {//startcoroutine (); m_ Webcamera.gettexture (Delegate (Sprite sp) {watermark.gameObject.SetActive (false);p hotoimg.sprite=sp;});}}

However, in general the use of anonymous functions, most people would like to use asynchronous, this time startcoroutine () must be used in the invocation, that is, in class B called. If Startcortine () is not used in class B and is used in Class A
That is, call the Class A screenshot method in class B
public void Executecallback () {//startcoroutine (); M_webcamera.gettexture (delegate (Sprite sp); m_ Webcamera.gettexture (Delegate (Sprite sp) {watermark.gameObject.SetActive (false);p hotoimg.sprite=sp;});}

In a class of screenshots using the Startcoroutine () method to enable asynchronous, there will be a null pointer exception, which I encountered during the use of the problem, bothered me for a while, I hope everyone attention!!!
Using unityengine;using system.collections;public class aclass{public delegate void OnComplete (Sprite sprite);p ublic void GetTexture (OnComplete callback) {Startcoroutine (Screencapture (callback));} Public IEnumerator Screencapture (OnComplete callback) {//savetexture2d 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);D Ebug. Log ("m_photopath=" +m_photopath)//load imagewww www = new www (m_photopath); yield return www;//callback callback (sprite);}}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Unity's C # uses callback functions to implement anonymous functions for C + +

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.