您的位置:首页 > 编程语言 > Lua

LuaManager实现

2015-10-16 15:33 519 查看
LuaManager.cs用于管理所有Lua脚本,并添加一些Lua脚本中用到的方便在C#中使用的公共方法以及简单的Lua消息实现。

</pre>using UnityEngine;using System.Collections;using System.Collections.Generic;using LuaInterface;using System.IO;using System;public class LuaManager : MonoBehaviour{    public static LuaManager instance { get; private set; }    private List<LuaComponent> luaList = new List<LuaComponent>();    private Dictionary<string, List<LuaMessage>> luaMessageDiction = new Dictionary<string, List<LuaMessage>>();    public LuaState luaState { get; private set; }    public string luaPath    {        get { return Application.dataPath + "/Resources/Lua/"; }    }    void Start()    {        instance = this;        luaState = new LuaState();        RunLuaScripts("LuaSetting");    }    void OnLevelWasLoaded()    {        luaMessageDiction.Clear();        LuaGC();    }    public GameObject FindChildObject(GameObject obj, string name)    {        Transform trans = null;        int count = obj.transform.childCount;        for (int i = 0; i < count; i++)        {            trans = obj.transform.GetChild(i);            if (trans.name == name)                return trans.gameObject;        }        return null;    }    public Transform FindTransformInChilds(Transform root, string name)    {        for (int i = 0; i < root.transform.childCount; i++)        {            Transform trans = root.transform.GetChild(i);            if (trans.name.Equals(name))                return trans;            else                return FindTransformInChilds(trans, name);        }        return null;    }    public RewardConfig GetLevelRewardConfig(int id)    {        return ConfigManager.instance.GetConfig<RewardConfig>(ConfigTypeEnum.Reward, id);    }    public TotalconsumemoneyrewardConfig GetEliminationConfig()    {        Dictionary<int, ConfigDataBase> map = ConfigManager.instance.GetConfig(ConfigTypeEnum.Totalconsumemoneyreward);        foreach (KeyValuePair<int, ConfigDataBase> pair in map)        {            TotalconsumemoneyrewardConfig config = pair.Value as TotalconsumemoneyrewardConfig;            return config;        }        return null;    }    public ItemConfig GetItemConfig(int id)    {        return ConfigManager.instance.GetConfig<ItemConfig>(ConfigTypeEnum.Item, id);    }    public Component GetComponentInChildren(GameObject obj, string name)    {        Type type = Type.GetType(name);        return obj.GetComponentInChildren(type);    }    public LuaComponent GetLuaBase(string name)    {        for (int i = 0; i < luaList.Count; i++)        {            if (luaList[i].name.Equals(name))                return luaList[i];        }        return null;    }    public void AddLuaBass(LuaComponent lua)    {        if (!luaList.Contains(lua))            luaList.Add(lua);    }    public object[] RunLuaScripts(string name)    {        string path = "Lua/" + name;        UnityEngine.Object text = Resources.Load(path);        TextAsset textAsset = (TextAsset)text;        object[] luaRet = luaState.DoString(textAsset.text);        return luaRet;    }    public void LuaDispatch(string key, LuaTable table)    {        object[] args = new object[table.Keys.Count];        for (int i = 0; i < table.Keys.Count; i++)        {            args[i] = table[i];        }        if (luaMessageDiction.ContainsKey(key))        {            List<LuaMessage> funcList = luaMessageDiction[key];            if (funcList == null || funcList.Count == 0)                return;            for (int i = 0; i < funcList.Count; i++)            {                funcList[i].CallLuaFunction(args);            }        }    }    public void LuaAddListener(string key, GameObject obj, string funcName)    {        LuaComponent lua = obj.GetComponent<LuaComponent>();        LuaFunction func = lua.luaModule[funcName] as LuaFunction;        if (func == null)        {            Debug.LogError("this " + funcName + " not exist");            return;        }        LuaMessage luaMessage = new LuaMessage(lua, funcName);        if (luaMessageDiction.ContainsKey(key))        {            List<LuaMessage> funcList = luaMessageDiction[key];            if (!funcList.Contains(luaMessage))                funcList.Add(luaMessage);        }        else        {            List<LuaMessage> funcList = new List<LuaMessage>();            funcList.Add(luaMessage);            luaMessageDiction.Add(key, funcList);        }    }    public void LuaRemoveListener(string key, GameObject obj, string funcName)    {        LuaComponent lua = obj.GetComponent<LuaComponent>();        LuaMessage luaMessage = new LuaMessage(lua, funcName);        if (luaMessageDiction.ContainsKey(key))        {            if (luaMessageDiction[key].Contains(luaMessage))                luaMessageDiction[key].Remove(luaMessage);        }    }    public void LuaRemoveAllListener(string key, GameObject obj, string funcName)    {        if (luaMessageDiction.ContainsKey(key))        {            luaMessageDiction.Remove(key);        }    }    public void LuaGC()    {        LuaDLL.lua_gc(luaState.L, LuaGCOptions.LUA_GCCOLLECT, 0);    }}public class LuaMessage{    public LuaComponent luaBase;    public string funcName;    public LuaMessage(LuaComponent luaBase, string funcName)    {        this.luaBase = luaBase;        this.funcName = funcName;    }    public object[] CallLuaFunction(params object[] args)    {        return luaBase.CallLuaFunction(funcName, args);    }}<pre name="code" class="csharp">


