Unity杂文——脚本创建辅助ScriptBuilder

  1. 简介
  2. 脚本和解析
  3. 用法

原文地址

简介

一个辅助快速生成需要的C#脚本的脚本。主要是快速生成引用,命名空间,类和方法一些。

脚本和解析

public class ScriptBuilder
{
    /// <summary>
    /// 脚本的字符串
    /// </summary>
    private StringBuilder m_Builder = new StringBuilder();
    /// <summary>
    /// 空白地字符串
    /// </summary>
    private StringBuilder m_Blank = new StringBuilder();

    /// <summary>
    /// 用于判断是否在方法内生成脚本
    /// </summary>
    public bool MarkInMethod { get; set; }

    /// <summary>
    /// 增加一行数据
    /// </summary>
    /// <param name="message"></param>
    public void AppendLine(string message)
    {
        m_Builder.Append(m_Blank);
        m_Builder.AppendLine(message);
    }

    /// <summary>
    /// 开始增加空白
    /// </summary>
    private void BeginBlank()
    {
        m_Blank.Append('\t');
    }

    /// <summary>
    /// 结束增加空白
    /// </summary>
    private void EndBlank()
    {
        m_Blank.Remove(m_Blank.Length - 1, 1);
    }

    /// <summary>
    ///开始增加命名空间
    /// </summary>
    /// <param name="name">命名空间的名字</param>
    public void BeginNamespace(string name)
    {
        BeginBrace($"namespace {name}");
    }

    /// <summary>
    /// 结束增加命名空间
    /// </summary>
    public void EndNamespace()
    {
        EndBrace();
    }

    /// <summary>
    /// 开始增加类
    /// </summary>
    /// <param name="modifier">类的修饰字符串</param>
    /// <param name="className">类的名字</param>
    /// <param name="superclass">继承类的名字</param>
    public void BeginClass(string modifier, string className, string superclass)
    {
        BeginBrace($"public {modifier} class {className} : {superclass}");
    }

    /// <summary>
    /// 开始增加类
    /// </summary>
    /// <param name="modifier">类的修饰字符串</param>
    /// <param name="className">类的名字</param>
    public void BeginClass(string modifier, string className)
    {
        BeginBrace($"public {modifier} class {className}");
    }

    /// <summary>
    /// 开始增加类
    /// </summary>
    /// <param name="className">类的名字</param>
    public void BeginClass(string className)
    {
        BeginBrace($"public class {className}");
    }

    /// <summary>
    /// 结束类
    /// </summary>
    public void EndClass()
    {
        EndBrace();
    }

    /// <summary>
    /// 开始增加方法
    /// </summary>
    /// <param name="method">方法名</param>
    /// <param name="permission">访问权限</param>
    /// <param name="returnType">返回类型</param>
    /// <param name="modifier">修饰符</param>
    public void BeginMethod(string method, string permission = "public", string returnType = "void",
        string modifier = "")
    {
        MarkInMethod = true;
        BeginBrace(string.IsNullOrEmpty(modifier)
            ? $"{permission} {returnType} {method}"
            : $"{permission} {modifier} {returnType} {method}");
    }

    /// <summary>
    /// 结束方法
    /// </summary>
    public void EndMethod()
    {
        EndBrace();
        MarkInMethod = false;
    }

    /// <summary>
    /// 开始属性
    /// </summary>
    /// <param name="property">属性名字</param>
    public void BeginProperty(string property)
    {
        BeginBrace(property);
    }

    /// <summary>
    /// 结束属性
    /// </summary>
    public void EndProperty()
    {
        EndBrace();
    }

    /// <summary>
    /// 大括号的开始
    /// </summary>
    /// <param name="code"></param>
    public void BeginBrace(string code)
    {
        AppendLine($"{code}");
        AppendLine("{");
        BeginBlank();
    }

    /// <summary>
    /// 结束大括号
    /// </summary>
    public void EndBrace()
    {
        EndBlank();
        AppendLine("}");
    }

    /// <summary>
    /// 开始字段
    /// </summary>
    /// <param name="code">Field的名字</param>
    public void BeginField(string code)
    {
        AppendLine($"{code}");
        AppendLine("{");
        BeginBlank();
    }

    /// <summary>
    /// 结束字段
    /// </summary>
    public void EndField()
    {
        EndBlank();
        AppendLine("};");
    }

    /// <summary>
    /// 开始Region
    /// </summary>
    /// <param name="name">Region的名字</param>
    public void BeginRegion(string name)
    {
        AppendLine($"#region {name}");
    }

    /// <summary>
    /// 结束Region
    /// </summary>
    public void EndRegion()
    {
        AppendLine("#endregion");
    }

    /// <summary>
    /// 转成字符串
    /// </summary>
    /// <returns></returns>
    public override string ToString()
    {
        return m_Builder.ToString();
    }
}

用法

上述方法就是辅助的脚本,下面是举例调用方法作为参考

// 获取类脚本的路径
var scriptPath = AssetDatabase.GUIDToAssetPath(kGUID);

var sb = new ScriptBuilder();
sb.BeginNamespace(typeof(StorageKeyConst).Namespace);
sb.BeginClass("static", "StorageKeyConst");
if (DataStorageEditorTools.Instance.DataStorageEditorDataRootList.Count > 0)
{
    sb.BeginRegion("RootKeys");
    sb.AppendLine(string.Empty);
    AddListScripts(sb, DataStorageEditorTools.Instance.DataStorageEditorDataRootList);
    sb.AppendLine(string.Empty);
    sb.EndRegion();
    sb.AppendLine(string.Empty);
}
if (DataStorageEditorTools.Instance.DataStorageEditorDataList.Count > 0)
{
    sb.BeginRegion("Keys");
    sb.AppendLine(string.Empty);
    AddListScripts(sb, DataStorageEditorTools.Instance.DataStorageEditorDataList);
    sb.AppendLine(string.Empty);
    sb.EndRegion();
}

sb.EndClass();
sb.EndNamespace();
File.WriteAllText(scriptPath, sb.ToString(), Encoding.UTF8);
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();

转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 841774407@qq.com

×

喜欢就点赞,疼爱就打赏