• 脚本编辑器的写法


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

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

     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 }

    这样多简单

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

  • 相关阅读:
    Windows下将ImageMagick移植到Android平台
    【转】对于JNI方法名,数据类型和方法签名的一些认识
    Android中图片占用内存的计算
    Android中Canvas绘图基础详解(附源码下载) (转)
    Android中如何查看内存(下)
    Android中如何查看内存(上)
    WMRouter:美团外卖Android开源路由框架
    写给工程师的十条精进原则
    Flutter的原理及美团的实践
    美团扫码付的前端可用性保障实践
  • 原文地址:https://www.cnblogs.com/qinshuaijun/p/8253188.html
Copyright © 2020-2023  润新知