脚本编辑器的写法

     哈哈,又学到一项新技能,以后不用写重复脚本啦,以往读取数据库文件一个一个往脚本中添加是件很麻烦的事情,这个新技能能省很多事情,

    所谓的新技能就是通过一个脚本把重复的东西逻辑化,创造一个模板,就相当于打印机一样,废话不多说,直接上代码

 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 += "
using UnityEngine;

";
44         strFileText += "public class LoadTheTable{
";
45 
46         foreach(var strTableName in table)
47         {
48             strFileText += "    public " + strTableName + " " + strTableName + " = null;
";
49             strFileText += "    public TextAsset " + strTableName + "s = null;

";            
50         }
51 
52         strFileText += "    private static LoadTheTable loadTheTable;
";
53         strFileText += "    public static LoadTheTable Instance(){
";
54         strFileText += "    if(loadTheTable == null)";
55         strFileText += "        {
";
56         strFileText += "             loadTheTable = new LoadTheTable ();
";
57         strFileText += "        }
";
58         strFileText += "         return loadTheTable;
";
59         strFileText += "     }

";
60 
61         foreach(var strTableName in table)
62         {
63             strFileText += "     public " + strTableName + " Get" + strTableName + "(){
";
64             strFileText += "         if(" + strTableName + " == null){
";
65             strFileText += "             " + strTableName + " = new " + strTableName + "();
";
66             strFileText += "         }
";
67             
68             strFileText += "         if(" + strTableName + "s == null){
";
69             strFileText += "         string tableName = "Plugin/Data/" + """ + ";
";
70             strFileText += "         tableName += " + strTableName + ".csvName;
";
71             strFileText += "              " + strTableName + "s = Resources.Load<TextAsset> (tableName);
";
72             strFileText += "              " + strTableName + ".Load(" + strTableName + "s);
";
73             strFileText += "          }
";
74             strFileText += "          return " + strTableName  + ";
";
75             strFileText += "      }

";        
76         }
77         strFileText += "}
";            
78 
79         using (StreamWriter s = new StreamWriter (strFullfilePath, false, Encoding.UTF8))
80             s.WriteLine (strFileText);
81     }
82 }

这样多简单

和写方法一样,仅仅只是对字符串进行处理,把需要处理的提取出来就行,还有一种更加简单的方法,等抽空在提炼一下。

原文地址:https://www.cnblogs.com/qinshuaijun/p/8253188.html