Unity创建单例脚本工具

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

public class ScriptCreatorWindow : EditorWindow
{
    [MenuItem("Assets/Create/C# Single Script", false, 1)]
    public static void ShowWindow()
    {
        var window = EditorWindow.GetWindow(typeof(ScriptCreatorWindow));
        var screenResolution = Screen.currentResolution;
        //left,top to center,center
        var w = 300;
        var h = 150;
        var r = new Rect(screenResolution.width * 0.5f - w * 0.5f, screenResolution.height * 0.5f - h * 0.5f, w, h);
        window.position = r;
    }

    private string scriptName;

    void OnGUI()
    {
        GUILayout.Label("Base Settings", EditorStyles.boldLabel);

        scriptName = EditorGUILayout.TextField("Name", scriptName);
        if (string.IsNullOrEmpty(scriptName))
        {
            scriptName = "SingleManager";
        }

        EditorGUILayout.Space();
        if (GUILayout.Button("Create Single"))
        {
            CreateSingle();
            Close();
        }

        EditorGUILayout.Space();
        if (GUILayout.Button("Create Single Mono"))
        {
            CreateSingleMonoBase();
            Close();
        }
    }

    void CreateSingle()
    {
        string codeSrc = "using System.Collections;\n";
        codeSrc += "using System.Collections.Generic;\n";
        codeSrc += "using UnityEngine;\n";
        codeSrc += "\n";

        codeSrc += "public class " + scriptName + " : SingleBase<" + scriptName + ">\n";
        codeSrc += "{\n";

        codeSrc += "    public void Init()\n";
        codeSrc += "    {\n";
        codeSrc += "    }\n";
        codeSrc += "\n";

        codeSrc += "    public override void Dispose()\n";
        codeSrc += "    {\n";
        codeSrc += "    }\n";
        codeSrc += "\n";

        codeSrc += "}\n";

        var assetPath = string.Format("{0}/{1}{2}", GetSelectedPathOrFallback(), scriptName, ".cs");
        var filePath = Application.dataPath.Replace("Assets", assetPath);

        File.WriteAllText(filePath, codeSrc);

        AssetDatabase.ImportAsset(assetPath);
    }

    void CreateSingleMonoBase()
    {
        string codeSrc = "using System.Collections;\n";
        codeSrc += "using System.Collections.Generic;\n";
        codeSrc += "using UnityEngine;\n";
        codeSrc += "\n";

        codeSrc += "public class " + scriptName + " : SingleMonoBase<" + scriptName + ">\n";
        codeSrc += "{\n";

        codeSrc += "    protected override void Awake()\n";
        codeSrc += "    {\n";
        codeSrc += "    }\n";
        codeSrc += "\n";

        codeSrc += "    protected override void Update()\n";
        codeSrc += "    {\n";
        codeSrc += "    }\n";
        codeSrc += "\n";

        codeSrc += "    protected override void OnDestroy()\n";
        codeSrc += "    {\n";
        codeSrc += "    }\n";
        codeSrc += "\n";

        codeSrc += "}\n";

        var assetPath = string.Format("{0}/{1}{2}", GetSelectedPathOrFallback(), scriptName, ".cs");
        var filePath = Application.dataPath.Replace("Assets", assetPath);

        File.WriteAllText(filePath, codeSrc);

        AssetDatabase.ImportAsset(assetPath);
    }

    public static string GetSelectedPathOrFallback()
    {
        string path = "Assets";

        foreach (UnityEngine.Object obj in Selection.GetFiltered(typeof(UnityEngine.Object), SelectionMode.Assets))
        {
            path = AssetDatabase.GetAssetPath(obj);
            if (!string.IsNullOrEmpty(path) && File.Exists(path))
            {
                path = Path.GetDirectoryName(path);
                break;
            }
        }

        return path;
    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值