C#_生成脚本模板

发布于 2020-05-11  28 次阅读


前言

有时候会用一些脚本需要自动生成,比如qf,gf里都有这样的功能。
在这里做个案例。

正文

今天讲一下自动生成脚本。

实现效果:

生成的脚本:

using UnityEngine;
public class TwoStepControl:MonoBehaviour
{
    private MainControl mainC;
    private TwoStepControlUI TwoUIC;
    public void Init(MainControl c)
    {
        mainC = c;
        TwoUIC = GetComponent<TwoStepControlUI>();
        TwoUIC.Init(this); 
    } 
}

using UnityEngine;
public class TwoStepControlUI:MonoBehaviour
{
    private TwoStepControl TwoC;
    public void Init(TwoStepControl c)
    { 
        TwoC = c;
    } 
}

实现逻辑:

  • EditorWindow,不了解的先点传送门
  • Directory

上脚本Open.cs

/**
 *Copyright(C) 2018 by DefaultCompany
 *All rights reserved.
 *FileName:     StepLogicEditor.cs
 *Author:       Pan
 *Version:      1.0
 *UnityVersion��2018.4.13f1
 *Date:         2020-05-12
 *Description:   
 *History:
*/ 
using System.IO;
using System.Text;
using UnityEditor;
using UnityEngine;

public class StepLogicEditor : EditorWindow
{  
    [MenuItem("Pan/Builder Tool", false, 41)]
    private static void CreateScripts()
    {
        Rect area = new Rect(0, 0, 300, 120);
        StepLogicEditor window = GetWindowWithRect<StepLogicEditor>(area, true, "步骤脚本生成器");
        window.Show();
    } 

    static string savePath = string.Empty;
    static string saveFileName = string.Empty;
    // 0 Name 1 ControlUI 
    static string commonContent =
"using UnityEngine;\n" +
"public class {0}{1}:MonoBehaviour\r\n" +
"{2}\r\n" +
"     private MainControl mainC;\n" +
"     private {0}{1}UI {0}UIC;\n" +
"     public void Init(MainControl c)\n" +
"     {2}\r\n" +
"         mainC = c;\n" +
"         {0}UIC = GetComponent<{0}{1}UI>();\n" +
"         {0}UIC.Init(this);\n" +
"     {3}\r\n\r\n"+
"{3}"; 
    static string commonContentUI =
"using UnityEngine;\n" +
"public class {0}{1}UI:MonoBehaviour\r\n" +
"{2}\r\n" +
"     private {0}{1} {0}C;\n"+
"     public void Init({0}{1} c)\r\n" +
"     {2}\r\n\r\n" +
"         {0}C = c;\n" +
"     {3}\r\n\r\n" +
"{3}";

    private void OnGUI()
    {
        GUIStyle buttonStyle = new GUIStyle();
        buttonStyle.alignment = TextAnchor.MiddleCenter;
        buttonStyle.fontSize = 15;
        buttonStyle.fixedWidth = 15;
        buttonStyle.normal.textColor = Color.red;
        buttonStyle.normal.background = null;

        EditorGUILayout.Space();
        EditorGUILayout.Space();
        EditorGUILayout.BeginHorizontal();
        EditorGUILayout.LabelField("脚本名:", GUILayout.Width(80));
        saveFileName = EditorGUILayout.TextField(string.Empty, saveFileName, GUILayout.Width(100));
        EditorGUILayout.EndHorizontal();

        EditorGUILayout.Space();
        EditorGUILayout.BeginHorizontal();
        EditorGUILayout.LabelField("脚本生成路径:", GUILayout.Width(80));
        savePath = EditorGUILayout.TextField(string.Empty, savePath, GUILayout.Width(190));
        if (GUILayout.Button("⊙", buttonStyle))
        {
            savePath = OpenFolder();
        }
        EditorGUILayout.EndHorizontal();

        EditorGUILayout.BeginHorizontal();
        if (GUILayout.Button("生成"))
        {
            if (!Directory.Exists(savePath))
            {
                Debug.LogError("路径不存在!!!");
                return;
            }

            File.WriteAllText(savePath + saveFileName + "Control.cs",
             string.Format(commonContent, saveFileName, "Control", '{', '}'), Encoding.UTF8);

            File.WriteAllText(savePath + saveFileName + "ControlUI.cs",
            string.Format(commonContentUI, saveFileName, "Control", '{', '}'), Encoding.UTF8);

            AssetDatabase.SaveAssets();
            AssetDatabase.Refresh();
            Debug.Log("/////////文件创建成功//////");

        }
        EditorGUILayout.EndHorizontal();
    }

    private string OpenFolder()
    {
        string tmpPath = string.Empty;
        tmpPath = EditorUtility.OpenFolderPanel("Resource Folder", "Assets", string.Empty);
        if (!string.IsNullOrEmpty(tmpPath))
        {
            var gamePath = System.IO.Path.GetFullPath(".");//TODO - FileUtil.GetProjectRelativePath??
            gamePath = gamePath.Replace("\\", "/");
            if (tmpPath.Length > gamePath.Length && tmpPath.StartsWith(gamePath))
                tmpPath = tmpPath.Remove(0, gamePath.Length + 1);
        }
        return tmpPath + '/';
    }  
}

不积跬步,无以至千里;不积小流,无以成江海。