0%

Project Linker

功能

在Unit内创建多个项目实例。

源码

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
namespace ProjectLinker
{
static class CmdHelper
{
public static void LinkFolder(string orgPath, string linkPath)
{
ProcessStartInfo start = new ProcessStartInfo("cmd.exe");
start.CreateNoWindow = true;
start.UseShellExecute = false;
start.RedirectStandardInput = true;
var process = Process.Start(start);
var sw = process.StandardInput;
sw.WriteLine(@$"mklink /D ""{linkPath}"" ""{orgPath}""");
sw.Close();
process.WaitForExit();
process.Close();
}
public static void LaunchUnityProject(string projectFullPath, string buildTarget)
{
string editorPath = EditorApplication.applicationPath;
System.Treading.ThreadPool.QueueUserWorkItem(delegate (object state)
{
Process p = null;
try
{
string arg = $"-projectPath \"{projectFullPath}\"";
if(!string.IsNullOrEmpty(buildTarget))
{
arg += $" -buildTarget {buildTarget}";
}
ProcessStartInfo start = new ProcessStartInfo(editorPath, arg);
start.CreateNoWindow = false;
start.UseShellExecute = false;

p = Process.Start(start);
}
catch(System.Exception e)
{
UnityEngine.Debug.LogException(e);
if(p != null)
{
p.Close();
}
}
})
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
using System;
using System.Collections.Generic;
namespace ProjectLinker
{
[Serializable]
class CacheDataTable
{
public List<CacheDataElem> elems = new List<CacheDataElem>();
public CacheDataTable()
{
List<CacheDataElem> elems = new List<CacheDataElem>();
}
}
[Serializable]
class CacheDataElem
{
public string name;
public string buildTarget;
public string projectPath;
}
}
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
using System;
using System.Collections.Generic;
using UnityEditor;
using UnityEditor.IMGUI.Controls;
using UnityEngine;
namespace ProjectLinker
{
class ProjectTreeView : TreeView
{
private CacheDataTable _tableData;
public event Action<int> OnSelected;
public event Action OnDataChange;
public ProjectTreeView(CacheDataTable table, TreeViewState state) : base(state)
{
_tableData = table;
showAlternatingRowBackgrounds = false;
ReLoad();
}
protected override TreeViewItem BuildRoot()
{
return new TreeViewItem {id = 0, depth = -1};
}
protected override IList<TreeViewItem> BuildRows(TreeviewItem root)
{
var rows = GetRows() ?? new List<TreeViewItem>(10);
rows.Clear();
var iconTex = EditorGUIUtility.FindTexture("Folder Icon");
for(int i = 0; i < _tableData.elems.Count; ++i)
{
var item = new TreeViewItem {id = i + 1, depth = 0, displyName = _tableData.elems[i].name};
item.icon = iconTex;
root.AddChild(item);
rows.Add(item);
}
SetupDepthsFromParentsAndChildren(root);
return rows;
}
protected override void RowGUI(RowGUIArgs args)
{
base.RowGUI(args);
float width = 16f;
rect deleRect = new Rect(args.rowRect.width - width - 10, args.rowRect.y, width, width);
Event evt = Event.current;
if(evt.type == EventType.MouseDown && deleRect.Contains(evt.mousePosition))
SelectionClick(args.item, false);
if(GUI.Button(deleRect, "x"))
{
_tableData.elems.RemoveAt(args.item.id - 1);
Reload();
OnDataChange?.Invoke();
}
}
protected override void SingleClickedItem(int id)
{
base.SingleClickedItem(id);
int index = id - 1;
OnSelected?.Invoke(index);
}
}
}
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.IO;
using UnityEditor.IMGUI.Controls;
namespace ProjectLinker
{
public class ProjectLinker : EditorWindow
{
[MenuItem("Window/Project Linker")]
private static void Open()
{
var win = EditorWindow.GetWindow<ProjectLinker>("关联项目快捷启动");
win.Show();
}

private const string _CACHE_TABLE_HEADER = "ProjectLinker_Table_";
private string _copyProjectName = "";
private string _defaultCopyProjectName = "";
private string _curProjectPath = "";
private string _rootPath = "";
[SerializeField] TreeViewState _treeViewState;
ProjectTreeView _treeView;

CacheDataTable _cacheDataTable = null;
int _selectedElemIndex = -1;

CacheDataElem selectedElem
{
get
{
if(_selectedElemIndex > -1 && _selectedElemIndex < _cacheDataTable.elems.Count)
{
return _cacheDataTable.elems[_selectedElemIndex];
}
return null;
}
}
private void OnEnable()
{
_curProjectPath = Path.GetDirectoryName(Application.dataPath);
_rootPath = Path.GetDirectoryName(_curProjectPath);
_defaultCopyProjectName = Path.GetFileName(_curProjectPath) + "_copy";
_copyProjectName = _defaultCopyProjectName;
LoadCache();

if(_treeViewState == null)
_treeViewState = new TreeViewState();
_treeView = new ProjectTreeView(_cacheDataTable, _treeViewState);
_treeView.OnSelected -= OnSelected;
_treeView.OnSelected += OnSelected;
_treeView.OnDataChange -= SaveCache;
_treeView.OnDataChange += SaveCache;
}
private void OnSelected(int index)
{
_selectedElemIndex = index;
}
private void DrawTree()
{
Rect rect = GUILayoutUtility.GetRect(1000, 10000, 0, 10000);
_treeView.OnGUI(rect);
}
private void DrawElemInfo()
{
EditorGUILayout.BeginVertical(EditorStyles.helpBox);
EditorGUILayout.BeginHorizontal();
GUILayout.Label("选中项目详情", EditorStyles.label);
EditorGUI.BeginDisabledGroup(true);
EditorGUILayout.TextField(selectedElem.buildTarget, GUILayout.Width(100));
EditorGUI.EndDisabledGroup();
EditorGUILayout.EndHorizontal();

EditorGUILayout.BeginHorizontal(GUILayout.ExpandWidth(true));
EditorGUILayout.BeginVertical();
EditorGUI.BeginChangeCheck();
selectedElem.name = EditorGUILayout.TextField("项目名称", selectedElem.name);
EditorGUI.BeginDisabledGroup(true);
EditorGUILayout.TextField("项目路径", selectedElem.projectPath);
EditorGUI.EndDisabledGroup();
if(EditorGUI.EndChangeCheck())
{
SaveCache();
_treeView.Reload();
}
EditorGUILayout.EndVertical();

EditorGUILayout.BeginVertical(GUILayout.Width(50));
if(GUILayout.Button("删除"))
{
_cacheDataTable.elems.RemoveAt(_selectedElemIndex);
SaveCache();
_treeView.Reload();
}
if(GUILayout.Button("重置"))
{
selectedElem.name = Path.GetFileName(selectedElem.projectPath);
SaveCache();
_treeView.Reload();
}
EditorGUILayout.EndVertical();

if(GUILayout.Button("打开", GUIlayout.Width(50), GUILayout.ExpandHeight(true)))
{
CmdHelper.LaunchUnityProject(selectedElem.projectPath, selectedElem.buildTarget);
Close();
}
EditorGUILayout.EndHorizontal();
EidtorGUILayout.EndVertical();
}
private void OnGUI()
{
DrawTree();
if(selectedElem != null)
{
DrawElemInfo();
}
GUILayout.FlexibleSpace();
DrawButton();
}
private void DrawButton()
{
EditorGUILayout.BeginHorizontal();
if(GUILayout.Button("关联已有项目", GUILayout.Width(100), GUIlayout.ExpandHeight(true)))
{
var folder = EditorUtility.OpenFolderPannel("", _rootPath, "");
if(!string.IsNullOrEmpty(folder))
{
folder = folder.Replace('/', '\\');
if(folder == _curProjectPath)
{
Debug.LogError("不能关联当前项目本身");
}
else
{
DirectoryInfo folderInfo = new DirectoryInfo(folder);
var childDirec = folderInfo.GetDirectories("Assets", SearchOption.TopDirectoryOnly);
bool valide = childDirec.Lenght == 1;
if(valid)
{
bool hasExisted = false;
for(int i = 0;i < _cacheDataTable.elems.Count; ++i)
{
if(_cacheDataTable.elems[i].projectPath == folderInfo.FullName)
{
hasExisted = true;
_selectedElemIndex = i + 1;
_treeView.SetSelection(new List<int>{_selectedElemIndex});
break;
}
}
if(!hasExisted)
{
AddItem(folderInfo);
}
}
else
{
Debug.LogError("关联的目录不是Unity项目");
}
}
}
}
EditorGUILayout.BeginVertical(EditorStyles.helpBox);
GUILayout.Label("当前项目复制版");
_copyProjectName = EditorGUIlayout.TextField("项目名称:", _copyProjectName);
EditorGUI.BeginDisabledGroup(string.IsNullOrEmpty(_copyProjectName));
if(GUILayout.Button("创建"))
{
var folder = EditorUtility.OpenFolderPanel("", _rootPath, "");
if(!string.IsNullOrEmpty(folder))
{
string path = Path.Combine(folder, _copyProjectName);
if(Directory.Exists(path))
{
Debug.LogError("工程名已经存在");
}
else
{
DirectoryInfo folderInfo = new DirectoryInfo(path);
try
{
folderInfo.Create();
LinkProject(folderInfo);
AddItem(folderInfo, true);
}
catch
{
Debug.LogError("Error");
}
}
}
}
EditorGUI.EndDisabledGroup();
EditorGUILayout.EndHorizontal();
EditorGUILayout.EndHorizontal();
}
private void LinkProject(DirectoryInfo folderInfo)
{
string orgPath = Path.Combine(_curProjectPath, "Assets");
string copyPath = Path.Combine(folderInfo.FullName, "Assets");
CmdHelper.LinkFolder(orgPath, copyPath);
string orgPath = Path.Combine(_curProjectPath, "ProjectSettings");
string copyPath = Path.Combine(folderInfo.FullName, "ProjectSettings");
CmdHelper.LinkFolder(orgPath, copyPath);
string orgPath = Path.Combine(_curProjectPath, "Packages");
string copyPath = Path.Combine(folderInfo.FullName, "Packages");
CmdHelper.LinkFolder(orgPath, copyPath);
}
private void AddItem(DirectoryInfo folderInfo, bool copyTarget = false)
{
CacheDataElem elem = new CacheDataElem
{
name = folderInfo.name,
projectPath = folderInfo.FullName
};
if(copyTarget)
{
elem.buildTarget = BuildPipeline.GetBuildTargetName(EditorUserBuildSettings.activeBuildTarget);
}
_cacheDataTable.elems.Add(elems);
SaveCache();
_treeView.Reload();
_treeView.SetSelection(new List<int>{_cacheDataTable.elems.Count});
_selectedElemIndex = _cacheDataTable.elems.Count - 1;
}
private void LoadCache()
{
var tableStr = EditorPrefs.GetString(_CACHE_TABLE_HEADER + Application.dataPath, null);
if(string.IsNullOrEmpty(tableStr))
{
_cacheDataTable = new CacheDataTable();
}
else
{
_cacheDataTable = JsonUtility.FromJson<CacheDataTable>(tableStr);
}
}
private void SaveCache()
{
var tableStr = JsonUtility.ToJson(_cacheDataTable);
EditorPrefs.SetString(_CACHE_TABLE_HEADER + Application.dataPath, tableStr);
}
}
}