自动创建StrangeIOC框架配套脚本

使用mvc之类的框架的时候,它的view,mediator,service,command,model基本都是成套创建的,这个时候有一个脚本创建的插件就会舒服很多,我这里写的是一个生成脚本的帮助类,可以帮你快速生成一个自定义的脚本内容

我这里以StrangeIOC为例,如果你想了解这个框架,可以看看我之前写的博文Unity框架探索——StrangeIOC篇,这个框架是MVCS架构,它需要生成的就是一套脚本

比如我们写Player功能,需要生成PlayerView,PlayerModel,PlayerService等等,就可以输入Player,自动生成这一系列脚本

一、功能分析

  1. 创建编辑器窗口,用于用户操作
  2. 需要保存路径的数据类,以便保存用户设置
  3. 添加拖动文件夹添加路径操作
  4. 自动生成脚本内容部分

二、功能实现

(1)数据类实现

[System.Serializable]
public class ScriptsPathData : ScriptableObject
{
    [SerializeField]
    public string ViewPath;
    [SerializeField]
    public string MediatorPath;
}

(2)保存及获取路径功能实现

    private void SavePathToLocation()
    {
        Directory.CreateDirectory(m_DataDirectoryPath);
        ScriptsPathData data = new ScriptsPathData();
        data.ViewPath = m_ViewPath;
        data.MediatorPath = m_MediatorPath;
        AssetDatabase.CreateAsset(data, m_DataDirectoryPath+m_DataName);
    }

    private static void GetPathFromLocation()
    {
        if (File.Exists(m_DataDirectoryPath + m_DataName))
        {
            ScriptsPathData data = AssetDatabase.LoadAssetAtPath<ScriptsPathData>(m_DataDirectoryPath + m_DataName);
            m_ViewPath = data.ViewPath;
            m_MediatorPath = data.MediatorPath;
        }
    }

(3)拖动文件夹添加路径操作

    private void DragToPath(Rect rect, ref string path)
    {
        if ((Event.current.type == EventType.DragUpdated
             || Event.current.type == EventType.DragExited)
            && rect.Contains(Event.current.mousePosition))
        {
            DragAndDrop.visualMode = DragAndDropVisualMode.Generic;
            if (DragAndDrop.paths != null && DragAndDrop.paths.Length > 0)
            {
                path = DragAndDrop.paths[0];
            }
        }
    }

(4)自动生成脚本内容

    private void CreateAllScripts()
    {
        CreatScript(m_IsCreatView, m_Viewkey, m_ViewPath, GetViewCode());
        CreatScript(m_IsCreatMediator, m_Mediatorkey, m_MediatorPath, GetMediator());
    }

    private void CreatScript(bool isSelected,string key,string path,string context)
    {
        if (isSelected)
        {
            Debug.Log(m_ScriptName);
            string className = m_ScriptName + key;
            string filePath = path + "/" + className + ".cs";
            if (!File.Exists(filePath))
            {
                File.WriteAllText(filePath, context,Encoding.UTF8);
                Debug.Log("生成"+ className+"成功");
            }
        }
    }

    private string GetViewCode()
    {
        var script = new ScriptBuildHelp();
        script.WriteUsing("UnityEngine");
        script.WriteUsing(m_NameSpaceNamePrefix + m_Viewkey);
        script.WriteEmptyLine();
        script.WriteNamespace(m_NameSpaceNamePrefix + m_Viewkey);

        script.IndentTimes++;
        script.WriteClass(m_ScriptName + m_Viewkey, "ViewBase");

        script.IndentTimes++;
        script.WriteFun("Init");
        return script.ToString();
    }

    private string  GetMediator()
    {
        var script = new ScriptBuildHelp();
        script.WriteUsing(m_NameSpaceNamePrefix + m_Viewkey);
        script.WriteUsing("strange.extensions.mediation.impl");
        script.WriteEmptyLine();
        script.WriteNamespace(m_NameSpaceNamePrefix + m_Mediatorkey);

        script.IndentTimes++;
        script.WriteClass(m_ScriptName + m_Mediatorkey, "Mediator");

        script.IndentTimes++;

        script.WriteProperty("Inject", m_ScriptName + m_Viewkey, m_NameSpaceNamePrefix + m_Viewkey);
        script.WriteFun("Init");
        return script.ToString();
    }

(5)自动生成脚本辅助类

详见自动创建脚本插件

三、插件操作

插件界面
在这里插入图片描述
添加脚本名称,选择要生成的模块脚本,然后点击生成就好

四、完整脚本

public class CreatFrameScript : EditorWindow
{

    private static CreatFrameScript m_CreatFrameScript;
    private static string m_DataDirectoryPath = "Assets/Scripts/Config/";
    private static string m_DataName = "PathData.asset";

    private static string m_ScriptName;
    private static string m_NameSpaceNamePrefix = "Frame_";

    private static string m_ViewPath;
    private static string m_MediatorPath;

    private static bool m_IsCreatView;
    private static bool m_IsCreatMediator;

    private static string m_Viewkey = "View";
    private static string m_Mediatorkey = "Mediator";

    [MenuItem("CustomTool/CreatFrameScript")]
    public static void Window()
    {
        m_CreatFrameScript = (CreatFrameScript) GetWindow(typeof (CreatFrameScript));
        m_CreatFrameScript.minSize = new Vector2(500,400);
        m_CreatFrameScript.Show();
        GetPathFromLocation();
        Init();
    }

