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 ...
随机推荐
- 使用celery之深入celery配置(转)
原文:http://www.dongwm.com/archives/shi-yong-celeryzhi-shen-ru-celerypei-zhi/ 前言 celery的官方文档其实相对还是写的很不 ...
- css+div打造三角形(箭头)
在很多网站都见过这样的箭头,之前我一直以为是图片,直到今天才知道原来可以用css做.开始看代码没太看懂,后来自己试了几遍才恍然大悟.贴出来分享下.(大神请直接忽略) 先看代码: HTML部分就是一个单 ...
- js之oop <二> 对象属性
js中对象属性可以动态添加和删除.删除对象属性用delete关键字. function obj(){ } var oo = new obj(); oo.a = "a"; oo.b ...
- 带选择的sql简单用法
一般写法: select * from itcast_topic order by (case type when 2 then 2 else 1 end ) desc ,postTime desc ...
- unity3d Light Probe Group图解超详细使用方法
原创文章如需转载请注明:转载自http://blog.csdn.net/qq617119142 第一步,创建一个Panel 和 三个 cube,搭建成如下图形状 第二步,创建2个点光源,一个为绿灯,一 ...
- 使用spawn-fcgi管理php-cgi
spawn-fcgi -a 127.0.0.1 -p 9000 -C 5 -u nginx -g nginx -f php-cgi
- <转>下一代Asp.net开发规范OWIN(1)—— OWIN产生的背景以及简单介绍
2014-09-04 07:22 by JustRun http://www.cnblogs.com/JustRun1983/p/3955238.html 随着VS2013的发布,微软在Asp.Net ...
- 转载---Java集合对象的深度复制与普通复制
原博文:http://blog.csdn.net/qq_29329775/article/details/49516247 最近在做算法作业时出现了错误,原因是没有弄清楚java集合的深度复制和浅度复 ...
- juqery easyui
私人做程序开发一直有个头疼的问题就是后台管理界面,以前一般都是自己用jquery+ps自己设计的,效果很一般,很不理想. 今天初次使用Jquery EasyUi,简单的做了个布局页面感觉还不错,给大家 ...
- wine install 32bit netframewok
WINEARCH=win32 WINEPREFIX=~/.wine32 winecfg env WINEARCH=win32 WINEPREFIX=~/.wine32 winetricks dotne ...