|
tolua# is a Unity lua static binder solution. the first solution that analyzes code by reflection and generates wrapper classes.
It is a Unity plugin that greatly simplifies the integration of C# code with Lua. which can automatically generate the binding code to access Unity from Lua and map c# constants, variables, functions, properties, classes, and enums to Lua.
tolua# grows up from cstolua. it's goal is to be a powerful development environment for Unity. github/tolua
All problems in computer science can be solved by another level of indirection, except of course for the problem of too many indirections tolua - CSDN博客
<hr/>ToLua基于LuaInterface,LuaInterface是一个实现lua和微软.Net平台的CLR混合编程的开源库,使得lua脚本可以实例化CLR对象,访问属性,调用方法甚至使用lua函数来处理事件。
ToLua保留了LuaInterface基本形式,重写或移除了部分内容,使代码更加简洁,提供了对Unity的支持、拓展了lua5.1.4源码。而最大的改进在于,LuaInterface中lua访问CLR需要运行时反射,对于游戏应用来说效率不够理想,ToLua则提供了一套中间层导出工具,对于需要访问的CLR、Unity及自定义类预生成wrap文件,lua访问时只访问wrap文件,wrap文件接收lua传递来的参数,进行类型(值、对象、委托)转换,再调用真正工作的CLR对象和函数,最后将返回值返回给lua,有效地提高了效率。
配置过程,略。
Figure 1. Assets
先开头,不定时更。
<hr/>GameObject: tolua-master\Assets\ToLua\Examples\12_GameObject
public class TestGameObject: MonoBehaviour
{
private string script =
@&#34;
local Color = UnityEngine.Color
local GameObject = UnityEngine.GameObject
local ParticleSystem = UnityEngine.ParticleSystem
function OnComplete()
print(&#39;OnComplete CallBack&#39;)
end
local go = GameObject(&#39;go&#39;)
go:AddComponent(typeof(ParticleSystem))
local node = go.transform
node.position = Vector3.one
print(&#39;gameObject is: &#39;..tostring(go))
--go.transform:DOPath({Vector3.zero, Vector3.one * 10}, 1, DG.Tweening.PathType.Linear, DG.Tweening.PathMode.Full3D, 10, nil)
--go.transform:DORotate(Vector3(0,0,360), 2, DG.Tweening.RotateMode.FastBeyond360):OnComplete(OnComplete)
GameObject.Destroy(go, 2)
go.name = &#39;123&#39;
--print(&#39;delay destroy gameobject is: &#39;..go.name)
&#34;;
public LuaState lua = null;
void Start()
{
#if UNITY_5 || UNITY_2017
Application.logMessageReceived += ShowTips;
#else
Application.RegisterLogCallback(ShowTips);
#endif
new LuaResLoader();
lua = new LuaState();
lua.LogGC = true;
lua.Start();
LuaBinder.Bind(lua);
lua.DoString(script, &#34;TestGameObject.cs&#34;);
}
void Update()
{
lua.CheckTop();
lua.Collect();
}
string tips = &#34;&#34;;
void ShowTips(string msg, string stackTrace, LogType type)
{
tips += msg;
tips += &#34;\r\n&#34;;
}
void OnApplicationQuit()
{
lua.Dispose();
lua = null;
#if UNITY_5 || UNITY_2017
Application.logMessageReceived -= ShowTips;
#else
Application.RegisterLogCallback(null);
#endif
}
void OnGUI()
{
GUI.Label(new Rect(Screen.width / 2 - 300, Screen.height / 2 - 300, 600, 600), tips);
}
}lua脚本实现最主要的部分代码:
lua.Start();
LuaBinder.Bind(lua);
lua.DoString(script, &#34;TestGameObject.cs&#34;);在&#39;private string script&#39;中,通过下面的代码,将lua与C#对应。
local Color = UnityEngine.Color
local GameObject = UnityEngine.GameObject
local ParticleSystem = UnityEngine.ParticleSystem
local go = GameObject(&#39;go&#39;)
对于Exmaple12给出的实例,通过GetType(), BaseType等函数,遍历ObjectPool中的所有object并得到其类型。
LuaState lua = go.lua;
foreach(var item in lua.translator.objectsBackMap)
{
object obj = item.Key;
Type s = obj.GetType();
Type z = s.BaseType;
}通过循环判断,实现遍历。
z == typeof(System.Object);
//or
//z.ToString == &#39;System.Object&#39;;
//or
//z.BaseType == null;在console中显示实现,类似于下图:
figure 2. Console
<hr/>LuaObjectPool & objectsBackMap ;
public readonly Dictionary<object, int> objectsBackMap = new Dictionary<object, int>(new CompareObject());
public readonly LuaObjectPool objects = new LuaObjectPool();ObjectPool:
public class LuaObjectPool
{
class PoolNode
{
public int index;
public object obj;
public PoolNode(int index, object obj)
{
this.index = index;
this.obj = obj;
}
}
private List<PoolNode> list;
private PoolNode head = null;
private int count = 0;
public LuaObjectPool()
{
list = new List<PoolNode>(1024);
head = new PoolNode(0, null);
list.Add(head);
list.Add(new PoolNode(1, null));
count = list.Count;
}
public object this[int i]
{
get
{
if (i > 0 && i < count)
{
return list.obj;
}
return null;
}
}
public void Clear()
{
list.Clear();
head = null;
count = 0;
}
public int Add(object obj)
{
int pos = -1;
if (head.index != 0)
{
pos = head.index;
list[pos].obj = obj;
head.index = list[pos].index;
}
else
{
pos = list.Count;
list.Add(new PoolNode(pos, obj));
count = pos + 1;
}
return pos;
}
public object TryGetValue(int index)
{
if (index > 0 && index < count)
{
return list[index].obj;
}
return null;
}
public object Remove(int pos)
{
if (pos > 0 && pos < count)
{
object o = list[pos].obj;
list[pos].obj = null;
list[pos].index = head.index;
head.index = pos;
return o;
}
return null;
}
public object Destroy(int pos)
{
if (pos > 0 && pos < count)
{
object o = list[pos].obj;
list[pos].obj = null;
return o;
}
return null;
}
public object Replace(int pos, object o)
{
if (pos > 0 && pos < count)
{
object obj = list[pos].obj;
list[pos].obj = o;
return obj;
}
return null;
}
}相关补充:
C# Memory Management for Unity Developers
Unity3D之对象池(Object Pool)
<hr/>待补:
tolua-master\Assets\ToLua\Examples 1-24 ;new LuaState() ; Update(){...} ;ObjectTranslator ;各类error: xxxx ;[del] GetType() & TypeOf() , BaseType ; [/del]C# Dictionary & Lua id ;......
|
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有账号?立即注册
×
|