JSCompnent is a normal Unity script.
It inherits from JSSerializer and JSSerializer inherits from MonoBehaviour.
public class JSSerializer : MonoBehaviour { }
public class JSComponent : JSSerializer { }
When using c#, steps to add a component to a gameobject are:
- In Hierarchy window, select a GameObject
- In Inspector window, click AddComponent button
- Select script you need
In the case of javascript, how to add a ‘js monobehaviour‘ to a gameobject? For example, we have a js monobehaviour:
// define a js monobehaviour jss.define_mb("TestMb", function () { // called from c# this.Start = function () { } // called from c# this.Update = function () { } });
Steps to add it to a gameobject:
- In Hierarchy window, select a GameObject
- In Inspector window, click AddComponent button
- Select JSComponent
- Set ‘Js Class Name‘ to ‘jss.TestMb‘
The main difference here is the use of JSComponent. JSComponent is an agent for javascript monobehaviour.
What does JSComponent do?
- Create a js object named ‘jss.TestMb‘
- Redirect MonoBehaviour‘s event funtions to js
- Destroy js object when its OnDestroy is called
public class JSComponent : JSSerializer { int jsObjID; void initJs() { // 1 create js object jsObjID = JSApi.newJSClassObject(this.jsClassName); } void Start() { // 2 call js Start CallJSFunction(jsObjID, "Start"); } void Update() { // 2 call js Update CallJSFunction(jsObjID, "Update"); } void OnDestroy() { // 2 call js OnDestroy CallJSFunction(jsObjID, "OnDestroy"); // 3 delete js object DeleteJSObject(jsObjID); } }
backto JSBinding / Home
时间: 2024-11-13 06:45:25