public static GetBaseTypeStr ( Type t ) : string | ||
t | Type | |
return | string |
static void GetReturnTypeStr(Type t, ref string returns, ref string valueType) { returns = null; valueType = null; if (t == typeof(void)) { returns = "void"; return; } if (t.IsByRef) { t = t.GetElementType(); } //如果是基础类型,不提示 if (t.IsPrimitive) { returns = t.Name; return; } else if (t == typeof(string)) { returns = "System.String"; return; } else if (t == typeof(Vector3)) { returns = "Vector3"; return; } else if (t == typeof(Quaternion)) { returns = "Quaternion"; return; } else if (t == typeof(Vector2)) { returns = "Vector2"; return; } else if (t == typeof(Vector4)) { returns = "Vector4"; return; } else if (t == typeof(Color)) { returns = "Color"; return; } else if (t == typeof(Ray)) { returns = "Ray"; return; } else if (t == typeof(Bounds)) { returns = "Bounds"; return; } else if (t == typeof(LayerMask)) { returns = "LayerMask"; return; } else if (t.IsSubclassOf(typeof(UnityEngine.Events.UnityEvent))) { returns = valueType = "UnityEngine.Events.UnityEvent"; return; } //如果是集合类,那么转下 if (t.IsArray) { returns = valueType = "System.Array"; return; } if (t.GetInterface("System.Collections.IList") != null) { returns = valueType = "List"; return; } if (t.GetInterface("System.Collections.IDictionary") != null) { returns = valueType = "Dictionary"; return; } if (t.GetInterface("System.Collections.IEnumerable") != null) { returns = valueType = "System.Collections.IEnumerable"; return; } if (t.GetInterface("System.Collections.IEnumerator") != null) { returns = valueType = "System.Collections.IEnumerator"; return; } //如果不是集合类,但又是泛型,tolua是处理成全局库的,这里直接忽略 if (t.IsGenericType) { return; } if (!s_apiTypeIdx.ContainsKey(t)) { return; } //最后剩下的类型就只有c#导出到lua的类型了 returns = valueType = ToLuaExport.GetBaseTypeStr(t); }
static void GenClassApi(ToLuaMenu.BindType bt) { Type type = bt.type; if (type.IsGenericType)//泛型类被tolua处理成全局库了,如果都加成api自动提示的时候反而不方便 { return; } //计算lua中的全局名和继承者 string name = bt.name; string inherits = null; if (bt.baseType != null && !bt.baseType.IsGenericType) { inherits = ToLuaExport.GetBaseTypeStr(bt.baseType); } //创建api类 var api = GetClassApi(name, inherits); string returns = null, valueType = null; //注册成员函数,参考的是ToLuaExport.GenRegisterFuncItems for (int i = 0; i < ToLuaExport.methods.Count; i++) { MethodInfo m = ToLuaExport.methods[i].Method as MethodInfo; int count = 1; string methodName = ToLuaExport.GetMethodName(m); if (ToLuaExport.nameCounter.TryGetValue(methodName, out count)) { ToLuaExport.nameCounter[methodName] = count + 1; continue; } ToLuaExport.nameCounter[methodName] = 1; if (m.IsGenericMethod || methodName == "set_Item" || methodName == "get_Item" || methodName.StartsWith("op_")) { continue; } //获取返回值信息 GetReturnTypeStr(m.ReturnType, ref returns, ref valueType); //获取参数信息 ParameterInfo[] paramInfos = m.GetParameters(); s_paramName.Clear(); s_paramName.Append('('); for (int j = 0; j < paramInfos.Length; ++j) { s_paramName.Append(paramInfos[j].Name); if (j != paramInfos.Length - 1) { s_paramName.Append(','); } } s_paramName.Append(')'); string param = s_paramName.ToString(); if (m.IsStatic) { api.AddFunction(methodName, param, returns, valueType); } else { api.AddMethod(methodName, param, returns, valueType); } } //注册成员变量和属性 for (int i = 0; i < ToLuaExport.fields.Length; i++) { GetReturnTypeStr(ToLuaExport.fields[i].FieldType, ref returns, ref valueType); api.AddValue(ToLuaExport.fields[i].Name, valueType); } for (int i = 0; i < ToLuaExport.props.Length; i++) { GetReturnTypeStr(ToLuaExport.props[i].PropertyType, ref returns, ref valueType); api.AddValue(ToLuaExport.props[i].Name, valueType); } //注册操作符,暂时不需要 //注册构造函数,暂时不需要 //注册索引器,暂时不需要 }