public TextFile Get_GetParam(Type t) { elementType = t.GetElementType(); if (elementType.IsArray) { //...error } bool needCast; string getVal = JSDataExchangeMgr.GetMetatypeKeyword(elementType, out needCast); var arrayFullName = string.Empty; var elementFullName = string.Empty; if (elementType.IsGenericParameter) { arrayFullName = "object[]"; elementFullName = "object"; } else { arrayFullName = JSNameMgr.CsFullName(t); elementFullName = JSNameMgr.CsFullName(elementType); } TextFile tf = new TextFile(); tf.AddMultiline(@"JSDataExchangeMgr.GetJSArg<{0}>(() => {{ int jsObjID = JSApi.getObject((int)JSApi.GetType.Arg); int length = jsObjID == 0 ? 0 : JSApi.getArrayLength(jsObjID); var ret = new {1}[length]; for (var i = 0; i < length; i++) {{ JSApi.getElement(jsObjID, i); ret[i] = ({1}){2}((int)JSApi.GetType.SaveAndRemove); }} return ret; }})", arrayFullName, elementFullName, getVal); return(tf); }
static void GenMMgr(string className) { TextFile tf = new TextFile(null, "// auto gen"); tf.Add("using UnityEngine;"); tf.Add("using UnityEngine.UI;"); tf.Add("using System;"); tf.Add("using System.Collections;"); tf.Add("using System.Collections.Generic;"); tf.Add("using jsb;"); tf.AddLine(); TextFile tfNs = tf.Add("namespace jsb").BraceIn(); { TextFile tfC = tfNs.Add("public class {0}", className).BraceIn(); { tfC.Add("static Dictionary<string, int[]> jID = new Dictionary<string, int[]>();"); { tfC.AddMultiline(@" static int[] GetJsClassMessages(string jsFullName) { if (jID.ContainsKey(jsFullName)) return jID[jsFullName]; if (!JSMgr.vCall.CallJSFunctionName(JSCache.GetBridgeJsID(), ""getLMsgs"", jsFullName)) throw new Exception(""call Bridge.getLMsgs failed!""); string str = JSApi.getStringS((int)JSApi.GetType.JSFunRet); if (string.IsNullOrEmpty(str)) { jID[jsFullName] = null; return null; } string[] arr = str.Split(','); int[] r = new int[arr.Length]; for (int i = 0; i < arr.Length; i++) r[i] = int.Parse(arr[i]); jID[jsFullName] = r; return r; }"); } tfC.AddLine(); { tfC.AddMultiline(@" public static void CreateMessages(string jsFullName, GameObject go) { int[] ids = GetJsClassMessages(jsFullName); if (ids == null) return; // ID号 JS和CS保持一致才不会错 for (int i = 0; i < ids.Length; i++) { Type type = MessageTypes[ids[i]]; if (go.GetComponent(type) == null) go.AddComponent(type); } }"); } tfC.AddLine(); { TextFile tfM = tfC.Add("static Type[] MessageTypes = new Type[]").BraceIn(); { for (int i = 0; i < infos.Length; i++) { Info info = infos[i]; tfM.Add("typeof(jsb.{0}),", info.className); } } tfM.BraceOutSC(); } } tfC.BraceOut(); } tfNs.BraceOut(); string s = tf.Format(-1); File.WriteAllText(CsDir + "/" + className + ".cs", s); }