Skip to content

Commit 945e700

Browse files
AmbratolmAmbratolm
Ambratolm
authored and
Ambratolm
committed
Initial Commit
0 parents  commit 945e700

18 files changed

+888
-0
lines changed

.gitignore

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# This .gitignore file should be placed at the root of your Unity project directory
2+
#
3+
# Get latest from https://github.com/github/gitignore/blob/main/Unity.gitignore
4+
#
5+
/[Ll]ibrary/
6+
/[Tt]emp/
7+
/[Oo]bj/
8+
/[Bb]uild/
9+
/[Bb]uilds/
10+
/[Ll]ogs/
11+
/[Uu]ser[Ss]ettings/
12+
13+
# MemoryCaptures can get excessive in size.
14+
# They also could contain extremely sensitive data
15+
/[Mm]emoryCaptures/
16+
17+
# Recordings can get excessive in size
18+
/[Rr]ecordings/
19+
20+
# Uncomment this line if you wish to ignore the asset store tools plugin
21+
# /[Aa]ssets/AssetStoreTools*
22+
23+
# Autogenerated Jetbrains Rider plugin
24+
/[Aa]ssets/Plugins/Editor/JetBrains*
25+
26+
# Visual Studio cache directory
27+
.vs/
28+
29+
# Gradle cache directory
30+
.gradle/
31+
32+
# Autogenerated VS/MD/Consulo solution and project files
33+
ExportedObj/
34+
.consulo/
35+
*.csproj
36+
*.unityproj
37+
*.sln
38+
*.suo
39+
*.tmp
40+
*.user
41+
*.userprefs
42+
*.pidb
43+
*.booproj
44+
*.svd
45+
*.pdb
46+
*.mdb
47+
*.opendb
48+
*.VC.db
49+
50+
# Unity3D generated meta files
51+
*.pidb.meta
52+
*.pdb.meta
53+
*.mdb.meta
54+
55+
# Unity3D generated file on crash reports
56+
sysinfo.txt
57+
58+
# Builds
59+
*.apk
60+
*.aab
61+
*.unitypackage
62+
*.app
63+
64+
# Crashlytics generated file
65+
crashlytics-build.properties
66+
67+
# Packed Addressables
68+
/[Aa]ssets/[Aa]ddressable[Aa]ssets[Dd]ata/*/*.bin*
69+
70+
# Temporary auto-generated Android Assets
71+
/[Aa]ssets/[Ss]treamingAssets/aa.meta
72+
/[Aa]ssets/[Ss]treamingAssets/aa/*

Editor.meta

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Editor/AssemblyDefinition.asmdef

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"name": "Ambratolm.ScriptGenerator",
3+
"references": [],
4+
"includePlatforms": [],
5+
"excludePlatforms": [],
6+
"allowUnsafeCode": false,
7+
"overrideReferences": false,
8+
"precompiledReferences": [],
9+
"autoReferenced": true,
10+
"defineConstraints": [],
11+
"versionDefines": [],
12+
"noEngineReferences": false
13+
}

Editor/AssemblyDefinition.asmdef.meta

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Editor/ClassGenerator.cs

+98
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
using Ambratolm.ScriptGenerator.Utilities;
2+
using System;
3+
using System.IO;
4+
using UnityEditor;
5+
using UnityEngine;
6+
using static Ambratolm.ScriptGenerator.Utilities.SelectionUtility;
7+
8+
namespace Ambratolm.ScriptGenerator
9+
{
10+
/// <summary>
11+
/// Generates C# classes from template assets.
12+
/// </summary>
13+
internal sealed class ClassGenerator : AssetModificationProcessor
14+
{
15+
private const string _templateExtension = ".template.cs";
16+
private const string _assetMenuItemPath = "Assets/Generate C# Class";
17+
private static string _templateFilePath;
18+
private static string _templateFileName;
19+
20+
//----------------------------------------------------------------------------------------------------
21+
22+
/// <summary>
23+
/// Menu item function. It generates a class file from the selected class template asset.
24+
/// </summary>
25+
[MenuItem(_assetMenuItemPath)]
26+
public static void OnGenerate() => Generate();
27+
28+
/// <summary>
29+
/// Menu item validation function. It validates the selected asset.
30+
/// <para>
31+
/// Called before invoking the menu item function with the same itemName on the MenuItem attribute.
32+
/// </para>
33+
/// </summary>
34+
/// <returns>A boolean value that indicates whether the validation succeeded or not.</returns>
35+
[MenuItem(_assetMenuItemPath, isValidateFunction: true)]
36+
private static bool OnValidate() => Validate(throwException: false);
37+
38+
//----------------------------------------------------------------------------------------------------
39+
40+
/// <summary>
41+
/// Generates a C# class file from a template and saves it to the output location.
42+
/// </summary>
43+
private static void Generate()
44+
{
45+
Validate();
46+
Type templateType = GetClassTemplateType(out string templateText);
47+
ClassTemplate template = Activator.CreateInstance(templateType, templateText) as ClassTemplate;
48+
string className = template.HasClass ? template.ClassName : "_";
49+
string fileName = $"{className}.cs";
50+
string filePath = _templateFilePath.Replace(_templateFileName, fileName);
51+
File.WriteAllText(filePath, contents: template.GenerateClassCode());
52+
UnityEngine.Object generatedAsset = AssetDatabase.LoadAssetAtPath(filePath, typeof(UnityEngine.Object));
53+
Debug.Log($"\"{className}\" class generated at \"{filePath}\"", generatedAsset);
54+
AssetDatabase.Refresh();
55+
}
56+
57+
/// <summary>
58+
/// Validates the selected asset.
59+
/// <para>
60+
/// The asset is considered valid if it is a class template script that has the template extension.
61+
/// </para>
62+
/// </summary>
63+
/// <param name="throwException">Whether to throw an exception if the validation fails.</param>
64+
/// <returns>True if the asset is valid, false otherwise.</returns>
65+
private static bool Validate(bool throwException = true)
66+
{
67+
_templateFilePath = SelectedAssetPath;
68+
_templateFileName = Path.GetFileName(_templateFilePath);
69+
bool assetIsValid = SelectedAssetIsScript;
70+
if (throwException && !assetIsValid) throw new InvalidDataException($"Invalid asset type at \"{_templateFilePath}\". " +
71+
$"\"{_templateFileName}\" asset should be a script.");
72+
assetIsValid &= FileUtility.Validate(_templateFilePath, _templateExtension, out Exception exception);
73+
if (throwException && !assetIsValid)
74+
throw exception;
75+
return assetIsValid;
76+
}
77+
78+
/// <summary>
79+
/// Gets the type of the ClassTemplate class defined in the selected script and its text.
80+
/// </summary>
81+
/// <param name="classTemplateText">The text of the class template script.</param>
82+
/// <returns>The class template type.</returns>
83+
/// <exception cref="InvalidDataException">
84+
/// Thrown when the script is invalid or does not inherit from ClassTemplate.
85+
/// </exception>
86+
private static Type GetClassTemplateType(out string classTemplateText)
87+
{
88+
MonoScript script = Selection.activeObject as MonoScript;
89+
Type classTemplateType = script.GetClass();
90+
if (classTemplateType is null) throw new InvalidDataException($"Invalid script at \"{_templateFilePath}\". " +
91+
$"Script doesn't implement any class. It should implement a class that inherits from \"{nameof(ClassTemplate)}\"");
92+
if (!classTemplateType.IsSubclassOf(typeof(ClassTemplate))) throw new InvalidDataException($"Invalid script at \"{_templateFilePath}\". " +
93+
$"Implemented \"{classTemplateType.Name}\" class should inherit from \"{nameof(ClassTemplate)}\".");
94+
classTemplateText = script.text;
95+
return classTemplateType;
96+
}
97+
}
98+
}

Editor/ClassGenerator.cs.meta

+11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)