赞
踩
使用mvc之类的框架的时候,它的view,mediator,service,command,model基本都是成套创建的,这个时候有一个脚本创建的插件就会舒服很多,我这里写的是一个生成脚本的帮助类,可以帮你快速生成一个自定义的脚本内容
我这里以StrangeIOC为例,如果你想了解这个框架,可以看看我之前写的博文Unity框架探索——StrangeIOC篇,这个框架是MVCS架构,它需要生成的就是一套脚本
比如我们写Player功能,需要生成PlayerView,PlayerModel,PlayerService等等,就可以输入Player,自动生成这一系列脚本
[System.Serializable]
public class ScriptsPathData : ScriptableObject
{
[SerializeField]
public string ViewPath;
[SerializeField]
public string MediatorPath;
}
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 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 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(); }
详见自动创建脚本插件
插件界面
添加脚本名称,选择要生成的模块脚本,然后点击生成就好
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 搜索名称或扫描二维码
希望我们能共同成长,共同进步
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。