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. python 正则表达式总结

    一.匹配元字符 使用元字符可以简便操作,写正则表达式时更方便 常用元字符: . 它匹配除了换行字符外的任何字符,在 alternate 模式(re.DOTALL)下它甚至可以匹配换行  ^ 匹配行首. ...

  2. stm32cube--IWDG使用

    IWDG使用的是32芯片内部的40k独立晶振,该晶振为rtc和iwdg提供时钟,即使是主时钟坏了也不影响它们. 主要用到三个寄存器, IWDG_KR    键值寄存器 IWDG_PR     预分频寄 ...

  3. ionic build android--> Build failed with an exception. Execution failed for task ':processDebugResources'.

    执行 ionic build android,   ionic 自动化生成安卓apk包, 出现以上报错的原因为:打包的文件中含有中文名,把中文名的文件去掉或改名即可打包成功.

  4. Android Material适配 为控件设置指定背景色和点击波纹效果

    Android Material适配 为控件设置指定背景色和点击波纹效果,有需要的朋友可以参考下. 大部分时候,我们都需要为控件设置指定背景色和点击效果 4.x以下可以使用selector,5.0以上 ...

  5. SUBLIME TEXT 2 设置文件详解

    SUBLIME TEXT 2 设置文件详解 Preferences.sublime-settings文件: // While you can edit this file, it’s best to ...

  6. SQL Server常用语句

    欢迎和大家交流技术相关问题: 邮箱: jiangxinnju@163.com 博客园地址: http://www.cnblogs.com/jiangxinnju GitHub地址: https://g ...

  7. android开发文档工具集(持续更新中...)

     http://www.androiddevtools.cn/ android 产品->交互->视觉->开发->测试各种工具地址下载, 各种文档下载应有尽有,强烈推荐.  ht ...

  8. 基础知识复习(二)——stdafx.h 头文件及x&(x-1)运算

    今天好久没写过C++程序了,使用VS2013 新建空的控制台程序,结果自动生成了头文件和main 方法. 就了解了stdafx.h头文件的含义及用法. stdafx:standard Applicat ...

  9. 【CSS3】标签使用说明

    转换(transform):改变元素的形状.大小和位置. transform:rotate(20deg):顺时针旋转20° rotate()用来2D旋转改变角度.支持负数,表示逆时针. transfo ...

  10. Nginx reopen reload作用及工作过程

    http://www.iigrowing.cn/nginx-reopen-reload-zuo-yong-ji-gong-zuo-guo-cheng.html Nginx reopen reload作 ...