forked from JeromeJ/Unity-ImportNinja
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path81b-Import Ninja-ImportNinja.cs.txt
160 lines (124 loc) · 4.76 KB
/
81b-Import Ninja-ImportNinja.cs.txt
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
using System;
using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEngine;
using System.Linq;
public class ImportNinja : ScriptableObject
{
public DirectoryInfo projectDirectory;
public string unityDirectory;
public string importNinja_template;
public Dictionary<string, string> vars = new Dictionary<string, string>();
// STATIC //
static private string _ninjaData = "_ninjaData";
static private string _editorTemplate = "Editor.ninjatemplate.cs.txt";
static public string ninjaData { get { return _ninjaData; } }
static public string editorTemplate { get { return _editorTemplate; } }
static private ImportNinja _data;
static public ImportNinja data
{
set { _data = value; }
get
{
if (_data == null)
{
string ninjaPath = AssetDatabase.FindAssets(_ninjaData).FirstOrDefault();
_data = AssetDatabase.LoadAssetAtPath<ImportNinja>(AssetDatabase.GUIDToAssetPath(ninjaPath));
}
return _data;
}
}
}
[InitializeOnLoad]
public class ImportNinjaInitializer
{
static ImportNinjaInitializer()
{
Debug.Log("Your Personal Ninja");
if (ImportNinja.data != null)
return;
ImportNinja.data = ScriptableObject.CreateInstance<ImportNinja>();
ImportNinja data = ImportNinja.data;
data.projectDirectory = Directory.GetParent(Application.dataPath);
data.unityDirectory = Directory.GetParent(EditorApplication.applicationPath).FullName;
data.importNinja_template = data.unityDirectory + @"\Data\Resources\ScriptTemplates\ImportNinjaTemplate\";
data.vars.Add("PROJECTNAME", data.projectDirectory.Name);
string from = data.importNinja_template;
string to = Application.dataPath;
foreach (EditablePathInfo node in DirectoryCopy(from, to, copySubDirs: true))
{
foreach (KeyValuePair<string, string> rr in data.vars)
node.nodeName = node.nodeName.Replace("#" + rr.Key + "#", rr.Value);
string nodeName = node.nodeName;
if (nodeName == ImportNinja.ninjaData)
{
AssetDatabase.CreateAsset(data, node.relativePath + ".asset");
node.dontSave = true;
}
}
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
}
/// <summary>
/// Source: https://docs.microsoft.com/en-us/dotnet/standard/io/how-to-copy-directories
/// Modified by JeromeJ.
/// </summary>
/// <param name="vars"></param>
private static IEnumerable<EditablePathInfo> DirectoryCopy(string sourceDirName, string destDirName, bool copySubDirs)
{
// Get the subdirectories for the specified directory.
DirectoryInfo dir = new DirectoryInfo(sourceDirName);
if (!dir.Exists)
throw new DirectoryNotFoundException("Source directory does not exist or could not be found: " + sourceDirName);
DirectoryInfo[] dirs = dir.GetDirectories();
// If the destination directory doesn't exist, create it.
Directory.CreateDirectory(destDirName);
// Get the files in the directory and copy them to the new location.
FileInfo[] files = dir.GetFiles();
foreach (FileInfo file in files)
{
EditablePathInfo path = new EditablePathInfo(Path.Combine(destDirName, file.Name));
yield return path; // By reference
if (path.dontSave)
continue;
file.CopyTo(path.fullPath);
}
// If copying subdirectories, copy them and their contents to new location.
if (copySubDirs)
{
foreach (DirectoryInfo subdir in dirs)
{
EditablePathInfo path = new EditablePathInfo(Path.Combine(destDirName, subdir.Name));
yield return path; // By reference
if (path.dontSave)
continue;
foreach (EditablePathInfo node in DirectoryCopy(subdir.FullName, path.fullPath, copySubDirs))
{
// Can be modified as well before execution resumes
yield return node;
}
}
}
}
}
public class EditablePathInfo
{
public bool dontSave = false;
public string fullPath;
public string nodeName
{
get { return Path.GetFileName(fullPath); }
set {
fullPath = Path.Combine(Path.GetDirectoryName(fullPath), value);
}
}
public string relativePath
{
get { return new Uri(Application.dataPath).MakeRelativeUri(new Uri(fullPath)).ToString(); }
}
public EditablePathInfo(string _fullPath)
{
fullPath = _fullPath;
}
}