Skip to content

Commit 3924f1a

Browse files
committed
feat: in Unity 2020.2 or later, initialize CSharpCompilerSettings for first compilation
1 parent 0a988fa commit 3924f1a

File tree

5 files changed

+107
-0
lines changed

5 files changed

+107
-0
lines changed

Plugins/ExternalCSharpCompiler.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"name": "ExternalCSharpCompiler_",
3+
"references": [],
4+
"optionalUnityReferences": [],
5+
"includePlatforms": [
6+
"Editor"
7+
],
8+
"excludePlatforms": [],
9+
"allowUnsafeCode": false,
10+
"overrideReferences": false,
11+
"precompiledReferences": [],
12+
"autoReferenced": false,
13+
"defineConstraints": [
14+
"CSC_SETTINGS_DEVELOP"
15+
]
16+
}

Plugins/ExternalCSharpCompiler/ExternalCSharpCompiler.asmdef.meta

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
using System;
2+
using System.IO;
3+
using UnityEditor;
4+
using System.Linq;
5+
using System.Reflection;
6+
using System.Runtime.CompilerServices;
7+
8+
namespace Coffee.CSharpCompilerSettings
9+
{
10+
internal class ExternalCSharpCompiler
11+
{
12+
[InitializeOnLoadMethod]
13+
static void InitializeOnLoad()
14+
{
15+
var coreAssemblyName = typeof(ExternalCSharpCompiler).Assembly.GetName().Name;
16+
if (coreAssemblyName == "ExternalCSharpCompiler_") return;
17+
18+
Console.WriteLine("[CSharpCompilerSettings] Initialize CSharpCompilerSettings for first compilation: " + typeof(ExternalCSharpCompiler));
19+
20+
var filepath = "Temp/" + typeof(ExternalCSharpCompiler).Assembly.GetName().Name + ".loaded";
21+
if (File.Exists(filepath)) return;
22+
File.WriteAllText(filepath, "");
23+
24+
var cscTypes = AssetDatabase.FindAssets("t:DefaultAsset CSharpCompilerSettings")
25+
.Select(AssetDatabase.GUIDToAssetPath)
26+
.Where(x => x.EndsWith(".dll"))
27+
.Select(Assembly.LoadFrom)
28+
.SelectMany(x => x.GetTypes())
29+
.ToArray();
30+
31+
var initializeOnLoadTypes = cscTypes
32+
.Where(x => 0 < x.GetCustomAttributes(typeof(InitializeOnLoadAttribute), false).Length);
33+
34+
var initializeOnLoadMethods = cscTypes
35+
.SelectMany(x => x.GetMethods(BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic))
36+
.Where(x => 0 < x.GetCustomAttributes(typeof(InitializeOnLoadMethodAttribute), false).Length);
37+
38+
foreach (var type in initializeOnLoadTypes)
39+
{
40+
try
41+
{
42+
Console.WriteLine(" > InitializeOnLoadAttribute: " + type);
43+
RuntimeHelpers.RunClassConstructor(type.TypeHandle);
44+
}
45+
catch (Exception e)
46+
{
47+
Console.Error.WriteLine(e.ToString());
48+
}
49+
}
50+
51+
foreach (var method in initializeOnLoadMethods)
52+
{
53+
try
54+
{
55+
Console.WriteLine(" > InitializeOnLoadMethodAttribute: " + method);
56+
method.Invoke(null, new object[0]);
57+
}
58+
catch (Exception e)
59+
{
60+
Console.Error.WriteLine(e.ToString());
61+
}
62+
}
63+
}
64+
}
65+
}

Plugins/ExternalCSharpCompiler/ExternalCSharpCompiler.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)