In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces how to analyze Delegate based on JSBinding+SharpKit. The content is very detailed. Interested friends can use it for reference. I hope it will be helpful to you.
Take NGUI's UIEventListener as an example:
There is one class:
Using SharpKit.JavaScript; using UnityEngine; using System.Collections; [JsType (JsMode.Clr, ".. / StreamingAssets/JavaScript/SharpKitGenerated/z_temp/test0610.javascript")] public class test0610: MonoBehaviour {public UIButton btn; void Start () {/ / register callback UIEventListener.Get (btn.gameObject) .onClick = this.OnClick; / / (*)} void OnClick (GameObject go) {Debug.Log ("onclick") }}
This class has a public UIButton btn; variable that can be assigned in Inspector. We use this class to respond to the click event of btn. The point is line 12.
The generated JS is as follows:
1 if (typeof (JsTypes) = = "undefined") 2 var JsTypes = []; 3 var test0610 = {4 fullname: "test0610", 5 baseTypeName: "UnityEngine.MonoBehaviour", 6 assemblyName: "SharpKitProj", 7 Kind: "Class", 8 definition: {9 ctor: function () {10 this.btn = null;11 UnityEngine.MonoBehaviour.ctor.call (this) 12}, 13 Start: function () {14 UIEventListener.Get (this.btn.get_gameObject ()). OnClick = $CreateDelegate (this, this.OnClick); / / (*) 15}, 16 Update: function () {17}, 18 OnClick: function (go) {19 UnityEngine.Debug.Log$$Object ("onclick"); 20} 21} 22} 23 JsTypes.push (test0610)
Look at line 14 of the JS code and assign onClick to the return value of $CreateDelegate
The function of $CreateDelegate is to return a function, as shown in the definition of this function in the jsclr.javascript file. The implementation details of this function are not discussed here, just know that it returns a function. When this function is passed to C #, it becomes an ID.
Of course, if you want this JS to work properly, of course, configure UIEventListener to the JSBindingSettings.classes array for him to export. Let's take a look at the C # code of the onClick field:
1 public static UIEventListener.VoidDelegate UIEventListener_onClick_GetDelegate_member2_arg0 (CSRepresentedObject objFunction) 2 {3 if (objFunction = = null | | objFunction.jsObjID = = 0) 4 {5 return null; 6} 7 UIEventListener.VoidDelegate action = (go) = > 8 {9 JSMgr.vCall.CallJSFunctionValue (0, objFunction.jsObjID, go); 10}; 11 return action 12} 13 static void UIEventListener_onClick (JSVCall vc) 14 {15 if (vc.bGet) {16 UIEventListener_ this = (UIEventListener) vc.csObj;17 var result = _ this.onClick;18 JSMgr.vCall.datax.setObject ((int) JSApi.SetType.Rval, result); 19} 20 else {21 UIEventListener_ this = (UIEventListener) vc.csObj 22 _ this.onClick = JSDataExchangeMgr.GetJSArg (() = > {23 if (JSApi.isFunctionS ((int) JSApi.GetType.Arg)) 24 return UIEventListener_onClick_GetDelegate_member2_arg0 (JSApi.getFunctionS ((int) JSApi.GetType.Arg)); 25 else26 return (UIEventListener.VoidDelegate) vc.datax.getObject ((int) JSApi.GetType.Arg); 27}) 28; 29} 30}
Line 22 is in the assigned onClick field. Because onClick is Delegate, the assignment also gives him a Delegate, which is returned by the function UIEventListener_onClick_GetDelegate_member2_arg0 (line 1).
Line 24 first calls JSApi.getFunctionS (..) Gets the JS function ID. CSRepresentedObject is just an encapsulation of the JS object. After UIEventListener_onClick_GetDelegate_member2_arg0 gets this function ID
A Delegate of type UIEventListener.VoidDelegate is constructed and finally assigned to UIEventListener.onClick.
The above is a relatively simple case: store the JS function in C #.
In another case, store the C # function in JS.
/ / C#
1 TweenEasingCallback func = TweenEasingFunctions.GetFunction (this.EaseType); 2 3 transform.TweenPosition () 4 .SetEndValue (transform.position + (Vector3.right * 9f)) 5 .SetDelay (0.5f, false) 6 .SetDuration (1.33f) 7 .SetEasing (func) 8 .SetLoopType (TweenLoopType.Loop) 9. Play ()
/ / JS code
1 var func = DaikonForge.Tween.TweenEasingFunctions.GetFunction (this.EaseType) / / get a delegate from C# (1) 2 TweenTransformExtensions.TweenPosition$$Transform (this.get_transform ()) 3 .SetEndValue (UnityEngine.Vector3.op_Addition (this.get_transform (). Get_position (), (UnityEngine.Vector3.op_Multiply$$Vector3 $$Single (UnityEngine.Vector3.get_right (), 9) 4 .SetDelay $Single$$Boolean (0.5) False) 5 .SetDuration (1.33) 6 .SetEasing (func) / / give back to C # (2) 7 .SetLoopType (1) 8. Play ()
In this case, you have to configure TweenEasingFunctions to JSBindingSettings.classes in advance and let him export it before JS can be used.
/ / binding code of TweenEasingFunctions.GetFunction:
1 static bool TweenEasingFunctions_GetFunction__EasingType (JSVCall vc, int argc) 2 {3 int len = argc; 4 if (len = = 1) 5 {6 DaikonForge.Tween.EasingType arg0 = (DaikonForge.Tween.EasingType) JSApi.getEnum ((int) JSApi.GetType.Arg); 7 JSMgr.vCall.datax.setObject ((int) JSApi.SetType.Rval, DaikonForge.Tween.TweenEasingFunctions.GetFunction (arg0)); 8} 9 10 return true;11}
As you can see, when JS gets a delegate from C #, the JSDataExchange.setObject function is called.
The specific code of setObject is not posted here, please check the source code.
In the setObject function, it determines that if the object is a delegate, then create a JSRepresentedObject object to return to the JS and save the corresponding relationship between the two.
The definition of JSRepresentedObject comes first in StreamingAssets/JavaScript/SharpKit/myclrhandler.javascript.
So, after JS gets this object, it can't be used. Because it's a function on this side of C #. He can only return this thing to C #. Please take a look at the JS code tag (2) above. When he returns to C #, C # can find the Delegate based on the previously stored correspondence.
On how to analyze Delegate based on JSBinding+SharpKit to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.