JSBinding / About JSComponent

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:

  1. In Hierarchy window, select a GameObject
  2. In Inspector window, click AddComponent button
  3. 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:

  1. In Hierarchy window, select a GameObject
  2. In Inspector window, click AddComponent button
  3. Select JSComponent
  4. 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?

  1. Create a js object named ‘jss.TestMb‘
  2. Redirect MonoBehaviour‘s event funtions to js
  3. 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

JSBinding / About JSComponent的相关文章

JSBinding+SharpKit / 菜单介绍

[JSB | Generate JS and CS Bindings] 生成绑定,即让 Js 和 Cs 互通.详情请看 JSBinding+SharpKit / 生成 JavaScript 绑定 [JSB | Add SharpKit JsType Attribute for all Structs and Classes] 在所有逻辑代码中,所有类的定义前面加上 [JsType(JsMode.Clr, "~/Assets/StreamingAssets/JavaScript/SharpKitG

JSBinding + SharpKit / Convert 2DPlatformer to JavaScript version

2DPlatformer is a Unity3D official demo. Asset store URL: https://www.assetstore.unity3d.com/en/#!/content/11228 What we are going to do now is convert 2DPlatformer to JavaScript version. All C# scripts are all in 2DPlatformer/Scripts/ folder. We are

JSBinding / Testing

Unity version compatibilities 5.3.5 5.2.0 5.1.5 5.0.4 4.7.2 4.7.0 4.6.9 4.6.0 4.5.5 Platform compatibilities Windows 32 editor 32 executable 64 editor 64 executable Mac OS 32 editor 32 executable 64 editor 64 executable Android arm-v7a x86 iOS Mono 2

JSBinding + SharpKit / Home

Description JSBinding is a great tool enabling you to run actual JavaScript in Unity3D. It contains Mozilla SpiderMonkey JavaScript engine version 33 library. New version is expected to work with SharpKit (sharpkit.net). SharpKit is an open source to

JSBinding+Bridge:逻辑代码中操作二进制数据

以这2个函数为例 class File { public static byte[] ReadAllBytes(string path); public static void WriteAllBytes(string path, byte[] data); } 如果不做特殊处理,ReadAllBytes在返回数据给Js时,是一个字节一个字节拷贝给Js的数组.这样性能是极差的.并且,大多数情况下,逻辑代码中不需要直接修改2进制数据,而只是拿着而已. 因此,不要直接使用上面的2个函数. 以下是一个

JSBinding / Plugins & Build Mozjswrap Library

There are 2 libraries in Plugins: mozjs-31. This is SpiderMonkey library, built from https://github.com/cocos2d/Spidermonkey (not Mozilla official repository, used by cocos2d-js). mozjswrap. This is JSBinding library, it links C# and SpiderMonkey tog

JSBinding + SharpKit / 编译 Cs 成 Js

轻轻一点菜单:[JSB | Compile Cs to Js] 主要产出:StreamingAssets/JavaScript/SharpkitGeneratedFiles.javascript,你的所有逻辑代码都在这里 其他产出: Temp/AllInvocations.txt:记录所有逻辑代码对框架代码的调用 (1) Temp/AllInvocationsWithLocation.txt:同上,但同时记录每个调用的文件名和行号 (2) Temp/YieldReturnTypes.txt:记录

JSBinding + SharpKit / Important Notes

Serialization of List<T> is not supported. public List<int> lst; // NOT supported, use int[] instead About menu: JSB | Add SharpKit JsType Attribute for all Structs and Classes. If you execute this menu more than once, only one JsType will be

JSBinding + SharpKit / Generate JS Bindings

Classes in JSBindingSettings.classes array will be exported to JavaScript. There are already many classes (most of them are from UnityEngine.dll) in that array, you have to add your own classes. 1 public static Type[] classes = new Type[] 2 { 3 /* 4