JSBinding / About JSComponent and Serialization
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:
- 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);
}
}
About Serialization
Serializing data to js object is JSSerializer's job. Steps to add serialization fields to javascript and let JSSerializer do work for you:
- 1. Add some variables to javascript monobehaviour:
// define a js monobehaviour
jss.define_mb("TestMb", function () {
// UnityEngine.Object
this.oValue = null; // int
this.iValue = 0; // string
this.sValue = ""; // double
this.fValue = 0; // another js monobehaviour
this.kValue = null;
});
- 2. Assign values in Inspector window:
Arr String
Size 5
Element 0 oValue/0
Element 1 iValue/5
Element 2 sValue/hello
Element 3 fValue/6.3
Element 4 kValue/1/jss.SayHello
Arr Object
Size 2
Element 0 [MainCamera]
Element 1 [SayHello]
After that, your js object's fields will be correctly assigned at application launching.
Extending & Customizing JSComponent
The default JSComponent only contains these event functions
- Awake
- Start
- OnDestroy
- FixedUpdate
- Update
- LateUpdate
- OnEnable
They are probably not enough. There is an example script extending JSComponent: JSComponentCustom1. Let me show you how it works:
- 1. First, add a new script inheriting from JSComponent, add event functions you need:
public class JSComponentCustom1 : JSComponent
{
int idOnGUI = 0; protected override void initMemberFunction()
{
base.initMemberFunction();
idOnGUI = JSApi.getObjFunction(jsObjID, "OnGUI");
} void OnGUI()
{
callIfExist(idOnGUI);
}
}
- 2. In Components.cs, make a slight change to GameObject_AddComponentT1 function:
public static bool GameObject_AddComponentT1(JSVCall vc, int count)
{
help_getGoAndType(vc); if (typeInfo.IsCSMonoBehaviour)
{
Component com = go.AddComponent(type);
JSMgr.datax.setObject((int)JSApi.SetType.Rval, com);
}
else
{
JSComponent jsComp;
int iOfJsComp = 0;
if (count > 1)
iOfJsComp = JSApi.getInt32((int)JSApi.GetType.Arg); switch (iOfJsComp)
{
// add here!
case 1:
jsComp = go.AddComponent<JSComponentCustom1>();
break; default:
jsComp = go.AddComponent<JSComponent>();
break;
} jsComp.jsClassName = typeString;
jsComp.jsFail = false;
jsComp.init(true);
jsComp.callAwake(); //JSApi.JSh_SetRvalObject(vc.cx, vc.vp, jsComp.jsObj);
JSApi.setObject((int)JSApi.SetType.Rval, jsComp.GetJSObjID(false));
}
return true;
}
- 3. When you call AddComponent$1 in javascript, add a custom parameter (1)
this.oDogGameObject.AddComponent$1(jss.Dog, 1/* Custom JSComponent */ );
backto JSBinding / Home
JSBinding / About JSComponent and Serialization的更多相关文章
- JSBinding / Home
Description JSBinding is a tool enabling you to run actual javascript in Unity3D. It contains Mozill ...
- JSBinding+SharpKit / MonoBehaviour替换成JSComponent原理
Unity 是基于组件式的开发,gameObject 身上可以绑定任意个脚本.每个脚本组成 gameObject 的一个部分. 脚本里通过添加预定义好的函数来执行自己的任务.比如Awake,用于初始化 ...
- JSBinding + SharpKit / Important Notes
Serialization of List<T> is not supported. 1 public int v; // SUPPORTED 2 public GameObject go ...
- JSBinding+Bridge.NET:Unity游戏热更新方案
老版本链接如下:http://www.cnblogs.com/answerwinner/p/4469021.html 新用户不要再使用老版本了. 新版本 JSBinding 将抛弃 SharpKit ...
- JSBinding / About 2048 sample
2048 Source 2048 source code is here: https://github.com/gabrielecirulli/2048 Play here!http://gabri ...
- JSBinding+SharpKit / 菜单介绍
- JSBinding + SharpKit / 实战:转换 Survival Shooter
从 asset store 下载 Survival Shooter (商店里有2个版本,一种是给Unity5用的,一个是给Unity4.6用的,我们这个实验用的是后者,版本是2.2.如果) 1 删除多 ...
- JSBinding + SharpKit / 需要注意及不支持的列表
1) 序列化不支持 public List<T>,其余都支持(JSBinding+Bridge无此功能) 2015年11月5日 补充:序列化只处理 Field.目前发现 Animation ...
- JSBinding + SharpKit / 实战:转换 2DPlatformer
最后修改:2015年07月29日 2016年2月25日 2DPlatformer 是 Unity3D 的一个官方 Demo.本文将介绍使用 JSBinging + SharpKit 转换 2DPlat ...
随机推荐
- maven eclipse 安装
jdk 的 系统变量一定要用 JAVA_HOME maven 的系统变量也一定要用 M2_HOME 配置在path 中 一定要用 %JAVA_HOME%\bin; 和 %M2_HOME%\bi ...
- Poisson回归模型
Poisson回归模型也是用来分析列联表和分类数据的一种方法,它实际上也是对数线性模型的一种,不同点是对数线性模型假定频数分布为多项式分布,而泊松回归模型假定频数分布为泊松分布. 首先我们来认识一下泊 ...
- c#调用c++动态链接库的问题
1. Server Error in '/' Application. Object reference not set to an instance of an object. 这个问题是接口中的方 ...
- android 发短信
SmsManager smsManager = SmsManager.getDefault(); List<String> divideContents = smsManager.divi ...
- 回车键转tab键解决方案二
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...
- 使用scp将文件/目录拷贝到另一台Linux主机上
如何将一台Linux主机上的文件或目录拷贝到另一台Linux主机上,scp命令可以实现该需求 前提条件:两台Linux主机处于同一网段,可以互相ping通 操作如下: 文件拷贝 ①将本地文件拷贝到远端 ...
- WCF初探-13:WCF客户端为双工服务创建回调对象
前言: 在WCF初探-5:WCF消息交换模式之双工通讯(Duplex)博文中,我讲解了双工通信服务的一个应用场景,即订阅和发布模式,这一篇,我将通过一个消息发送的例子讲解一下WCF客户端如何为双工服务 ...
- [Jquery]某宝图片轮播(无缝、带左右切换按钮)
[效果] 左右移动(非渐隐) [思路] 1.结构与样式 ①最外层div盒子当容器,里面ul宽度无限大并且相对定位(到时候移动其实移的是ul的left) ②按钮的透明度可用background:rgba ...
- (C语言)结构体成员的引用->(箭头)和 .(点)
关于结构体成员的引用有这样的规律: 箭头(->):左边必须为指针: 点号(.):左边必须为实体. 那么如果一个结构体指针引用一个成员,这个成员又是一个结构体(并且是一个实体),那么如果要引用这个 ...
- 【php常用】常用函数啥的
1.intval() 把变量转换成整数类型 2.trim() 去除字符串两边空格or 加上参数是去除该参数 3.array_values() 函数返回一个包含给定数组中所有键值的数组,但不保留键名 ...