示例#1
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);
        }
示例#2
0
        IEnumerator CopyToPersistentDataPath()
        {
            string dataPath = Application.persistentDataPath;       //Util.DataPath;  //数据目录
            string resPath  = PathTools.AppContentPath();           //Util.AppContentPath(); //游戏包资源目录

            if (Directory.Exists(dataPath))
            {
                Directory.Delete(dataPath, true);
            }
            Directory.CreateDirectory(dataPath);

            string infile  = resPath + "md5filelist.txt";
            string outfile = dataPath + "md5filelist.txt";

            if (File.Exists(outfile))
            {
                File.Delete(outfile);
            }

            string message = "正在解包文件:>files.txt";

            Debug.Log(infile);
            Debug.Log(outfile);

            if (Application.platform == RuntimePlatform.Android)
            {
                WWW www = new WWW(infile);

                while (true)
                {
                    if (www.isDone || !string.IsNullOrEmpty(www.error))
                    {
                        System.Threading.Thread.Sleep(50);
                        if (!string.IsNullOrEmpty(www.error))
                        {
                            Debug.LogError(www.error);
                        }
                        else
                        {
                            File.WriteAllBytes(outfile, www.bytes);
                        }
                        break;
                    }
                }
                yield return(0);
            }
            else
            {
                File.Copy(infile, outfile, true);
            }
            yield return(new WaitForEndOfFrame());

            //释放所有文件到数据目录
            string[] files = File.ReadAllLines(outfile);
            foreach (var file in files)
            {
                string[] fs = file.Split('=');

                if (fs.Length == 1)
                {
                    Debug.LogWarning("跳过 >>" + file);
                    continue;
                }

                if (resPath.EndsWith("/") && fs [0].StartsWith("/"))
                {
                    resPath = resPath.Substring(0, resPath.Length - 1);
                }

                if (dataPath.EndsWith("/") && fs [0].StartsWith("/"))
                {
                    dataPath = dataPath.Substring(0, dataPath.Length - 1);
                }

                infile  = resPath + fs[0];                   //
                outfile = dataPath + fs[0];

                message = "正在解包文件:>" + fs[0];
                Debug.Log("正在解包文件:>" + infile);

                string dir = Path.GetDirectoryName(outfile);
                if (!Directory.Exists(dir))
                {
                    Directory.CreateDirectory(dir);
                }

                if (Application.platform == RuntimePlatform.Android)
                {
                    WWW www = new WWW(infile);

                    while (true)
                    {
                        if (www.isDone || !string.IsNullOrEmpty(www.error))
                        {
                            System.Threading.Thread.Sleep(50);
                            if (!string.IsNullOrEmpty(www.error))
                            {
                                Debug.LogError(www.error);
                            }
                            else
                            {
                                File.WriteAllBytes(outfile, www.bytes);
                                Debug.LogWarning(">>" + outfile + ">>" + www.bytes.Length);
                            }
                            break;
                        }
                    }

                    /*
                     * yield return www;
                     *
                     * if (www.isDone) {
                     *      File.WriteAllBytes(outfile, www.bytes);
                     *      Debug.LogWarning (">>" + outfile+">>"+www.bytes.Length);
                     * }
                     */
                    yield return(0);
                }
                else
                {
                    if (File.Exists(outfile))
                    {
                        File.Delete(outfile);
                    }
                    File.Copy(infile, outfile, true);
                }
                yield return(new WaitForEndOfFrame());
            }
            message = "解包完成!!!";

            luaManager = LuaManager.GetInstance();
            luaManager.InitDoString = "require 'lua/game/GameState' \n game_state_init() \n";
            env = luaManager.LuaEnvGetOrNew();

            yield return(new WaitForSeconds(1f));

            RunState();

            isCopyCmp = true;
        }
示例#3
0
 void OnDestroy()
 {
     env        = null;
     luaManager = null;
 }
示例#4
0
 static public void SetLateUpdateFun(string luaLateUpdateFunName)
 {
     LuaManager.GetInstance().SetLuaLateUpdate(luaLateUpdateFunName);
 }