예제 #1
0
    static int AddJSComponent(IntPtr cx, uint argc, IntPtr vp)
    {
        if (argc != 2)
        {
            return(0);
        }

        IntPtr jsObj = JSApi.JSh_ArgvObject(cx, vp, 0);
        object csObj = JSMgr.getCSObj(jsObj);

        if (csObj == null || !(csObj is GameObject))
        {
            return(0);
        }

        GameObject go = (GameObject)csObj;

        if (!JSApi.JSh_ArgvIsString(cx, vp, 1))
        {
            return(0);
        }

        string jsScriptName = JSApi.JSh_ArgvStringS(cx, vp, 1);

        JSComponent jsComp = go.AddComponent <JSComponent>();

        jsComp.jsScriptName = jsScriptName;
        jsComp.InitScript();
        JSApi.JSh_SetRvalObject(cx, vp, jsComp.go);
        return(1);
    }
예제 #2
0
    /*
     * GameObject.AddComponent<T>()
     */
    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
        {
            string jsComponentName = JSCache.GetMonoBehaviourJSComponentName(typeString);
            Type   jsComponentType = typeof(JSComponent);
            if (string.IsNullOrEmpty(jsComponentName))
            {
                Debug.LogWarning(string.Format("\"{0}\" has no JSComponent_XX. Use JSComponent instead.", typeString));
            }
            else
            {
                jsComponentType = JSDataExchangeMgr.GetTypeByName(jsComponentName, jsComponentType);
            }

            JSComponent jsComp = (JSComponent)go.AddComponent(jsComponentType);
            jsComp.jsClassName = typeString;
            jsComp.jsFail      = false;
            jsComp.init(true);
            jsComp.callAwake(); // Òªµ÷Óà js µÄ Awake

            //JSApi.JSh_SetRvalObject(vc.cx, vc.vp, jsComp.jsObj);
            JSApi.setObject((int)JSApi.SetType.Rval, jsComp.GetJSObjID(false));
        }
        return(true);
    }
예제 #3
0
    static void help_searchAndRetComs(JSVCall vc, JSComponent[] com, string typeString)
    {
        List<JSComponent> lst = new List<JSComponent>();
        foreach (var c in com)
        {
            if (c.jsClassName == typeString ||
                JSEngine.inst.IsInheritanceRel(typeString, c.jsClassName))
            {
                lst.Add(c);
            }
        }
        for (int i = 0; i < lst.Count; i++)
        {
            int jsObjID = lst[i].GetJSObjID(true);
            JSApi.setObject((int)JSApi.SetType.SaveAndTempTrace, jsObjID);
            JSApi.moveSaveID2Arr(i);
        }
        JSApi.setArrayS((int)JSApi.SetType.Rval, lst.Count, true);

//         var arrVal = new JSApi.jsval[lst.Count];
//         for (int i = 0; i < lst.Count; i++)
//         {
//             JSApi.JSh_SetJsvalObject(ref arrVal[i], lst[i].jsObj);
//         }
//         JSMgr.datax.setArray(JSDataExchangeMgr.eSetType.SetRval, arrVal);
    }
예제 #4
0
    // GameObject.AddComponent<T>()
    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
        {
            // TODO
            JSComponent jsComp = go.AddComponent <JSComponent>();
            jsComp.jsClassName = typeString;
            jsComp.jsFail      = false;
            jsComp.init(true);
            if (go.activeInHierarchy)
            {
                jsComp.callAwake(); // Òªµ÷Óà js µÄ Awake
                jsComp.callOnAwake();
                jsComp.callOnEnable();
            }

            //JSApi.JSh_SetRvalObject(vc.cx, vc.vp, jsComp.jsObj);
            JSApi.setObject((int)JSApi.SetType.Rval, jsComp.GetJSObjID(false));
        }
        return(true);
    }
    public static JSComponent s_AddComponent(GameObject go, string jsName)
    {
        JSComponent jsComp = go.AddComponent <JSComponent>();

        jsComp.jsClassName = jsName;
        jsComp.jsFail      = false;
        jsComp.init(true);
        jsComp.callAwake(); // 要调用 js 的 Awake
        return(jsComp);
    }
