|
项目结束,目前无事,家中老人甚是思念,已经思考准备回京,不准备继续留沪了,也坚持到项目结束了,虽然还没上线,不过该做的也都做了,百善孝为先,架不住老人连续一年多的思念,准备离职了,点开到这个公司的第一篇文档,思绪万千,想起当年还在实习的时候,写的C#连接XLUA的文档,发出来玩玩,- using System;
- using System.IO;
- using UnityEngine;
- using XLua;
- using System.Text;
- public class GameStart : MonoBehaviour
- {
- //更改可识别文件后缀文件后缀,这样文件的后缀名就可以直接写成.lua,不必写成.txt了
- byte[] CustomLoader(ref string fileName)
- {
- fileName = fileName.Replace(".", "/");
- string luapath = Application.dataPath + "/LuaSource/" + fileName + ".lua";
- string strLuaContent = File.ReadAllText(luapath);
- byte[] result = Encoding.UTF8.GetBytes(strLuaContent);
- return result;
- }
- public LuaEnv luaEnv;
- private LuaTable scriptEnv;
- private Action luaStart;
- private Action luaUpdate;
- private Action<Collider> luaOnTriggerEnter; //触发
- private Action<Collision> luaOnCollisionEnter; //碰撞
- private Action luaOnDestroy;
- internal static float lastGCTime = 0;
- internal const float GCInterval = 1;
- void Awake()
- {
- luaEnv = new LuaEnv();
- scriptEnv = luaEnv.NewTable();
- LuaTable meta = luaEnv.NewTable();
- meta.Set("__index", luaEnv.Global);
- scriptEnv.SetMetaTable(meta);
- meta.Dispose();
- scriptEnv.Set("self", this);
- luaEnv.AddLoader(CustomLoader);
- luaEnv.AddBuildin("rapidjson", XLua.LuaDLL.Lua.LoadRapidJson); //读取json
- luaEnv.DoString("require('main')", "GameStart", scriptEnv);
- scriptEnv.Get("Start", out luaStart);
- scriptEnv.Get("Update", out luaUpdate);
- scriptEnv.Get("OnTriggerEnter", out luaOnTriggerEnter); //触发
- scriptEnv.Get("OnCollisionEnter", out luaOnCollisionEnter); //碰撞
- scriptEnv.Get("OnDestroy", out luaOnDestroy);
- }
- void Start()
- {
- if (luaStart != null)
- {
- luaStart();
- }
- }
- void Update()
- {
- try
- {
- if (luaUpdate != null)
- {
- luaUpdate();
- }
- if (Time.time - GameStart.lastGCTime > GCInterval)
- {
- luaEnv.Tick();
- GameStart.lastGCTime = Time.time;
- }
- }
- catch
- {
- // print("已经没怪了");
- }
- }
- private void OnTriggerEnter(Collider other)
- {
- if (luaOnTriggerEnter != null)
- {
- luaOnTriggerEnter(other);
- }
- }
- private void OnCollisionEnter(Collision collision)
- {
- if (luaOnCollisionEnter != null)
- {
- luaOnCollisionEnter(collision);
- }
- }
- private void OnDestroy()
- {
- if (luaOnDestroy != null)
- {
- luaOnDestroy();
- }
- luaOnCollisionEnter = null;
- luaOnTriggerEnter = null;
- luaUpdate = null;
- luaStart = null;
- luaOnDestroy = null;
- scriptEnv.Dispose();
- }
- }
复制代码 |
|