tolua篇:
我们目前开发采用的是tolua,大家可能都知道c#到lua是需要导出接口给lua调用的,只要lua获取unity的对象,那么tolua都会根据id(唯一)保存在 LuaState中的 ObjectTranslator 中,每个lua变量都是唯一对应的,参考图
#define MISS_WARNING
using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Runtime.InteropServices;
using System.Runtime.CompilerServices;
using System.Text;
using UnityEngine;
namespace LuaInterface
{
public class LuaState : LuaStatePtr, IDisposable
{
public ObjectTranslator translator = new ObjectTranslator();
public LuaReflection reflection = new LuaReflection();
public int ArrayMetatable { get; private set; }
public int DelegateMetatable { get; private set; }
public int TypeMetatable { get; private set; }
public int EnumMetatable { get; private set; }
public int IterMetatable { get; private set; }
public int EventMetatable { get; private set; }
//function ref
public int PackBounds { get; private set; }
public int UnpackBounds { get; private set; }
public int PackRay { get; private set; }
public int UnpackRay { get; private set; }
public int PackRaycastHit { get; private set; }
public int PackTouch { get; private set; }
具体可以参考tolua中的类ObjectTranslator.cs,
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using UnityEngine;
namespace LuaInterface
{
public class ObjectTranslator
{
private class DelayGC
{
public DelayGC(int id, UnityEngine.Object obj, float time)
{
this.id = id;
this.time = time;
this.obj = obj;
}
public int id;
public UnityEngine.Object obj;
public float time;
}
private class CompareObject : IEqualityComparer
{
public new bool Equals(object x, object y)
{
return object.ReferenceEquals(x, y);
}
public int GetHashCode(object obj)
{
return RuntimeHelpers.GetHashCode(obj);
}
}
public bool LogGC { get; set; }
public readonly Dictionary objectsBackMap = new Dictionary (new CompareObject());
public readonly LuaObjectPool objects = new LuaObjectPool();
private List gcList = new List();