代码

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

using System.IO;
using UnityEngine;

public class ScriptChange : UnityEditor.AssetModificationProcessor
{
static string namespaceName = "XC";

/// <summary>
/// 将要创建资源时会调用这个函数
/// </summary>
static void OnWillCreateAsset(string path)
{
//导入资源的路径,不知道具体是什么的时候建议输出查看
Debug.Log(path);
string str = path.Replace(".meta", "");
string[] splitArgs = str.Split('.');
if (splitArgs[splitArgs.Length - 1].Equals("cs"))
{
//Debug.Log("导入的是脚本");
string[] newSplitArgs = str.Split('/');
bool isEditor = false;
foreach (var item in newSplitArgs)
{
if (item.Equals("Editor"))
{
isEditor = true;
break; ;
}
}
if (isEditor) return;
ParseAndChangeScript(str.Substring(6, str.Length - 6));
}
}



private static void ParseAndChangeScript(string path)
{
string str = File.ReadAllText(Application.dataPath + path);
if (string.IsNullOrEmpty(str))
{
Debug.Log("读取出错了,Application.dataPath=" + Application.dataPath + " path=" + path);
return;
}

string newStr = "";
//增加命名空间
if (!str.Contains("namespace"))
{
if (!string.IsNullOrEmpty(namespaceName))
{
int length = str.IndexOf("public");
newStr += str.Substring(0, length);
string extraStr = "";
string[] extraStrs = str.Substring(length, str.Length - length).Replace("\r\n", "\n").Split('\n');
foreach (var item in extraStrs)
{
extraStr += "\t" + item + "\r\n";
}


newStr += "\r\nnamespace " + namespaceName + "\r\n{\r\n" + extraStr + "}";
//newStr = newStr.Replace("\n", "\r\n");
//newStr = newStr.Replace('\r', ' ');
}
else
{
newStr = str;
}
File.WriteAllText(Application.dataPath + path, newStr);
}
}
}