使用mvc之類的框架的時候,它的view,mediator,service,command,model基本都是成套創建的,這個時候有一個腳本創建的插件就會舒服很多,我這裏寫的是一個生成腳本的幫助類,可以幫你快速生成一個自定義的腳本內容
我這裏以StrangeIOC爲例,如果你想了解這個框架,可以看看我之前寫的博文Unity框架探索——StrangeIOC篇,這個框架是MVCS架構,它需要生成的就是一套腳本
比如我們寫Player功能,需要生成PlayerView,PlayerModel,PlayerService等等,就可以輸入Player,自動生成這一系列腳本
一、功能分析
- 創建編輯器窗口,用於用戶操作
- 需要保存路徑的數據類,以便保存用戶設置
- 添加拖動文件夾添加路徑操作
- 自動生成腳本內容部分
二、功能實現
(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 搜索名稱或掃描二維碼
希望我們能共同成長,共同進步