private static void GenerateMono2JsComConfig() { var mono2JsCom = new Dictionary <string, string>(); Assembly logicCodeLib = Assembly.Load("Assembly-CSharp"); if (logicCodeLib != null) { var types = logicCodeLib.GetExportedTypes(); foreach (var t in types) { if (t.IsSubclassOf(typeof(MonoBehaviour))) { if (WillTypeBeTranslatedToJavaScript(t)) { string jsComponentName = JSComponentGenerator.GetJSComponentClassName(t); mono2JsCom.Add(JSNameMgr.GetTypeFullName(t, false), jsComponentName); } } } } else { Debug.LogError("Load Assembly-CSharp.dll failed"); } string filePath = JSPathSettings.Mono2JsComConfig; File.WriteAllText(filePath, JsonMapper.ToJson(mono2JsCom)); Debug.Log(string.Format("Mono2JsCom:{0}\nOK. File: {1}", mono2JsCom.Count, filePath)); }
/// <summary> /// Replace MonoBehaviour with JSComponent, only when this MonoBehaviour has JsType attribute /// Will copy serialized data if needed /// </summary> /// <typeparam name="T"></typeparam> /// <param name="go">GameObject to replace MonoBehaviours.</param> /// <returns>return true if any MonoBehaviour of go has been replaced.</returns> public static bool CopyGameObject(GameObject go) { bool bReplaced = false; var behaviours = go.GetComponents <MonoBehaviour>(); for (var i = 0; i < behaviours.Length; i++) { var behav = behaviours[i]; // ignore JSSerializer here if (behav is JSSerializer) { continue; } if (behav == null) { // Debug.Log("00000000000"); Debug.LogWarning("There is null behaviour in gameObject \"" + go.name + "\""); continue; } if (WillTypeBeTranslatedToJavaScript(behav.GetType())) { // if this MonoBehaviour is going to be translated to JavaScript // replace this behaviour with JSComponent // copy the serialized data if needed //JSSerializer helper = (JSSerializer)go.AddComponent<T>(); JSSerializer helper = JSComponentGenerator.CreateJSComponentInstance(go, behav); if (helper == null) { continue; } CopyBehaviour(behav, helper); bReplaced = true; } } return(bReplaced); }
public static void OutputAllTypesWithJsTypeAttribute() { //var sb = new StringBuilder(); var sb2 = new StringBuilder(); // sb.Append(@"/* Generated by JSBinding Menu : JSB | Generate SharpKit JsType file CS.require list //* see JSAnalyzer.cs / OutputAllTypesWithJsTypeAttribute() function //* better not modify manually. //*/ // //"); sb2.Append(@"/* Generated by JSBinding Menu : JSB | Generate MonoBehaviour to JSComponent_XX * see JSAnalyzer.cs / OutputAllTypesWithJsTypeAttribute() function * better not modify manually. */ "); sb2.AppendLine("var MonoBehaviour2JSComponentName =").AppendLine("["); foreach (Assembly a in AppDomain.CurrentDomain.GetAssemblies()) { Type[] types = a.GetTypes(); foreach (Type t in types) { if (JSSerializerEditor.WillTypeBeTranslatedToJavaScript(t)) { System.Object[] attrs = t.GetCustomAttributes(typeof(JsTypeAttribute), false); JsTypeAttribute jsTypeAttr = (JsTypeAttribute)attrs[0]; if (jsTypeAttr.Filename != null) { //Debug.Log(jsTypeAttr.filename); string mustBegin = "StreamingAssets/JavaScript/"; //string mustBegin = JSBindingSettings.sharpKitGenFileDir; int index = 0; if ((index = jsTypeAttr.Filename.IndexOf(mustBegin)) >= 0) { //sb.AppendFormat("CS.require(\"{0}\");\n", jsTypeAttr.Filename.Substring(index + mustBegin.Length)); } else { Debug.LogError(JSNameMgr.GetTypeFullName(t) + " is ignored because JsType.filename doesn't contain \"" + mustBegin + "\""); } } ///// if (t.IsSubclassOf(typeof(MonoBehaviour))) { string jsComponentName = JSComponentGenerator.GetJSComponentClassName(t); sb2.AppendFormat(" \"{0}|{1}\",\n", JSNameMgr.GetTypeFullName(t, false), jsComponentName); } } } } sb2.AppendLine("];"); sb2.Append(@" var GetMonoBehaviourJSComponentName = function (i) { if (i < MonoBehaviour2JSComponentName.length) { return MonoBehaviour2JSComponentName[i]; } return """"; // returning empty string when end } "); //Debug.Log(sb); string path = JSBindingSettings.sharpkitGeneratedFiles; // 现在不需要这个 // File.WriteAllText(path, sb.ToString()); // Debug.Log("OK. File: " + path); // AssetDatabase.Refresh(); path = JSBindingSettings.monoBehaviour2JSComponentName; File.WriteAllText(path, sb2.ToString()); Debug.Log("OK. File: " + path); }