码迷,mamicode.com
首页 > Web开发 > 详细

JSBinding / About JSComponent

时间:2016-07-06 13:23:38      阅读:236      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!