首页 > Web开发 > 详细

JSBinding / About JSComponent

时间:2016-07-06 13:22:37      阅读:333      评论:0      收藏:0      [点我收藏+]

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

 

 

 

backto JSBinding / Home

JSBinding / About JSComponent

原文:http://www.cnblogs.com/answerwinner/p/5646524.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!