상속: SLua.LuaSvr
    /**
     * Load an lua file.
     *
     * @param string strFile - The file name without extension.
     * @return bool - true if success, otherwise false.
     */
    public bool DoFile(string strFile)
    {
        if (string.IsNullOrEmpty(strFile))
        {
            return(false);
        }

        // Try to do file.
        try
        {
            // The lua file return the table itself.
            object cChunk = YwLuaScriptMng.Instance.LuaSvr.luaState.doFile(strFile);
            if ((null == cChunk) || !(cChunk is LuaTable))
            {
                return(false);
            }

            // Remember lua table.
            m_cLuaTableOpt = new YwLuaTable((LuaTable)cChunk);
            return(true);
        }
        catch (System.Exception e)
        {
            YwDebug.LogError(YwLuaSvr.FormatException(e));
        }

        return(false);
    }
예제 #2
0
    public void Initialize()
    {
        YwLuaSvr cLuaSvr = LuaSvr;

        if (!cLuaSvr.Initialize(OnInitializeProgress, OnInitializedOk, false))
        {
            YwDebug.LogError("The lua environment can not be initialized!");
            return;
        }
    }
예제 #3
0
    /**
     * Initialize the lua script environment.
     *
     * @param void.
     * @return void.
     */
    public void Initialize()
    {
        YwLuaSvr cLuaSvr = LuaSvr;

        if (!cLuaSvr.Initialize(OnInitializeProgress, OnInitializedOk, LuaSvrFlag.LSF_BASIC | LuaSvrFlag.LSF_EXTLIB))
        {
            YwDebug.LogError("The lua environment can not be initialized!");
            return;
        }
    }
    /**
     * Call a lua method.
     *
     * @param ref LuaFunction cResFunc - The result of the function, if the lua function calls ok, if it is not null, will call it instead of look up from table by strFunc.
     * @param string strFunc - The function name.
     * @param params object[] aParams - The params.
     * @return object - The number of result.
     */
    public object CallMethod(ref LuaFunction cResFunc, string strFunc, params object[] aParams)
    {
        // Check function first.
        if (null == cResFunc)
        {
            // Check params.
            if (string.IsNullOrEmpty(strFunc))
            {
                return(null);
            }

            // Check table.
            if (!Valid)
            {
                return(null);
            }

            // Check function.
            object cFuncObj = m_cLuaTable[strFunc];
            if ((null == cFuncObj) || !(cFuncObj is LuaFunction))
            {
                return(null);
            }

            // Get function.
            cResFunc = (LuaFunction)cFuncObj;
            if (null == cResFunc)
            {
                return(null);
            }
        }

        // Try to call this method.
        try
        {
            if (null == aParams)
            {
                return(cResFunc.call());
            }
            else
            {
                return(cResFunc.call(aParams));
            }
        }
        catch (Exception e)
        {
            YwDebug.LogError(YwLuaSvr.FormatException(e));
            cResFunc = null;
            return(null);
        }
    }
예제 #5
0
    /**
     * Update method.
     *
     * @param void.
     * @return void.
     */
    public void Update()
    {
        // The main update logic entry.
        if (null == m_cUpdateFunc)
        {
            return;
        }

        // Try to call update.
        try
        {
            m_cUpdateFunc.call();
        }
        catch (System.Exception e)
        {
            YwDebug.LogError(YwLuaSvr.FormatException(e));
        }
    }
예제 #6
0
    /**
     * The initializing complete callback event.
     *
     * @param void.
     * @return void.
     */
    private void OnInitializedOk()
    {
        YwLuaSvr cLuaSvr = LuaSvr;

        if (!cLuaSvr.inited)
        {
            YwDebug.LogError("Create lua server failed!");
            return;
        }

        cLuaSvr.start("Main");

        // Get update function.
        m_cUpdateFunc = cLuaSvr.GetFunction("Update");
        if (null == m_cUpdateFunc)
        {
            YwDebug.LogError("There is no update function in main file! Are you missing \'Update()\'?");
            return;
        }

        // Get late update function.
        m_cLateUpdateFunc = cLuaSvr.GetFunction("LateUpdate");
        if (null == m_cLateUpdateFunc)
        {
            YwDebug.LogError("There is no late update function in main file! Are you missing \'LateUpdate()\'?");
            return;
        }

        // Get fixed update function.
        m_cFixedUpdateFunc = cLuaSvr.GetFunction("FixedUpdate");
        if (null == m_cFixedUpdateFunc)
        {
            YwDebug.LogError("There is no fixed update function in main file! Are you missing \'FixedUpdate()\'?");
            return;
        }

        // Get lite update function.
        m_cLiteUpdateFunc = cLuaSvr.GetFunction("LiteUpdate");
        if (null == m_cLiteUpdateFunc)
        {
            YwDebug.LogError("There is no lite update function in main file! Are you missing \'LiteUpdate()\'?");
            return;
        }
    }
    /**
     * Create a lua class instance for monobehavior instead of do a file.
     *
     * @param string strFile - The lua class name.
     * @return bool - true if success, otherwise false.
     */
    public bool CreateClassInstance(string strClassName)
    {
        if (string.IsNullOrEmpty(strClassName))
        {
            return(false);
        }

        // Try to get global lua class.
        try
        {
            // Get class first.
            LuaTable cClsTable = (LuaTable)YwLuaScriptMng.Instance.LuaSvr.luaState[strClassName];
            if (null == cClsTable)
            {
                return(false);
            }

            // Get "new" method of the lua class to create instance.
            LuaFunction cNew = (LuaFunction)cClsTable["new"];
            if (null == cNew)
            {
                return(false);
            }

            // We choose no default init parameter for constructor.
            object cInsChunk = cNew.call();
            if (null == cInsChunk)
            {
                return(false);
            }

            // If we create instance ok, use it as table.
            m_cLuaTableOpt = new YwLuaTable((LuaTable)cInsChunk);
            return(true);
        }
        catch (System.Exception e)
        {
            YwDebug.LogError(YwLuaSvr.FormatException(e));
        }

        return(false);
    }
    /**
     * Update method.
     *
     * @param void.
     * @return void.
     */
    public void Update()
    {
        // The main update logic entry.
        if (null == m_cUpdateFunc)
        {
            return;
        }

        // Try to call update.
        //UWAEngine.PushSample("YwLuaScriptMng.Test.Update");
        try
        {
            m_cUpdateFunc.call();
        }
        catch (System.Exception e)
        {
            YwDebug.LogError(YwLuaSvr.FormatException(e));
        }
        //UWAEngine.PopSample();
    }