当前位置:   article > 正文

自动创建StrangeIOC框架配套脚本_strangeioc插件

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;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

(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;
        }
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

(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];
            }
        }
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

(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();
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54

(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();
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171

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

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

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/89914
推荐阅读
相关标签
  

闽ICP备14008679号