哈哈,又学到一项新技能,以后不用写重复脚本啦,以往读取数据库文件一个一个往脚本中添加是件很麻烦的事情,这个新技能能省很多事情,
所谓的新技能就是通过一个脚本把重复的东西逻辑化,创造一个模板,就相当于打印机一样,废话不多说,直接上代码
1 using System.Collections; 2 using System.Collections.Generic; 3 using UnityEngine; 4 using System.IO; 5 using UnityEditor; 6 using System.Collections.Generic; 7 using System.Text; 8 using Debug = UnityEngine.Debug; 9 using System; 10 11 public class LoadTable { 12 13 private static string ExportDir; 14 List<string> table = new List<string>(); 15 16 [MenuItem("Tools/Help/Load")] 17 static void Init() 18 { 19 LoadTable load = new LoadTable (); 20 load.Export (); 21 } 22 23 public void Export() 24 { 25 string texts = ""; 26 27 28 29 for(int i = 0; i < Resources.LoadAll ("Plugin/Scripts").Length; i++) 30 { 31 table.Add (Resources.LoadAll ("Plugin/Scripts")[i].name); 32 } 33 ProcessTable (); 34 } 35 36 37 void ProcessTable () 38 { 39 40 string strFileText = ""; 41 string strFullfilePath = Application.dataPath +"/LoadTheTable.cs"; 42 43 strFileText += "\nusing UnityEngine;\n\n"; 44 strFileText += "public class LoadTheTable{\n"; 45 46 foreach(var strTableName in table) 47 { 48 strFileText += " public " + strTableName + " " + strTableName + " = null;\n"; 49 strFileText += " public TextAsset " + strTableName + "s = null;\n\n"; 50 } 51 52 strFileText += " private static LoadTheTable loadTheTable;\n"; 53 strFileText += " public static LoadTheTable Instance(){\n"; 54 strFileText += " if(loadTheTable == null)"; 55 strFileText += " {\n"; 56 strFileText += " loadTheTable = new LoadTheTable ();\n"; 57 strFileText += " }\n"; 58 strFileText += " return loadTheTable;\n"; 59 strFileText += " }\n\n"; 60 61 foreach(var strTableName in table) 62 { 63 strFileText += " public " + strTableName + " Get" + strTableName + "(){\n"; 64 strFileText += " if(" + strTableName + " == null){\n"; 65 strFileText += " " + strTableName + " = new " + strTableName + "();\n"; 66 strFileText += " }\n"; 67 68 strFileText += " if(" + strTableName + "s == null){\n"; 69 strFileText += " string tableName = \"Plugin/Data/" + "\"" + ";\n"; 70 strFileText += " tableName += " + strTableName + ".csvName;\n"; 71 strFileText += " " + strTableName + "s = Resources.Load<TextAsset> (tableName);\n"; 72 strFileText += " " + strTableName + ".Load(" + strTableName + "s);\n"; 73 strFileText += " }\n"; 74 strFileText += " return " + strTableName + ";\n"; 75 strFileText += " }\n\n"; 76 } 77 strFileText += "}\n"; 78 79 using (StreamWriter s = new StreamWriter (strFullfilePath, false, Encoding.UTF8)) 80 s.WriteLine (strFileText); 81 } 82 }
这样多简单
和写方法一样,仅仅只是对字符串进行处理,把需要处理的提取出来就行,还有一种更加简单的方法,等抽空在提炼一下。