Lua_ uLua_Lua和C#变量交互_018
2017-09-09 10:28
393 查看
反射方式
using UnityEngine; using System.Collections; using LuaInterface; public class AccessingLuaVariables01 : MonoBehaviour { private string script = @" luanet.load_assembly('UnityEngine') GameObject = luanet.import_type('UnityEngine.GameObject') ParticleSystem = luanet.import_type('UnityEngine.ParticleSystem') particles = {} for i = 1, Objs2Spawn, 1 do local newGameObj = GameObject('NewObj' .. tostring(i)) local ps = newGameObj:AddComponent(luanet.ctype(ParticleSystem)) --local ps = newGameObj:AddComponent('ParticleSystem') PS:Unity5.x已经废弃这种方式,Unity4.x可用-- ps:Stop() table.insert(particles, ps) end var2read = 42 "; // Use this for initialization void Start () { LuaState l = new LuaState(); // Assign to global scope variables as if they're keys in a dictionary (they are really) l["Objs2Spawn"] = 5; l.DoString(script); // Read from the global scope the same way print("Read from lua: " + l["var2read"].ToString()); // Get the lua table as LuaTable object LuaTable particles = (LuaTable)l["particles"]; // Typical foreach over values in table foreach( ParticleSystem ps in particles.Values ) { ps.Play(); } } // Update is called once per frame void Update () { } }
非反射方式
using UnityEngine; using System.Collections; using LuaInterface; public class LuaTest02 : MonoBehaviour { string var=@"sum =10"; string script =@" ParticleSystem = UnityEngine.ParticleSystem; GameObject = UnityEngine.GameObject; for i=1,sum, 1 do obj = GameObject('obj'); obj:AddComponent(ParticleSystem:GetClassType()) end "; // Use this for initialization void Start () { LuaScriptMgr mgr = new LuaScriptMgr(); mgr.Start();//必须有这句话,否则Lua脚本无法正常执行 LuaState l = mgr.lua; l.DoString(var); l.DoString(script); } // Update is called once per frame void Update () { } }
相关文章推荐
- Lua_ uLua_Lua和C#变量交互_018
- Tolua使用笔记二:lua与C#的函数,变量交互方法
- Unity3d ulua c#与lua交互+wrap文件理解
- Unity3d ulua c#与lua交互+wrap文件理解
- Tolua使用笔记二:lua与C#的函数,变量交互方法
- 在C#环境中与Lua进行交互(一)
- javascript与cs代码互相调用 asp.net中前台javascript与后台C#交互 这里主要包括了javascipt与后台CS代码四种方法互调(其中包括函数与变量的访问)
- 用好Lua+Unity,让性能飞起来——Lua与C#交互篇
- 在C#环境中与Lua进行交互(二)
- 用好Lua+Unity,让性能飞起来——Lua与C#交互篇
- 【Unity+Lua】实测如何性能优化(Lua和C#交互篇)
- [C#/Lua]C#和Lua交互的Demo
- Lua_ uLua_C#调用Lua方法_019
- c# lua 简单交互
- c# lua 简单交互
- lua 与c#交互原理
- Lua_ uLua_Lua调用C#方法_020
- Lua_ uLua_C#中使用Lua_016
- 用好lua+unity,让性能飞起来——lua与c#交互篇
- ulua里如何将C#创建的UnityEngine对象返回给lua