zoukankan      html  css  js  c++  java
  • JSBinding / About JSComponent and Serialization

    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

  • 相关阅读:
    linux下vim更改注释颜色
    使用web3j工具生成java版本的智能合约
    Pycharm增加新安装Python的路径
    Python第三弹--------文件和异常
    (转)以太坊(Ethereum)创世揭秘 以太坊(Ethereum)创世揭秘
    (转)以太坊(Ethereum)全零地址(0x000000...)揭秘
    (转)Java大数操作(BigInteger、BigDecimal)
    关于SimpleDateFormat时间转换总是显示1970年的问题
    以太坊abi
    (转)以太坊私链的挖矿速度与难度值的关系
  • 原文地址:https://www.cnblogs.com/answerwinner/p/5646524.html
Copyright © 2011-2022 走看看