Exemple #1
0
        // @desc 指定したGameObjectに貼り付いているLuaScriptの関数を呼び出す
        // @decl [result] kick(self, funcname[, args])
        // @param funcname 呼び出したい関数名
        // @param args 渡したい引数(いくつでも)
        // @result 呼び出した関数による
        // @sample g:kick("func")
        // @sample g:kick("some_args", 0, 1, 2)
        // @sample local r = g:kick("result")
        // @sample local a, b = g:kick("multi_results")
        private static int L_kick(ILuaState lua)
        {
            check_identifier(lua, 1, klassName);
            GameObject obj  = get_internal <GameObject>(lua, 1);
            string     name = lua.ToString(2);
            int        narg = lua.GetTop() - 2;

            lua.NewThread();
            ILuaState sublua = lua.ToThread(-1);

            lua.Pop(1);
            LuaScriptBehaviour lsb = obj.GetComponent <LuaScriptBehaviour>();

            lsb.PushSelfTable(sublua);
            for (int i = 0; i < narg; ++i)
            {
                lua.PushValue(3 + i);
            }
            if (narg > 0)
            {
                lua.XMove(sublua, narg);
            }
            int nresult = lsb.InvokeScriptFunction(sublua, name, narg);

            sublua.XMove(lua, nresult);
            sublua.SetTop(0);
            return(nresult);
        }
Exemple #2
0
 //
 void OnEnable()
 {
     _lsb             = (LuaScriptBehaviour)target;
     _gameObject      = _lsb.gameObject;
     _fileList        = new FileList("StreamingAssets/LuaRoot");
     _fileSelectIndex = _fileList.Find(_lsb.luaScriptFile);
 }
Exemple #3
0
 //
 public LuaThread(ILuaState lua, LuaScriptBehaviour behaviour)
 {
     lua.NewThread();
     _luaState = lua.ToThread(-1);
     lua.Pop(1);
     _phase = Phase.NONE;
 }
Exemple #4
0
        // @desc このスクリプトが動いているMonoBehaviourが貼り付いているGameObjectを取得する
        // @decl GameObject current()
        // @result GameObject
        // @sample local g = GameObject.current()
        private static int L_current(ILuaState lua)
        {
            LuaScriptBehaviour lsb = LuaScriptBehaviour.FindFromState(lua);
            GameObject         obj = lsb.gameObject;

            PushNew(lua, obj);
            return(1);
        }
Exemple #5
0
        // @desc スレッドを開始する
        // @param function スレッドで実行する関数
        // @result スレッドインスタンス
        // @sample local th = lthread.start(function() print("HOGE") end)
        private static int L_lthread_start(ILuaState lua)
        {
            LuaScriptBehaviour lsb = LuaScriptBehaviour.FindFromState(lua);
            LuaThread          th  = lsb.StartThread(lua);

            NewLuaThread(lua, th);
            return(1);
        }
Exemple #6
0
        // @class util
        // @obsolete
        // @desc timeout
        // @decl (time, func)
        // @param time 遅延させる時間(秒)
        // @param func 関数
        // @sample util.timeout(5, function() print("5sec") end)
        private static int L_timeout(ILuaState lua)
        {
            double             time = lua.ToNumber(1);
            LuaScriptBehaviour lsb  = LuaScriptBehaviour.FindFromState(lua);
            Element            e    = new Element();

            e._fid  = lsb.ExportAnonymousFunction(lua);
            e._time = (float)time;
            e._lua  = lua;
            _instance._timeouts.Add(e);
            return(0);
        }
Exemple #7
0
 public void Update()
 {
     for (int i = 0; i < _timeouts.Count; ++i)
     {
         Element e = _timeouts[i];
         e._time -= Time.deltaTime;
         if (e._time <= 0.0f)
         {
             LuaScriptBehaviour lsb = LuaScriptBehaviour.FindFromState(e._lua);
             lsb.InvokeAnonymousFunction(e._lua, e._fid, 0, 0);
         }
     }
     _timeouts.RemoveAll(e => e._time <= 0.0f);
 }
Exemple #8
0
 void OnDisable()
 {
     _lsb        = null;
     _gameObject = null;
 }
Exemple #9
0
        //
        public LuaThread CreateThread(LuaScriptBehaviour behaviour)
        {
            LuaThread th = new LuaThread(_luaState, behaviour);

            return(th);
        }
Exemple #10
0
        // -- describing object oriented class in Lua
        // Test = {}
        // function Test:new()
        //   o = {}
        //   setmetatable(o, self)
        //   self.__index = self
        //   return o
        // end
        // function Test:hoge()
        //   return 3
        // end
        // NewTest = Test:new()
        // function NewTest:hoge()
        //   return 4
        // end
        public static int PushNew(ILuaState lua, GameObject obj)
        {
            LuaVM.Assert(lua, obj != null);
            LuaGameObjectCache cache = obj.GetComponent <LuaGameObjectCache>();

            if (cache != null && cache.index == LuaConstants.LUA_NOREF)
            {
                Object.Destroy(cache);
                cache = null;
            }
            if (cache == null)
            {
                cache = obj.AddComponent <LuaGameObjectCache>();
                // o = {}
                lua.NewTable();
                // setmetatable(o, self)
                //lua.PushValue(-2); // self
                lua.GetGlobal(klassName);
                lua.SetMetaTable(-2); // -> o
                // self.__index = self
                lua.GetMetaTable(-1);
                lua.SetField(-1, "__index");
                // return o
                set_internal <GameObject>(lua, -1, obj);
                set_boolean(lua, -1, "is_prefab", false);
                //
                LuaScriptBehaviour lsb = obj.GetComponent <LuaScriptBehaviour>();
                if (lsb != null)
                {
                    LuaComponentBehaviour.PushNew(lua, lsb);
                    lua.SetField(-2, "behaviour");
                }
                LuaComponentTransform.PushNew(lua, obj.transform);
                lua.SetField(-2, "transform");
                RectTransform rt = obj.GetComponent <RectTransform>();
                if (rt != null)
                {
                    LuaComponentRectTransform.PushNew(lua, rt);
                    lua.SetField(-2, "rect_transform");
                }
                LuaComponentUIText.PushNew(lua, obj);
                lua.SetField(-2, "ui_text");
                LuaComponentUIImage.PushNew(lua, obj);
                lua.SetField(-2, "ui_image");
                if (constructorHook != null)
                {
                    constructorHook(lua, obj);
                }
                lua.PushValue(-1);
                int refidx = lua.L_Ref(LuaDef.LUA_REGISTRYINDEX);
                cache.index    = refidx;
                cache.luaState = lua;
            }
            lua.RawGetI(LuaDef.LUA_REGISTRYINDEX, cache.index);
            //Debug.Log(string.Format("Find {0} {1}", objid, obj.name));
            if (get_internal <GameObject>(lua, -1) == null)
            {
                set_internal <GameObject>(lua, -1, obj);
            }
            return(1);
        }