예제 #6
0
    static int GetJSComponent(IntPtr cx, uint argc, IntPtr vp)
    {
        if (argc != 1 && argc != 2)
        {
            return(0);
        }

        IntPtr jsObj = JSApi.JSh_ArgvObject(cx, vp, 0);
        object csObj = JSMgr.getCSObj(jsObj);

        if (csObj == null || !(csObj is GameObject))
        {
            return(0);
        }

        GameObject go = (GameObject)csObj;

        if (argc == 1)
        {
            JSComponent jsComp = go.GetComponent <JSComponent>();
            if (jsComp == null)
            {
                JSApi.JSh_SetRvalUndefined(cx, vp);
            }
            else
            {
                JSApi.JSh_SetRvalObject(cx, vp, jsComp.go);
            }
            return(1);
        }
        else
        {
            if (!JSApi.JSh_ArgvIsString(cx, vp, 1))
            {
                return(0);
            }

            string        jsScriptName = JSApi.JSh_ArgvStringS(cx, vp, 1);
            JSComponent[] jsComps      = go.GetComponents <JSComponent>();
            foreach (var v in jsComps)
            {
                if (v.jsScriptName == jsScriptName)
                {
                    JSApi.JSh_SetRvalObject(cx, vp, v.go);
                    return(1);
                }
            }
            JSApi.JSh_SetRvalUndefined(cx, vp);
            return(1);
        }
    }
예제 #7
0
    /// <summary>
    /// 根据 behav,给 go 添加一个 JSComponent_XX 组件
    /// </summary>
    /// <param name="go"></param>
    /// <param name="behav"></param>
    /// <returns></returns>
    public static JSComponent CreateJSComponentInstance(GameObject go, MonoBehaviour behav)
    {
        Type   type            = behav.GetType();
        string className       = GetJSComponentClassName(type);
        Type   jsComponentType = JSDataExchangeMgr.GetTypeByName(className, null);

        if (jsComponentType == null)
        {
            Debug.LogError(type.Name + " JSComponent not found!");
        }
        JSComponent ret = (JSComponent)go.AddComponent(jsComponentType);

        return(ret);
    }
예제 #8
0
 static void help_searchAndRetCom(JSVCall vc, JSComponent[] jsComs, string typeString)
 {
     int id = 0;
     foreach (var jsCom in jsComs)
     {
         if (jsCom.jsClassName == typeString ||
             JSEngine.inst.IsInheritanceRel(typeString, jsCom.jsClassName))
         {
             id = jsCom.GetJSObjID(true);
             break;
         }
     }
     JSApi.setObject((int)JSApi.SetType.Rval, id);
 }
예제 #9
0
    static int RemoveJSComponent(IntPtr cx, uint argc, IntPtr vp)
    {
        if (argc != 1 && argc != 2)
        {
            return(0);
        }

        IntPtr jsObj = JSApi.JSh_ArgvObject(cx, vp, 0);
        object csObj = JSMgr.getCSObj(jsObj);

        if (csObj == null || !(csObj is GameObject))
        {
            return(0);
        }

        GameObject go = (GameObject)csObj;

        if (argc == 1)
        {
            JSComponent jsComp = go.GetComponent <JSComponent>();
            if (jsComp != null)
            {
                UnityEngine.Object.Destroy(jsComp);
            }
            return(1);
        }
        else
        {
            if (!JSApi.JSh_ArgvIsString(cx, vp, 1))
            {
                return(0);
            }

            string        jsScriptName = JSApi.JSh_ArgvStringS(cx, vp, 1);
            JSComponent[] jsComps      = go.GetComponents <JSComponent>();
            foreach (JSComponent v in jsComps)
            {
                if (v.jsScriptName == jsScriptName)
                {
                    UnityEngine.Object.Destroy(v);
                    return(1);
                }
            }
            return(1);
        }
    }
