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);
}
}

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的更多相关文章

  1. JSBinding / Home

    Description JSBinding is a tool enabling you to run actual javascript in Unity3D. It contains Mozill ...

  2. JSBinding+SharpKit / MonoBehaviour替换成JSComponent原理

    Unity 是基于组件式的开发,gameObject 身上可以绑定任意个脚本.每个脚本组成 gameObject 的一个部分. 脚本里通过添加预定义好的函数来执行自己的任务.比如Awake,用于初始化 ...

  3. JSBinding + SharpKit / Important Notes

    Serialization of List<T> is not supported. 1 public int v; // SUPPORTED 2 public GameObject go ...

  4. JSBinding+Bridge.NET:Unity游戏热更新方案

    老版本链接如下:http://www.cnblogs.com/answerwinner/p/4469021.html 新用户不要再使用老版本了. 新版本 JSBinding 将抛弃 SharpKit ...

  5. JSBinding / About 2048 sample

    2048 Source 2048 source code is here: https://github.com/gabrielecirulli/2048 Play here!http://gabri ...

  6. JSBinding+SharpKit / 菜单介绍

  7. JSBinding + SharpKit / 实战:转换 Survival Shooter

    从 asset store 下载 Survival Shooter (商店里有2个版本,一种是给Unity5用的,一个是给Unity4.6用的,我们这个实验用的是后者,版本是2.2.如果) 1 删除多 ...

  8. JSBinding + SharpKit / 需要注意及不支持的列表

    1) 序列化不支持 public List<T>,其余都支持(JSBinding+Bridge无此功能) 2015年11月5日 补充:序列化只处理 Field.目前发现 Animation ...

  9. JSBinding + SharpKit / 实战:转换 2DPlatformer

    最后修改:2015年07月29日 2016年2月25日 2DPlatformer 是 Unity3D 的一个官方 Demo.本文将介绍使用 JSBinging + SharpKit 转换 2DPlat ...

随机推荐

  1. hdu4734 F(x)

    链接 这题当时在网络赛时很费劲的推出来的,以递推的形式写出来的,一些边界点特别不好控制,靠看数据改出来的.现在改出dfs形式,也是很简单的. 因为f(x)的数不会很大,直接保留前面枚举的数得出的结果即 ...

  2. 关于tomcat会在url末尾自动追加斜杠(/)

    今天,突然发现一个问题, 比如我的请求路径为  http://ip:port/my_project/myapp, 在浏览器中敲入这个地址,然后会显示 http://ip:port/my_project ...

  3. C语言输出字符串

    在VS2012中,使用gets_s()方法,其中第二个参数可以用sizeof(...)代替.例子代码如下: #include <stdio.h> int main( ) { ]; gets ...

  4. zoj 1789 The Suspects

    好高兴,又AC一道 ,不过是很类似的两道..还是好高兴呀思想跟2833是一样的,不过要重新设计输入和输出.老师上课又重新讲解了一下,因为嫌疑人已知是0,所以加入集中时应该默认让数值小的做树根,即最终让 ...

  5. Yii 检查输入的数据是否已经存在

    只需要在对应的Model文件中的rules方法里面加入 array('username', 'unique', 'message'=>'{attribute}已存在'),//username是需 ...

  6. angularJS 报错: [ngModel:numfmt] http://errors.angularjs.org/1.4.1/ngModel/numfmt?p0=333

    <!doctype html> <html ng-app="a10086"> <head> <meta charset="utf ...

  7. UVA 1349(二分图匹配)

    1349 - Optimal Bus Route Design Time limit: 3.000 seconds A big city wants to improve its bus transp ...

  8. ios基础篇(二十)—— UIBezierPath绘制

    UIBezierPath类可以创建基于矢量的路径,可以定义简单的形状,如椭圆或者矩形,或者有多个直线和曲线段组成的形状. 一.UIBezierPath使用: 1.创建path: 2.添加路径到path ...

  9. NET中的规范标准注释(一) -- XML注释标签讲解

    一.摘要 .Net允许开发人员在源代码中插入XML注释,这在多人协作开发的时候显得特别有用. C#解析器可以把代码文件中的这些XML标记提取出来,并作进一步的处理为外部文档. 这篇文章将展示如何使用这 ...

  10. HTTP-Session工作机制

    HTTP-Session将HTTP这种无状态协议通过session来保存状态.然而session通过记录服务器生成编号来标识协议,从而服务器就可以识别协议的状态. session保存服务器端,  co ...