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. mysql简介

    1.什么是数据库 ? 数据库(Database)是按照数据结构来组织.存储和管理数据的仓库,它产生于距今六十多年前,随着信息技术和市场的发展,特别是二十世纪九十年代以后,数据管理不再仅仅是存储和管理数 ...

  2. html5常见问题

    H5项目常见问题汇总及解决方案 2016-12-21 FrontEndZQ JavaScript 转自 https://github.com/FrontEndZQ/HTML5-FAQ H5项目常见问题 ...

  3. C#日常总结1

    Rows:行的集合: Columns:列的集合: Gridview:用来显示数据的表格{ //设置 AutoGenerateColumns="false":表示不允许自动产生列,列 ...

  4. 无障碍网页设计(WCAG2.0)

    无障碍化是指无论健全人还是残疾人,年轻人还是老年人都可以平等地获取互联网上的信息和服务.无障碍化网站建设不单可以增加网站的受益群体,更是一个有情怀.有担当的互联网企业的责任,也是一个互联网从业人员应该 ...

  5. 【个人使用.Net类库】(4)验证码类

    验证码是现在管理系统常用的一种保护用户帐户信息的一种功能. 验证码可以有效防止某个黑客对某一个特定注册用户用特定程序暴力破解方式进行不断的登录尝试,虽然这可能是我们登录麻烦一点,但是对用户的密码安全来 ...

  6. golang——channel笔记

    1.for i := range channel { //... } 相当于 循环进行 i<-channel,直至close(channel) 2. · 给一个 nil channel 发送数据 ...

  7. Hive Over HBase

    1. 在hbase上建测试表 hbase(main)::> create 'test_hive_over_hbase','f' row(s) in 2.5810 seconds hbase(ma ...

  8. android使用html+javascript来制作页面

    一般的android界面使用的是XML.但是XML如果要制作很高级的UI,会很复杂.如果使用HTML老进行UI设计就会简单很多. android早就提供了这样的借口. WebView.addJavas ...

  9. pig hive 区别

    Pig是一种编程语言,它简化了Hadoop常见的工作任务.Pig可加载数据.表达转换数据以及存储最终结果.Pig内置的操作使得半结构化数据变得有意义(如日志文件).同时Pig可扩展使用Java中添加的 ...

  10. android广播接收器BroadcastReceiver

    首先看一下什么是 BroadcastReceiver BroadcastReceiver:直译是"广播接收者",所以它的作用是用来接收发送过来的广播的. 那我们有必要知道:什么是广 ...