private static void OnWillCreateAsset(string path) { if (!AddNamespaceWindow.GetData().isOn) { return; } path = path.Replace(".meta", ""); if (path.Contains("Sources")) { return; } if (path.EndsWith(".cs")) { string text = ""; //读取xml和json建议用File.ReadAllText,因为它会自动的关闭IO流 text += File.ReadAllText(path); string name = GetClassName(text); if (string.IsNullOrEmpty(name)) { return; } var newText = GetNewScriptContext(name); File.WriteAllText(path, newText, System.Text.Encoding.UTF8); } AssetDatabase.Refresh(); }
//更新脚本内容为新的类名 private static string GetScriptContext(string className) { var script = new ScriptBuildHelper(); script.WriteUsing("UnityEngine"); script.WriteLine(""); var data = AddNamespaceWindow.GetData(); var name = data == null ? "UIFrame" : data.name; script.WriteNamespace(name); script.WriteClass(className, 1); script.WriteFunction("Start", 2); //script.WriteFunction("Update",2, "float delta", "bool isUpdate"); return(script.toString()); }
private static string GetNewScriptContext(string className) { var script = new ScriptBuildHelp(); script.WriteUsing("UnityEngine"); script.WriteEmptyLine(); AddNamespaceData data = AddNamespaceWindow.GetData(); string name = data == null ? "UIFrame" : data.name; script.WriteNameSpace(name); script.IndentTimes++; script.WriteClass(className, "MonoBehaviour"); script.IndentTimes++; List <string> keyName = new List <string>(); keyName.Add("void"); script.WriteFun("Start", ScriptBuildHelp.Private, keyName); return(script.ToString()); }