Esempio n. 1
0
 public static byte[] ReadByteForLua(string fileName)
 {
                 #if UNITY_EDITOR
     return(System.IO.File.ReadAllBytes(PathTools.Combine(Application.dataPath + "/Resources", fileName)));
                 #else
     return(ReadRes.ReadByte(fileName));
                 #endif
 }
Esempio n. 2
0
        public virtual void Init()
        {
            if (scriptEnv != null)
            {
                return;
            }

            if (string.IsNullOrEmpty(luaPath))
            {
                luaFilePathDic.TryGetValue(gameObject, out luaPath);
                luaFilePathDic.Remove(gameObject);
            }

            if (string.IsNullOrEmpty(luaPath))
            {
                Debug.LogError(" luaPath = null !");
                return;
            }

            if (!luaPath.Trim().EndsWith(".lua"))
            {
                luaPath = luaPath + ".lua";
            }

            string luaCode;

            string filePath;

            byte[] code = null;

                        #if UNITY_EDITOR
            /* 在编辑器下从 Resources 下加载 */
            filePath = Application.dataPath + "/Resources/" + luaPath;
            code     = System.IO.File.ReadAllBytes(filePath);
            Debug.LogWarningFormat("LuaBaseBehaviour load file {0}", filePath);
                        #else
            //filePath = Application.persistentDataPath + "/" + luaPath;
            code = ReadRes.ReadByte(luaPath);
                        #endif

            if (code == null)
            {
                Debug.LogError("lua code is null " + luaPath);
                return;
            }

            luaEnv = LuaManager.GetInstance().LuaEnvGetOrNew();

            scriptEnv = luaEnv.NewTable();

            LuaTable meta = luaEnv.NewTable();
            meta.Set("__index", luaEnv.Global);
            scriptEnv.SetMetaTable(meta);
            meta.Dispose();

            scriptEnv.Set("self", this);
            scriptEnv.Set("scriptEnv", scriptEnv);

            if (injections != null)
            {
                foreach (var injection in injections)
                {
                    scriptEnv.Set(injection.name, injection.value);
                }
            }

            scriptEnv.Set("luaPath", luaPath);

            luaEnv.DoString(code, "LuaBaseBehaviour", scriptEnv);

            //luaEnv.DoString(string.Format("require '{0}'",luaPath), "LuaBaseBehaviour_"+gameObject.GetInstanceID(), scriptEnv);
            //luaEnv.DoString("function awake()\n\nend\t\n\nfunction start()\n\tprint(\"LuaBaseBehaviour start...\"..self.luaPath)\nend\n\nfunction update()\n\tlocal r = CS.UnityEngine.Vector3.up * CS.UnityEngine.Time.deltaTime\n\tself.transform:Rotate(r)\nend\n\nfunction ondestroy()\n    print(\"LuaBaseBehaviour destroy...\"..self.luaPath)\nend", "LuaBaseBehaviour", scriptEnv);

            scriptEnv.Get("awake", out luaAwake);

            /*
             * if (luaAwake == null) {
             *      Debug.LogError ("awake null");
             * } else {
             *      Debug.LogError ("awake ");
             * }
             */

            //#if UNITY_EDITOR
            if (luaAwake != null && !luaAwakeInited)
            {
                luaAwake();
                luaAwakeInited = true;
            }
            //#endif

            scriptEnv.Get("start", out luaStart);
            //scriptEnv.Get("update", out luaUpdate);
            scriptEnv.Get("ondestroy", out luaOnDestroy);
        }
Esempio n. 3
0
 public static byte[] ReadByte(string fileName)
 {
     return(ReadRes.ReadByte(fileName));
 }