In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
What is the loading process of JS Binding and SharpKit or JavaScript? in view of this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.
First of all, the current solution is to load all the JavaScript code as soon as the game starts.
First, take a look at the directory structure under the StreamingAssets/JavaScript/ folder:
Debug/: JavaScript code related to JavaScript debugging needs to be loaded only when debugging is turned on.
Manual/: is also used for handwritten JavaScript code, with some differences from JSImp/. The most representative of Manual are Vector2 and Vector3. These two objects are handwritten. Running in JavaScript will only use JavaScript versions of Vector2 and Vector3.
SharpKit/: took some JavaScript library files from SharpKit. You can implement class inheritance, etc.
GeneratedFiles.javascript: is generated by the menu [JSB | Generate JS and CS Bindings]. Used to bind C # functions to JavaScript. For example, if you want to use GameObject in JavaScript, you need to use the files in it. This is different from LUA, where LUA generates binding time only to generate C # code, which we have on both sides.
SharpKitGeneratedFiles.javascript: all JavaScript code compiled by the SharpKit project will be placed in this file.
Includes.javascript:JavaScript code master entry
Currently there is only one entry for JavaScript loading, StreamingAsset/JavaScript/includes.javascript.
View the configuration on _ JSEngine.prefab:
Includes.javascript contains all the JavaScript files through CS.require:
(this content may not be up to date)
1 / * 2 * Author: Qiucw 3 * DO NOT change order 4 * / 567 / /-1) 8 /-9 function jsb_ReplaceOrPushJsType (jst) {10 / / if (! JsTypes) {JsTypes = [];} 11 var found = false;12 for (var I = 0; I
< JsTypes.length; i++) {13 if (JsTypes[i].fullname == jst.fullname) {14 JsTypes[i] = jst;15 found = true;16 break;17 }18 }19 if (!found) {20 JsTypes.push(jst);21 }22 }23 24 25 // 0) SharpKit library26 //--------------------------------------------------27 CS.require("SharpKit/jsclr");28 CS.require("SharpKit/clrlibrary");29 30 // 1) Files generated by JSBinding31 // may overwrite some classes in step 0)32 //--------------------------------------------------33 CS.require("GeneratedFiles");34 35 36 // 2) Manually written js37 // will overwrite some classes in step 1)38 //--------------------------------------------------39 CS.require("Manual/UnityEngine_Vector3");40 CS.require("Manual/UnityEngine_Vector2");41 CS.require("Manual/UnityEngine_MonoBehaviour");42 CS.require("Manual/UnityEngine_WaitForSeconds");43 CS.require("Manual/MissingClasses");44 //45 // may be more..46 //47 48 // 3) code generated by SharpKit49 //--------------------------------------------------50 CS.require("SharpKitGeneratedFiles");51 52 53 // 4) JavaScript implemented54 // will overwrite some classes in step 1)55 //--------------------------------------------------56 CS.require("JSImp/Reflection");57 CS.require("JSImp/Coroutine");58 CS.require("JSImp/Iterator");59 60 //61 // may be more..62 //63 64 // 5) SharpKit handler (Compile)65 //--------------------------------------------------66 CS.require("SharpKit/myclrhandler");67 68 // 6) Error handler69 //--------------------------------------------------70 CS.require("ErrorHandler"); JavaScript 里的类一开始都是用 JsType 对象来表示,可以打开 StreamingAssets/JavaScript/SharpKitGeneratedFiles.javascript/ 查看一下。这里贴出来 V3Test 这个 MonoBehaviour 的 JavaScript 代码: 1 if (typeof(JsTypes) == "undefined") 2 var JsTypes = []; 3 var V3Test = { 4 fullname: "V3Test", 5 baseTypeName: "UnityEngine.MonoBehaviour", 6 assemblyName: "SharpKitProj", 7 Kind: "Class", 8 definition: { 9 ctor: function (){10 this.elapsed = 0;11 UnityEngine.MonoBehaviour.ctor.call(this);12 },13 Start: function (){14 },15 Update: function (){16 this.elapsed += UnityEngine.Time.get_deltaTime();17 if (this.elapsed >1) {18 var sb = new System.Text.StringBuilder.ctor (); 19 this.elapsed = 0 20 var v = new UnityEngine.Vector3.ctor$$Single$$Single$$Single (2,3,6); 21 var w = new UnityEngine.Vector3.ctor$$Single$$Single$$Single (7,23,1); 22 var n = v.get_normalized () 23 var arr = [n.x, n.y, n.z]; 24 UnityEngine.Debug.Log$$Object (sb.AppendFormat$$String$$Object$Array ("v.normalized = ({0}, {1}, {2})", arr) .toString (); 25 sb.Remove (0, sb.get_Length ()); 26 var cross = UnityEngine.Vector3.Cross (v, w) 27 arr = [cross.x, cross.y, cross.z]; 28 UnityEngine.Debug.Log$$Object ("Cross (v, w) = ({0}, {1}, {2})", arr) .toString (); 29 UnityEngine.Debug.Log$$Object ("v.magnitude =" + v.get_magnitude ()) 30 UnityEngine.Debug.Log$$Object ("w.magnitude =" + w.get_magnitude ()); 31 UnityEngine.Debug.Log$$Object ("Dot (v, w) =" + UnityEngine.Vector3.Dot (v, w)); 32 UnityEngine.Debug.Log$$Object ("Angle (v, w) =" + UnityEngine.Vector3.Angle (v, w)); 33 var proj = UnityEngine.Vector3.Project (v, w) 34 UnityEngine.Debug.Log$$Object ("Project (vForce w) =" + proj.toString ()); 35 v.Normalize (); 36 w.Normalize (); 37 UnityEngine.Debug.Log$$Object ("normalized v =" + v.toString ()); 38 UnityEngine.Debug.Log$$Object ("normalized w =" + w.toString ()) 39} 40} 41} 42}; 43 JsTypes.push (V3Test)
This file is generated by the SharpKit project and is not handwritten.
A JsType is used to define a class. The fields inside are usually:
Fullname: represents the full name of this class, as well as the namespace. V3Test doesn't have a namespace, so it doesn't.
BaseTypeName: the name of the parent class. In the next steps, it will be used to implement inheritance.
Kind: usually "Class", indicating that this is a class. It can also be "Struct" or "Enum"
Definition: represents the instance function and its Property, as well as the constructor. Ctor represents the constructor. V3Test only has two functions, Start and Update, which are typical functions of MonoBehaviour.
StaticDefinition: represents a static function.
Fields: instance field.
StaticFields: static field.
InterfaceNames: the name of the interface, which is an array
As you can see, the file of each class defines a JsType, and the file finally push it into the JsTypes array.
What are you doing in the JsTypes array?
There is require SharpKit/myclrhandler.javascript in includes.javascript. You can open this file and have a look. The most important line of code in this file is:
1 Compile ()
The Compile function is defined in SharpKit/jsclr.javascript. If you are interested, please take a look at the source code.
Compile does two main things:
Set up global objects so that you can access these classes. For example, you can use UnityEngine.GameObject to access this class.
Handle inheritance relationships. For example, V3Test does not define the gameObject attribute, but it can be accessed by this.gameObject in V3Test.
The answer to the question about the loading process of JS Binding and SharpKit or JavaScript is shared here. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.
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.