    private static void Init()
    {
        m_IsCreatView = false;
        m_IsCreatMediator = false;
        m_ScriptName = "";
    }

    private void OnGUI()
    {
        GUILayout.Label("脚本地址");
        CreatPathItem("View脚本地址", ref m_ViewPath);
        CreatPathItem("Mediator脚本地址", ref m_MediatorPath);

        if (GUILayout.Button("保存路径", GUILayout.MaxWidth(100)))
        {
            SavePathToLocation();
        }

        CreatItem("脚本名称(前缀)", ref m_ScriptName);
     
        ShowScriptName(ref m_IsCreatView, m_Viewkey);
        ShowScriptName(ref m_IsCreatMediator, m_Mediatorkey);



        if (GUILayout.Button("生成脚本", GUILayout.MaxWidth(100)))
        {
            CreateAllScripts();
            m_CreatFrameScript.Close();
        }
    }

    private void ShowScriptName(ref bool isSelected,string key)
    {
        GUILayout.BeginHorizontal();
        isSelected = GUILayout.Toggle(isSelected, "脚本名称:" + m_ScriptName + key);
        GUILayout.Label("命名空间:" + m_NameSpaceNamePrefix + key);
        GUILayout.EndHorizontal();
    }

    private void CreatPathItem(string name,ref string path)
    {
        Rect rect = CreatItem(name, ref path);
        DragToPath(rect,ref path);
    }

    private Rect CreatItem(string name, ref string context)
    {
        GUILayout.Label(name);
        Rect rect = EditorGUILayout.GetControlRect(GUILayout.Width(200));
        context = EditorGUI.TextField(rect, context);
        return rect;
    }

    private void DragToPath(Rect rect, ref string path)
    {
        if ((Event.current.type == EventType.DragUpdated
             || Event.current.type == EventType.DragExited)
            && rect.Contains(Event.current.mousePosition))
        {
            DragAndDrop.visualMode = DragAndDropVisualMode.Generic;
            if (DragAndDrop.paths != null && DragAndDrop.paths.Length > 0)
            {
                path = DragAndDrop.paths[0];
            }
        }
    }

    private void SavePathToLocation()
    {
        Directory.CreateDirectory(m_DataDirectoryPath);
        ScriptsPathData data = new ScriptsPathData();
        data.ViewPath = m_ViewPath;
        data.MediatorPath = m_MediatorPath;
        AssetDatabase.CreateAsset(data, m_DataDirectoryPath+m_DataName);
    }

    private static void GetPathFromLocation()
    {
        if (File.Exists(m_DataDirectoryPath + m_DataName))
        {
            ScriptsPathData data = AssetDatabase.LoadAssetAtPath<ScriptsPathData>(m_DataDirectoryPath + m_DataName);
            m_ViewPath = data.ViewPath;
            m_MediatorPath = data.MediatorPath;
        }
    }

    private void CreateAllScripts()
    {
        CreatScript(m_IsCreatView, m_Viewkey, m_ViewPath, GetViewCode());
        CreatScript(m_IsCreatMediator, m_Mediatorkey, m_MediatorPath, GetMediator());
    }

    private void CreatScript(bool isSelected,string key,string path,string context)
    {
        if (isSelected)
        {
            Debug.Log(m_ScriptName);
            string className = m_ScriptName + key;
            string filePath = path + "/" + className + ".cs";
            if (!File.Exists(filePath))
            {
                File.WriteAllText(filePath, context,Encoding.UTF8);
                Debug.Log("生成"+ className+"成功");
            }
        }
    }

    private string GetViewCode()
    {
        var script = new ScriptBuildHelp();
        script.WriteUsing("UnityEngine");
        script.WriteUsing(m_NameSpaceNamePrefix + m_Viewkey);
        script.WriteEmptyLine();
        script.WriteNamespace(m_NameSpaceNamePrefix + m_Viewkey);

        script.IndentTimes++;
        script.WriteClass(m_ScriptName + m_Viewkey, "ViewBase");

        script.IndentTimes++;
        script.WriteFun("Init");
        return script.ToString();
    }

    private string  GetMediator()
    {
        var script = new ScriptBuildHelp();
        script.WriteUsing(m_NameSpaceNamePrefix + m_Viewkey);
        script.WriteUsing("strange.extensions.mediation.impl");
        script.WriteEmptyLine();
        script.WriteNamespace(m_NameSpaceNamePrefix + m_Mediatorkey);

        script.IndentTimes++;
        script.WriteClass(m_ScriptName + m_Mediatorkey, "Mediator");

        script.IndentTimes++;

        script.WriteProperty("Inject", m_ScriptName + m_Viewkey, m_NameSpaceNamePrefix + m_Viewkey);
        script.WriteFun("Init");
        return script.ToString();
    }
}

类似于StrangeIOC框架这样,这样一个小工具,能帮助我们节省大量时间,这也是提升开发效率的有效途径

工具收录于我自己写的工具集,内部还有我写的几个小插件,我会慢慢更新,欢迎关注
工具集地址:https://github.com/BlueMonk1107/BlueToolkit

我会在我的公众号上推送新的博文,也可以帮大家解答问题
微信公众号 Andy and Unity 搜索名称或扫描二维码
在这里插入图片描述
希望我们能共同成长,共同进步

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值