예제 #10
0
    void SerializeData(JSComponent script)
    {
        int jsId = script.GetJSObjID(false);

        if (jsId == 0)
        {
            return;
        }

        if (Gos != null)
        {
            foreach (var n in Gos)
            {
                JSMgr.datax.setObject((int)JSApi.SetType.SaveAndTempTrace, n.v);
                SetField(jsId, n.name);
            }
        }
        if (Ints != null)
        {
            foreach (var n in Ints)
            {
                JSApi.setInt32((int)JSApi.SetType.SaveAndTempTrace, n.v);
                SetField(jsId, n.name);
            }
        }
        if (Strings != null)
        {
            foreach (var n in Strings)
            {
                JSApi.setStringS((int)JSApi.SetType.SaveAndTempTrace, n.v);
                SetField(jsId, n.name);
            }
        }
        if (Bools != null)
        {
            foreach (var n in Bools)
            {
                JSApi.setBooleanS((int)JSApi.SetType.SaveAndTempTrace, n.v);
                SetField(jsId, n.name);
            }
        }
    }
예제 #11
0
    public void InitAndDestroy()
    {
        if (!inited)
        {
            inited = true;

            JSComponent script = gameObject.AddComponent <JSComponent>();
            script.jsClassName = ScriptName;
            script.jsFail      = false;
            script.init(true);

            SerializeData(script);

            script.callAwake();
            script.callOnAwake();
            script.callOnEnable();

            Destroy(this);
        }
    }
예제 #12
0
    void Start()
    {
        // 这个函数是一个演示,他是整个应用程序的入口
        // 逻辑代码不能直接在Inspector中挂上去,那么总得有一个入口点,就是这里
        //
        //
        GameObject prefab = (GameObject)EditorEnv.LoadMainAssetAtPath("Assets/Prefabs/Login.prefab");
        GameObject go     = (GameObject)Instantiate(prefab);

        go.transform.SetParent(transform, false);
#if JS
        Instantiate(AssetDatabase.LoadMainAssetAtPath("Assets/Scripts/JSBinding/_JSEngine.prefab"));

        JSComponent jsComp = go.AddComponent <JSComponent>();
        jsComp.jsClassName = "Login";
        jsComp.jsFail      = false;
        jsComp.init(true);
        jsComp.callAwake(); // 要调用 js 的 Awake
#else
        go.AddComponent <Login>();
#endif
    }
예제 #13
0
    /// <summary>
    /// Get JSComponent ID of a GameObject by scriptName.
    /// </summary>
    /// <param name="go">The gameobject.</param>
    /// <param name="scriptName">Name of the script.</param>
    /// <returns></returns>
    public int GetGameObjectMonoBehaviourJSObj(GameObject go, string scriptName, out JSComponent component)
    {
        component = null;

        // go may be null
        // because the serialized MonoBehaviour can be null
        if (go == null)
        {
            return(0);
        }

        JSComponent[] jsComs = go.GetComponents <JSComponent>();
        foreach (var com in jsComs)
        {
            // NOTE: if a script bind to a GameObject twice, it will always return the first one
            if (com.jsClassName == scriptName)
            {
                component = com;
                return(com.GetJSObjID(false));
            }
        }
        return(0);
    }
예제 #14
0
 static void help_searchAndRetCom(JSVCall vc, JSComponent[] jsComs, string typeString)
 {
     int id = 0;
     foreach (var jsCom in jsComs)
     {
         if (jsCom.jsClassName == typeString)
         {
             id = jsCom.jsObjID;
             break;
         }
     }
     JSApi.setObject((int)JSApi.SetType.Rval, id);
 }