Auto Script Maker​​​​​​​ in unity

AutoScriptMaker

using System.Collections.Generic; 
using UnityEngine; 
using System.IO; 
using UnityEditor;

public class AutoScriptMaker : MonoBehaviour
{
    public string CodeName; internal string FinalCode; public List Usings; public string NameSpace; public List Regions;

    public void ScrpitMaker()
    { // Clear the code FinalCode = "";

        // Add all usings to the first of script
        for (int i = 0; i < Usings.Count; i++)
        {
            FinalCode += "using " + Usings[i] + ";" + "\n";
        }

        // Add NameSpace name
        if (NameSpace != "")
        {
            FinalCode += "\n" + "namespace " + NameSpace + "\n";
            FinalCode += "{" + "\n";
        }

        // Add the class name
        FinalCode += "    public class " + CodeName + " : MonoBehaviour" + "\n";
        FinalCode += "{" + "\n";

        // Add all regions to the script
        for (int i = 0; i < Regions.Count; i++)
        {
            FinalCode += "\n" + "#region " + Regions[i] + "\n";
            FinalCode += "\n" + "#endregion" + "\n";
        }

        // Close the public class
        FinalCode += "\n" + "}" + "\n";

        // Close the code if the code has namespace
        if (NameSpace != "")
        {
            FinalCode += "\n" + "}" + "\n";
        }

        // Save as C# script
        string textsaver = Application.dataPath + "/" + CodeName + ".cs";
        if (File.Exists(textsaver))
        {
            File.Delete(textsaver);
        }
        File.AppendAllText(textsaver, FinalCode + "\n");
    }
}

[CustomEditor(typeof(AutoScriptMaker))]
public class MyScriptEditor : Editor
{
    public override void OnInspectorGUI()
    {
        DrawDefaultInspector();

        AutoScriptMaker myScript = (AutoScriptMaker)target;

        if (GUILayout.Button("ScrpitMaker"))
        {
            myScript.ScrpitMaker();
        }
    }
}


Content ©2023 Abolfazl Tanha All Rights Reserved​​​​​​​