From edc2e1c34b276ed95711622ac4e870a9ef35da9e Mon Sep 17 00:00:00 2001 From: esnya Date: Tue, 9 Mar 2021 18:21:25 +0900 Subject: [PATCH] :sparkles: feat: Credit generator --- .../Editor/CreditGeneratorInspector.cs | 96 +++++++++++++++++++ .../Editor/CreditGeneratorInspector.cs.meta | 11 +++ .../Editor/EsnyaUnityTools.Editor.asmdef | 4 +- .../Scripts/CreditGenerator.cs | 14 +++ .../Scripts/CreditGenerator.cs.meta | 11 +++ .../Scripts/EsnyaUnityTools.Scripts.asmdef | 4 +- 6 files changed, 138 insertions(+), 2 deletions(-) create mode 100644 Assets/EsnyaUnityTools/Editor/CreditGeneratorInspector.cs create mode 100644 Assets/EsnyaUnityTools/Editor/CreditGeneratorInspector.cs.meta create mode 100644 Assets/EsnyaUnityTools/Scripts/CreditGenerator.cs create mode 100644 Assets/EsnyaUnityTools/Scripts/CreditGenerator.cs.meta diff --git a/Assets/EsnyaUnityTools/Editor/CreditGeneratorInspector.cs b/Assets/EsnyaUnityTools/Editor/CreditGeneratorInspector.cs new file mode 100644 index 0000000..661065d --- /dev/null +++ b/Assets/EsnyaUnityTools/Editor/CreditGeneratorInspector.cs @@ -0,0 +1,96 @@ +using System.Collections.Generic; +using System.IO; +using System.Linq; +using UnityEngine; +using UnityEditor; + +namespace EsnyaFactory { + [CustomEditor(typeof(CreditGenerator))] + public class CreditGeneratorInspector : Editor { + public bool filesFoldout, addFoldout = false; + public List files = new List(); + + static List GetFileList(CreditGenerator generator) + { + return generator.fileNames + .Split(' ') + .SelectMany(AssetDatabase.FindAssets) + .Select(AssetDatabase.GUIDToAssetPath) + .ToList(); + } + + public override void OnInspectorGUI() { + var generator = target as CreditGenerator; + using (var change = new EditorGUI.ChangeCheckScope()) { + base.OnInspectorGUI(); + + if (change.changed) files = GetFileList(generator); + } + + + EditorGUILayout.Space(); + + filesFoldout = EditorGUILayout.Foldout(filesFoldout, "License Files"); + if (filesFoldout) { + generator.files = generator.files + .Where(file => { + using (new EditorGUILayout.HorizontalScope()) { + EditorGUILayout.LabelField(file); + return !GUILayout.Button("Remove"); + } + }) + .ToList(); + } + + EditorGUILayout.Space(); + + addFoldout = EditorGUILayout.Foldout(addFoldout, "Add License Files"); + if (addFoldout) { + using (var change = new EditorGUI.ChangeCheckScope()) { + var added = files + .Where(file => !generator.files.Contains(file)) + .Where(file => { + using (new EditorGUILayout.HorizontalScope()) { + EditorGUILayout.LabelField(file); + return GUILayout.Button("Add"); + } + }) + .ToList(); + + if (change.changed) { + generator.files = generator.files.Where(files.Contains).Concat(added).OrderBy(f => f).ToList(); + } + } + } + + EditorGUILayout.Space(); + + if (GUILayout.Button("Refresh File List")) { + files = GetFileList(generator); + } + + if (GUILayout.Button("Update Text")) { + var lines = generator.files + .Where(File.Exists) + .Select(file => ( + Path.GetFileName(Path.GetDirectoryName(file)), + File.ReadAllLines(file).Select(line => line.Trim()) + )) + .Select(t => { + var name = t.Item1; + var license = t.Item2.FirstOrDefault(line => line.ToLower().Contains("license")); + if (license.Contains("SIL Open Font License")) license = "SIL Open Font License"; + var copyright = license == "Apache License" + ? "" + : t.Item2.FirstOrDefault(line => line.ToLower().Contains("copyright")); + return generator.format.Replace("%name%", name).Replace("%license%", license).Replace("%copyright%", copyright); + }) + .Prepend(generator.prefix) + .Append(generator.suffix) + .Where(line => !string.IsNullOrWhiteSpace(line)); + generator.targetText.text = string.Join("\n", lines); + generator.targetText.ForceMeshUpdate(); + } + } + } +} diff --git a/Assets/EsnyaUnityTools/Editor/CreditGeneratorInspector.cs.meta b/Assets/EsnyaUnityTools/Editor/CreditGeneratorInspector.cs.meta new file mode 100644 index 0000000..3b25000 --- /dev/null +++ b/Assets/EsnyaUnityTools/Editor/CreditGeneratorInspector.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: d9ddc74f08b774d44814cdc1c8a255ff +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/EsnyaUnityTools/Editor/EsnyaUnityTools.Editor.asmdef b/Assets/EsnyaUnityTools/Editor/EsnyaUnityTools.Editor.asmdef index 8e83775..ee374f8 100644 --- a/Assets/EsnyaUnityTools/Editor/EsnyaUnityTools.Editor.asmdef +++ b/Assets/EsnyaUnityTools/Editor/EsnyaUnityTools.Editor.asmdef @@ -3,7 +3,9 @@ "references": [ "Unity.Postprocessing.Runtime", "Unity.Postprocessing.Editor", - "EsbyaUnityTools.Scripts" + "EsbyaUnityTools.Scripts", + "Unity.TextMeshPro", + "Unity.TextMeshPro.Editor" ], "optionalUnityReferences": [], "includePlatforms": [ diff --git a/Assets/EsnyaUnityTools/Scripts/CreditGenerator.cs b/Assets/EsnyaUnityTools/Scripts/CreditGenerator.cs new file mode 100644 index 0000000..4bed7e8 --- /dev/null +++ b/Assets/EsnyaUnityTools/Scripts/CreditGenerator.cs @@ -0,0 +1,14 @@ +using System.Collections.Generic; +using UnityEngine; +using TMPro; + +namespace EsnyaFactory { + public class CreditGenerator : MonoBehaviour { + public TMP_Text targetText; + public string fileNames = "LICENSE OFL"; + public string prefix = "Credits:"; + public string suffix = ""; + public string format = " %name% [%license%]: %copyright%"; + [HideInInspector] public List files; + } +} diff --git a/Assets/EsnyaUnityTools/Scripts/CreditGenerator.cs.meta b/Assets/EsnyaUnityTools/Scripts/CreditGenerator.cs.meta new file mode 100644 index 0000000..154052e --- /dev/null +++ b/Assets/EsnyaUnityTools/Scripts/CreditGenerator.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 4e8cd7f7e9cdf6648abe94b3c4a773b1 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/EsnyaUnityTools/Scripts/EsnyaUnityTools.Scripts.asmdef b/Assets/EsnyaUnityTools/Scripts/EsnyaUnityTools.Scripts.asmdef index a1cc81e..51a6a32 100644 --- a/Assets/EsnyaUnityTools/Scripts/EsnyaUnityTools.Scripts.asmdef +++ b/Assets/EsnyaUnityTools/Scripts/EsnyaUnityTools.Scripts.asmdef @@ -1,6 +1,8 @@ { "name": "EsbyaUnityTools.Scripts", - "references": [], + "references": [ + "Unity.TextMeshPro" + ], "optionalUnityReferences": [], "includePlatforms": [], "excludePlatforms": [],