Roslyn instead UnityCodeGen

This commit is contained in:
ThomasCharester committed 2026-10-13 18:13:23 +03:00
1 parent e05cf28266
commit e8393a28ad
694 files changed
+24070 -47982

No files matched your search

Binary file not shown.

Before

Width:  |  Height:  |  Size: 875 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 81 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 80 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 73 KiB

@@ -1,8 +0,0 @@
fileFormatVersion: 2
guid: 909e2019934834fae9f46ed0781155ed
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
@@ -1,16 +0,0 @@
{
"name": "AnnulusGames.UnityCodeGen.Editor",
"rootNamespace": "",
"references": [],
"includePlatforms": [
"Editor"
],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": false,
"precompiledReferences": [],
"autoReferenced": true,
"defineConstraints": [],
"versionDefines": [],
"noEngineReferences": false
}
@@ -1,7 +0,0 @@
fileFormatVersion: 2
guid: 81d29a5e4682947dfa73ba6bd09fe0be
AssemblyDefinitionImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
@@ -1,8 +0,0 @@
fileFormatVersion: 2
guid: e6b8efca00a3340c398c1120e98ea7bd
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
@@ -1,8 +0,0 @@
namespace UnityCodeGen
{
internal class CodeText
{
public string fileName;
public string text;
}
}
@@ -1,11 +0,0 @@
fileFormatVersion: 2
guid: 1d7126fb29d2849a597d936778dbedd4
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -1,7 +0,0 @@
using System;
namespace UnityCodeGen
{
[AttributeUsage(AttributeTargets.Class)]
public sealed class GeneratorAttribute : Attribute { }
}
@@ -1,11 +0,0 @@
fileFormatVersion: 2
guid: 2a85bd5042f1843a68ccc259b4e9f546
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -1,23 +0,0 @@
using System.Collections.Generic;
namespace UnityCodeGen
{
public sealed class GeneratorContext
{
private List<CodeText> _codeList = new List<CodeText>();
internal IReadOnlyList<CodeText> codeList => _codeList;
private string _overrideFolderPath = null;
internal string overrideFolderPath => _overrideFolderPath;
public void AddCode(string fileName, string text)
{
_codeList.Add(new CodeText() { fileName = fileName, text = text });
}
public void OverrideFolderPath(string path)
{
_overrideFolderPath = path;
}
}
}
@@ -1,11 +0,0 @@
fileFormatVersion: 2
guid: 34c0377667ac24572a456cf84c47aa9c
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -1,7 +0,0 @@
namespace UnityCodeGen
{
public interface ICodeGenerator
{
public void Execute(GeneratorContext context);
}
}
@@ -1,11 +0,0 @@
fileFormatVersion: 2
guid: 584462a42ae54483eba7152b41c86e9b
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -1,12 +0,0 @@
namespace UnityCodeGen
{
public static class UnityCodeGenUtility
{
public const string defaultFolderPath = "Assets/UnityCodeGen.Generated";
public static void Generate()
{
ScriptFileGenerator.Generate();
}
}
}
@@ -1,11 +0,0 @@
fileFormatVersion: 2
guid: 1c80887af80d8407b833eb2670ae2eae
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -1,29 +0,0 @@
using UnityEditor;
namespace UnityCodeGen
{
static class MenuItems
{
const string MENU_GENERATE = "Tools/UnityCodeGen/Generate";
const string MENU_TOGGLE_AUTO_GENERATE = "Tools/UnityCodeGen/Auto-generate on Compile";
[InitializeOnLoadMethod]
static void Init()
{
Menu.SetChecked(MENU_TOGGLE_AUTO_GENERATE, UnityCodeGenSettings.autoGenerateOnCompile);
}
[MenuItem(MENU_GENERATE)]
static void Generate()
{
ScriptFileGenerator.Generate();
}
[MenuItem(MENU_TOGGLE_AUTO_GENERATE)]
static void ToggleAutoGenerate()
{
UnityCodeGenSettings.autoGenerateOnCompile = !UnityCodeGenSettings.autoGenerateOnCompile;
Menu.SetChecked(MENU_TOGGLE_AUTO_GENERATE, UnityCodeGenSettings.autoGenerateOnCompile);
}
}
}
@@ -1,11 +0,0 @@
fileFormatVersion: 2
guid: 2aa0ed75c5b414fdebd1ea068e1e6b50
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -1,8 +0,0 @@
fileFormatVersion: 2
guid: 191f0e1d40c654f20bf570a27ebeb5eb
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
@@ -1,25 +0,0 @@
using UnityEditor;
namespace UnityCodeGen
{
public static class UnityCodeGenSettings
{
const string KEY_GENERATE_ON_COMPILE = "UnityCodeGen-AutoGenerateOnCompile";
public static bool autoGenerateOnCompile
{
get
{
if (bool.TryParse(EditorUserSettings.GetConfigValue(KEY_GENERATE_ON_COMPILE), out var result))
{
return result;
}
return false;
}
set
{
EditorUserSettings.SetConfigValue(KEY_GENERATE_ON_COMPILE, value.ToString());
}
}
}
}
@@ -1,11 +0,0 @@
fileFormatVersion: 2
guid: 399313ef1fc1343fd80cab2c0e90a46a
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -1,8 +0,0 @@
fileFormatVersion: 2
guid: d54ccfe58f28548af9fa4414edbcfb3a
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
@@ -1,129 +0,0 @@
using System;
using System.Linq;
using System.Reflection;
using System.IO;
using UnityEditor;
namespace UnityCodeGen
{
internal static class ScriptFileGenerator
{
const string KEY_ISGENERATING = "UnityCodeGen-IsGenerating";
const string EXTENSION_META = ".meta";
static bool isGenerating
{
get
{
if (bool.TryParse(EditorUserSettings.GetConfigValue(KEY_ISGENERATING), out var result))
{
return result;
}
return false;
}
set
{
EditorUserSettings.SetConfigValue(KEY_ISGENERATING, value.ToString());
}
}
// static List<string> fileNames = new List<string>();
[InitializeOnLoadMethod]
static void Initialize()
{
if (isGenerating)
{
isGenerating = false;
}
else if (UnityCodeGenSettings.autoGenerateOnCompile)
{
Generate();
}
}
internal static void Generate()
{
if (isGenerating) return;
isGenerating = true;
// fileNames.Clear();
var generatorTypes = TypeCache.GetTypesDerivedFrom<ICodeGenerator>()
.Where(x => !x.IsAbstract && x.GetCustomAttribute<GeneratorAttribute>() != null);
var changed = false;
foreach (var t in generatorTypes)
{
var generator = (ICodeGenerator)Activator.CreateInstance(t);
var context = new GeneratorContext();
generator.Execute(context);
if (GenerateScriptFromContext(context))
{
changed = true;
}
}
// foreach (var file in Directory.GetFiles(FOLDER_PATH))
// {
// var name = Path.GetFileName(file);
// if (Path.GetExtension(name) != EXTENSION_META && !fileNames.Contains(name))
// {
// AssetDatabase.DeleteAsset(file);
// changed = true;
// }
// }
if (changed)
{
AssetDatabase.Refresh();
AssetDatabase.SaveAssets();
}
}
static bool GenerateScriptFromContext(GeneratorContext context)
{
var changed = false;
var folderPath = context.overrideFolderPath ?? UnityCodeGenUtility.defaultFolderPath;
if (!Directory.Exists(folderPath))
{
Directory.CreateDirectory(folderPath);
}
foreach (var code in context.codeList)
{
var hierarchy = code.fileName.Split('/');
var fileName = hierarchy[hierarchy.Length - 1];
var path = folderPath;
for (int i = 0; i < hierarchy.Length; i++)
{
path += "/" + hierarchy[i];
if (i == hierarchy.Length - 1) break;
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
}
if (File.Exists(path))
{
var text = File.ReadAllText(path);
if (text == code.text)
{
// fileNames.Add(code.fileName);
continue;
}
}
File.WriteAllText(path, code.text);
// fileNames.Add(code.fileName);
changed = true;
}
return changed;
}
}
}
@@ -1,11 +0,0 @@
fileFormatVersion: 2
guid: baab6cdaea8de4502b39347e43e52c4d
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -1,8 +0,0 @@
fileFormatVersion: 2
guid: f69ab95fa787948b6b39c24a13619948
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
@@ -1,18 +0,0 @@
{
"name": "AnnulusGames.UnityCodeGen.Samples.TagsAndLayers.Editor",
"rootNamespace": "",
"references": [
"GUID:81d29a5e4682947dfa73ba6bd09fe0be"
],
"includePlatforms": [
"Editor"
],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": false,
"precompiledReferences": [],
"autoReferenced": true,
"defineConstraints": [],
"versionDefines": [],
"noEngineReferences": false
}
@@ -1,7 +0,0 @@
fileFormatVersion: 2
guid: 305b8537f51ba4b77b20a2a43a0fbc23
AssemblyDefinitionImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
@@ -1,24 +0,0 @@
using System.Linq;
using UnityEngine;
using UnityEditorInternal;
namespace UnityCodeGen.Samples.TagsAndLayers
{
[Generator]
public class LayersGenerator : ICodeGenerator
{
public void Execute(GeneratorContext context)
{
var tags = InternalEditorUtility.layers
.Select(x => $"public const int {x.Replace(" ", string.Empty)} = {LayerMask.NameToLayer(x)};");
var fields = string.Join("\n\t", tags);
var code =
@$"//<auto-generated/>
public static class Layers
{{
{fields}
}}";
context.AddCode("Tags & Layers/Layers.Generated.cs", code);
}
}
}
@@ -1,11 +0,0 @@
fileFormatVersion: 2
guid: 3b6e0724a537348d0b49ebeac5195510
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -1,33 +0,0 @@
using System.Linq;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using UnityEditorInternal;
namespace UnityCodeGen.Samples.TagsAndLayers
{
[Generator]
public class SortingLayersGenerator : ICodeGenerator
{
public void Execute(GeneratorContext context)
{
var tagManager = new SerializedObject(AssetDatabase.LoadAllAssetsAtPath("ProjectSettings/TagManager.asset")[0]);
var sortingLayer = tagManager.FindProperty("m_SortingLayers");
var list = new List<string>();
for (int i = 0; i < sortingLayer.arraySize; ++i)
{
list.Add(sortingLayer.GetArrayElementAtIndex(i).displayName);
}
var layers = list
.Select(x => $"public const string {x.Replace(" ", string.Empty)} = \"{x}\";");
var fields = string.Join("\n\t", layers);
var code =
@$"//<auto-generated/>
public static class SortingLayers
{{
{fields}
}}";
context.AddCode("Tags & Layers/SortingLayers.Generated.cs", code);
}
}
}
@@ -1,11 +0,0 @@
fileFormatVersion: 2
guid: d8ea2bc9baf9a43ec919eeceb37efc2a
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -1,23 +0,0 @@
using System.Linq;
using UnityEditorInternal;
namespace UnityCodeGen.Samples.TagsAndLayers
{
[Generator]
public class TagsGenerator : ICodeGenerator
{
public void Execute(GeneratorContext context)
{
var tags = InternalEditorUtility.tags
.Select(x => $"public const string {x.Replace(" ", string.Empty)} = \"{x}\";");
var fields = string.Join("\n\t", tags);
var code =
@$"//<auto-generated/>
public static class Tags
{{
{fields}
}}";
context.AddCode("Tags & Layers/Tags.Generated.cs", code);
}
}
}
@@ -1,11 +0,0 @@
fileFormatVersion: 2
guid: cb6c2e2aea37049bfb1ac699f1aeaabd
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -1,21 +0,0 @@
{
"name": "com.annulusgames.unity-codegen",
"displayName": "Unity CodeGen",
"version": "1.0.0",
"unity": "2020.1",
"license": "MIT",
"author": {
"name": "Annulus Games",
"url": "https://github.com/AnnulusGames"
},
"description": "Code Generation Library for Unity Editor",
"dependencies": {},
"samples": [
{
"displayName": "Tags & Layers",
"description": "A sample that generates a class that manages Tags, Layers and Sorting Layers with constants.",
"path": "Samples~/Tags & Layers"
}
],
"_fingerprint": "c93461327f0c4df4206a1cf95ac0c3a278fe0f05"
}
@@ -1,7 +0,0 @@
fileFormatVersion: 2
guid: 27b09209611ec460a919e4ebd38c6885
TextScriptImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant: