Jsbinding + Sharpkit/principle: Delegate

Source: Internet
Author: User

Take NGUI's Uieventlistener as an example:

There is a class:

1 usingSharpkit.javascript;2 usingUnityengine;3 usingSystem.Collections;4 5[Jstype (JSMODE.CLR,".. /streamingassets/javascript/sharpkitgenerated/z_temp/test0610.javascript")]6  Public classTest0610:monobehaviour {7      PublicUIButton btn;8 9     voidStart ()Ten     { One         //Register callback here AUieventlistener.get (btn.gameobject). OnClick = This. OnClick; -     } -  the     voidOnClick (gameobject go) -     { -Debug.Log ("onclick"); -     } +}

This class has a public UIButton btn; Variable, which can be assigned in Inspector. We use this class to respond to BTN click events. The point is line 12th.

The generated JS is as follows:

1 if(typeof(jstypes) = = "Undefined")2     varJstypes = [];3 vartest0610 = {4FullName: "test0610",5Basetypename: "Unityengine.monobehaviour",6AssemblyName: "Sharpkitproj",7Kind: "Class",8 definition: {9ctorfunction (){Ten              This. btn =NULL; OneUnityEngine.MonoBehaviour.ctor.call ( This); A         }, -Start:function (){ -Uieventlistener.get ( This. Btn.get_gameobject ()). OnClick = $CreateDelegate ( This, This. OnClick); the         }, -Update:function (){ -         }, -OnClick:function(GO) { +unityengine.debug.log$ $Object ("onclick"); -         } +     } A }; atJstypes.push (test0610);

Look at the JS code line 14th, the OnClick assignment is the return value of the $CreateDelegate

The function of the $CreateDelegate is to return a function, depending on the definition of the function in the Jsclr.javascript file. The implementation details of this function are not discussed here, as long as you know that he is returning a function. This function is passed to C # and becomes an ID.

Of course, if you want this JS to be able to execute properly, of course, to configure Uieventlistener to jsbindingsettings.classes array let him export, look at the OnClick this field of C # code:

1  Public Staticuieventlistener.voiddelegate uieventlistener_onclick_getdelegate_member2_arg0 (CSRepresentedObject Objfunction)2 {3     if(Objfunction = =NULL|| Objfunction.jsobjid = =0)4     {5         return NULL;6     }7Uieventlistener.voiddelegate action = (go) = =8     {9JSMgr.vCall.CallJSFunctionValue (0, Objfunction.jsobjid, go);Ten     }; One     returnAction; A } - Static voidUieventlistener_onclick (jsvcall vc) - { the     if(vc.bget) { -Uieventlistener _this =(Uieventlistener) vc.csobj; -         varresult =_this.onclick; -JSMgr.vCall.datax.setObject ((int) JSApi.SetType.Rval, result); +     } -     Else { +Uieventlistener _this =(Uieventlistener) vc.csobj; A_this.onclick = jsdataexchangemgr.getjsarg<uieventlistener.voiddelegate> (() ={ at     if(Jsapi.isfunctions (int) JSApi.GetType.Arg)) -         returnUieventlistener_onclick_getdelegate_member2_arg0 (Jsapi.getfunctions (int) (JSAPI.GETTYPE.ARG)) ; -     Else -         return(uieventlistener.voiddelegate) Vc.datax.getObject (int) JSApi.GetType.Arg); - }) - ; in     } -}

The 22nd line is in the Assignment OnClick field. Because the OnClick is Delegate, the assignment also gives him a Delegate, the Delegate is returned by the function uieventlistener_onclick_getdelegate_member2_arg0 (line 1th).

Line 24th first calls Jsapi.getfunctions (..) to get the JS function ID. Csrepresentedobject is just an encapsulation of the JS object. UIEVENTLISTENER_ONCLICK_GETDELEGATE_MEMBER2_ARG0 get this function ID,

Constructs a uieventlistener.voiddelegate type of Delegate, and finally assigns the value to Uieventlistener.onclick.

Back to Home:

Unity Code Hot Update Scenario jsbinding + Sharpkit Home

Jsbinding + Sharpkit/principle: Delegate

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.