在Start方法中执行了LuaSetting.lua即初始化Lua默认加载路径,require "Base"将Base.lua读入内存中,后面发现在IOS下不能使用Lua的require,只能改成在C#中执行DoString的方法读取Lua脚本。

Base.lua

luanet.load_assembly('UnityEngine')
luanet.load_assembly('Assembly-CSharp')
luanet.load_assembly('System')
luanet.load_assembly('HOTween')

HOTween = luanet.import_type('Holoville.HOTween.HOTween')
TweenParms = luanet.import_type('Holoville.HOTween.TweenParms')
Sequence = luanet.import_type('Holoville.HOTween.Sequence')
EaseType = luanet.import_type('Holoville.HOTween.EaseType')
Vector3 = luanet.import_type('UnityEngine.Vector3')
Vector2 = luanet.import_type('UnityEngine.Vector2')
Camera = luanet.import_type('UnityEngine.Camera')
GameObject = luanet.import_type('UnityEngine.GameObject')
Resources = luanet.import_type('UnityEngine.Resources')
Object = luanet.import_type('UnityEngine.Object')
Debug = luanet.import_type('UnityEngine.Debug')
object = luanet.import_type('System.object')
Component = luanet.import_type('UnityEngine.Component')
Transform = luanet.import_type('UnityEngine.Transform')
Type = luanet.import_type('System.Type')
MonoBehaviour = luanet.import_type('UnityEngine.MonoBehaviour')
Instantiate = luanet.import_type('UnityEngine.Instantiate')
UIScrollView = luanet.import_type('UIScrollView')
UIGrid = luanet.import_type('UIGrid')
UILabel = luanet.import_type('UILabel')
UISprite = luanet.import_type('UISprite')
UIEventListener = luanet.import_type('UIEventListener')
EventDelegate = luanet.import_type('EventDelegate')
Destroy = luanet.import_type('UnityEngine.Destroy')
DestroyImmediate = luanet.import_type('UnityEngine.DestroyImmediate')
Dictionary = luanet.import_type('System.Collections.Generic.Dictionary')
Message = luanet.import_type('Message')

LuaComponent = luanet.import_type('LuaComponent')
LuaManager = luanet.import_type('LuaManager')
MessageManager = luanet.import_type('MessageManager')
UserManager = luanet.import_type('UserManager')
Utility = luanet.import_type('Utility')
ConfigManager = luanet.import_type('ConfigManager')
ConfigDataBase = luanet.import_type('ConfigDataBase')
ConfigTypeEnum = luanet.import_type('ConfigTypeEnum')
UIConfig = luanet.import_type('UIConfig')
Config = luanet.import_type('Config')
UnityConfig = luanet.import_type('UnityConfig')
MsgType = luanet.import_type('MsgType')
ViewType = luanet.import_type('ViewType')
ErrorCodeEnum = luanet.import_type('ErrorCodeEnum')
ScreenLockManager = luanet.import_type('ScreenLockManager')

function string.split(str, delimiter)
    if str == nil or str == '' or delimiter == nil then
        return nil
    end

    local result = { }
    for match in(str .. delimiter):gmatch("(.-)" .. delimiter) do
        table.insert(result, match)
    end
    return result
end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: