Skip to content

Commit

Permalink
✨ feat: Credit generator
Browse files Browse the repository at this point in the history
  • Loading branch information
esnya committed Mar 9, 2021
1 parent eb3f990 commit edc2e1c
Show file tree
Hide file tree
Showing 6 changed files with 138 additions and 2 deletions.
96 changes: 96 additions & 0 deletions Assets/EsnyaUnityTools/Editor/CreditGeneratorInspector.cs
Original file line number Diff line number Diff line change
@@ -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<string> files = new List<string>();

static List<string> 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();
}
}
}
}
11 changes: 11 additions & 0 deletions Assets/EsnyaUnityTools/Editor/CreditGeneratorInspector.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion Assets/EsnyaUnityTools/Editor/EsnyaUnityTools.Editor.asmdef
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
"references": [
"Unity.Postprocessing.Runtime",
"Unity.Postprocessing.Editor",
"EsbyaUnityTools.Scripts"
"EsbyaUnityTools.Scripts",
"Unity.TextMeshPro",
"Unity.TextMeshPro.Editor"
],
"optionalUnityReferences": [],
"includePlatforms": [
Expand Down
14 changes: 14 additions & 0 deletions Assets/EsnyaUnityTools/Scripts/CreditGenerator.cs
Original file line number Diff line number Diff line change
@@ -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<string> files;
}
}
11 changes: 11 additions & 0 deletions Assets/EsnyaUnityTools/Scripts/CreditGenerator.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
{
"name": "EsbyaUnityTools.Scripts",
"references": [],
"references": [
"Unity.TextMeshPro"
],
"optionalUnityReferences": [],
"includePlatforms": [],
"excludePlatforms": [],
Expand Down

0 comments on commit edc2e1c

Please sign in to comment.