additional package setup

This commit is contained in:
2025-11-16 18:31:17 -05:00
parent 3da42beb46
commit 2ca8077013
55 changed files with 1746 additions and 12 deletions

View File

@@ -0,0 +1,39 @@
using System.IO;
using UnityEngine;
public class TALGeneratorSettings : ScriptableObject
{
public string FilePath;
public string FileName;
public int Seconds = 5;
private const string SettingsPath = @"ProjectSettings\TALGeneratorSettings.asset";
private static TALGeneratorSettings _instance;
public static TALGeneratorSettings GetOrCreate()
{
if (_instance == null)
_instance = CreateInstance<TALGeneratorSettings>();
var path = GetPath();
if (!File.Exists(path)) return _instance;
var json = File.ReadAllText(path);
JsonUtility.FromJsonOverwrite(json, _instance);
return _instance;
}
private static string GetPath()
{
var projectPath = Directory.GetParent(Application.dataPath).FullName;
return Path.Combine(projectPath, SettingsPath);
}
public void Save()
{
var json = JsonUtility.ToJson(this, true);
var path = GetPath();
File.WriteAllText(path, json);
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: b3a04823d2914c21a608882e87dc7d70
timeCreated: 1742631304

View File

@@ -0,0 +1,157 @@
using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEngine;
using UnityEngine.UIElements;
public class TALGeneratorSettingsProvider : SettingsProvider
{
private TALGeneratorSettings _settings;
public TALGeneratorSettingsProvider(string path, SettingsScope scopes, IEnumerable<string> keywords = null) : base(path, scopes, keywords) { }
[SettingsProvider]
public static SettingsProvider CreateCustomSettingsProvider()
{
return new TALGeneratorSettingsProvider("Project/Tags and Layers Generator", SettingsScope.Project);
}
public override void OnActivate(string searchContext, VisualElement rootElement)
{
_settings = TALGeneratorSettings.GetOrCreate();
var container = new VisualElement
{
style =
{
paddingTop = 10,
paddingLeft = 10,
paddingRight = 10
}
};
var titleLabel = new Label("Tags And Layers Generator Settings")
{
style =
{
fontSize = 14,
unityFontStyleAndWeight = FontStyle.Bold
}
};
container.Add(titleLabel);
var folderPickerRow = new VisualElement
{
style =
{
flexDirection = FlexDirection.Row,
marginBottom = 5
}
};
var folderPathField = new TextField("Selected Path")
{
value = _settings.FilePath,
tooltip = "Select a folder that the generated class will be placed into",
isReadOnly = true,
style =
{
flexGrow = 1
}
};
folderPickerRow.Add(folderPathField);
var button = new Button(() =>
{
var path = EditorUtility.OpenFolderPanel("Select a Folder", Application.dataPath, "");
if (string.IsNullOrWhiteSpace(path) || !IsInAssetFolder(path)) return;
_settings.FilePath = path;
_settings.Save();
folderPathField.value = path;
})
{
text = "Browse",
style =
{
marginLeft = 5
}
};
folderPickerRow.Add(button);
container.Add(folderPickerRow);
var fileNameRow = new VisualElement
{
style =
{
flexDirection = FlexDirection.Row,
marginBottom = 5
}
};
var fileNameField = new TextField("File Name")
{
value = _settings.FileName,
tooltip = "Select a name for the generated file",
style = { flexGrow = 1 }
};
fileNameField.RegisterValueChangedCallback(evt =>
{
_settings.FileName = evt.newValue;
_settings.Save();
});
fileNameRow.Add(fileNameField);
var csLabel = new Label(".cs")
{
style = { marginRight = 10 }
};
fileNameRow.Add(csLabel);
container.Add(fileNameRow);
var buttonRow = new VisualElement()
{
style =
{
flexGrow = 1,
flexDirection = FlexDirection.RowReverse,
justifyContent = new StyleEnum<Justify>(Justify.SpaceBetween),
marginBottom = 5
}
};
var generateButton = new Button(TagAndLayerGenerator.Generate)
{
text = "Manually Generate",
style =
{
flexGrow = 0,
marginRight = 10
}
};
buttonRow.Add(generateButton);
var secondsField = new IntegerField("AutoGenerate Threshold")
{
value = _settings.Seconds,
tooltip = "Minimum number of seconds to wait before allowing auto generation again. Minimum of 5 seconds",
style =
{
minWidth = 30
}
};
secondsField.RegisterValueChangedCallback(evt =>
{
_settings.Seconds = Mathf.Max(evt.newValue, 5);
_settings.Save();
});
buttonRow.Add(secondsField);
container.Add(buttonRow);
rootElement.Add(container);
}
private bool IsInAssetFolder(string path)
{
var fullName = Path.GetFullPath(path);
return fullName.StartsWith(Application.dataPath);
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 49bd8a9472aa46a3b4b3fa44e65a8599
timeCreated: 1742631234

View File

@@ -0,0 +1,99 @@
using System;
using System.IO;
using System.Text;
using UnityEditor;
using UnityEngine;
[InitializeOnLoad]
public static class TagAndLayerGenerator
{
private static readonly FileSystemWatcher _fileSystemWatcher;
static TagAndLayerGenerator()
{
if (_fileSystemWatcher != null) return;
var projectPath = Directory.GetParent(Application.dataPath).FullName;
var path = Path.Combine(projectPath, "ProjectSettings");
_fileSystemWatcher = new FileSystemWatcher(path, "TagManager.asset");
_fileSystemWatcher.NotifyFilter = NotifyFilters.LastWrite;
_fileSystemWatcher.Changed += OnChanged;
_fileSystemWatcher.EnableRaisingEvents = true;
Generate();
}
#pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
private static void OnChanged(object sender, FileSystemEventArgs e) => OnChanged();
#pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
private static async Awaitable OnChanged()
{
await Awaitable.MainThreadAsync();
Generate();
}
public static void Generate()
{
var settings = TALGeneratorSettings.GetOrCreate();
if (string.IsNullOrWhiteSpace(settings.FileName) || string.IsNullOrWhiteSpace(settings.FilePath))
return;
var relativeDir = Path.GetRelativePath(Application.dataPath, settings.FilePath);
var path = Path.Combine("Assets", relativeDir, settings.FileName + ".cs");
if (File.Exists(path))
{
var lastEdit = File.GetLastWriteTime(path);
var difference = DateTime.Now.Subtract(lastEdit).TotalSeconds;
if (difference < Mathf.Max(settings.Seconds, 5))
return;
}
AssetDatabase.StartAssetEditing();
var builder = new StringBuilder();
builder.AppendLine("namespace Boxfriend.Generated \n{");
builder.AppendLine(GenerateTags());
builder.AppendLine(GenerateLayers());
builder.AppendLine("}");
File.WriteAllText(path, builder.ToString());
AssetDatabase.StopAssetEditing();
AssetDatabase.ImportAsset(path);
}
private static string GenerateTags()
{
var builder = new StringBuilder();
builder.AppendLine("public static class Tags \n{");
var tags = UnityEditorInternal.InternalEditorUtility.tags;
foreach (var tag in tags)
{
builder.AppendLine($"public const string {tag} = \"{tag}\";");
builder.AppendLine($"public static readonly UnityEngine.TagHandle {tag}Handle = UnityEngine.TagHandle.GetExistingTag({tag});");
}
builder.AppendLine("}");
return builder.ToString();
}
private static string GenerateLayers()
{
var layerBuilder = new StringBuilder();
layerBuilder.AppendLine("public static class Layers \n{");
var maskBuilder = new StringBuilder();
maskBuilder.AppendLine("public static class Mask \n{");
maskBuilder.AppendLine("public const int All = int.MaxValue;\npublic const int None = 0;");
for (var i = 0; i < 32; i++)
{
var name = LayerMask.LayerToName(i);
if (string.IsNullOrWhiteSpace(name))
continue;
name = name.Replace(" ", "");
layerBuilder.AppendLine($"public const string {name} = \"{name}\";");
maskBuilder.AppendLine($"public const int {name} = 1 << {i};");
}
maskBuilder.AppendLine("}");
layerBuilder.Append(maskBuilder);
layerBuilder.AppendLine("}");
return layerBuilder.ToString();
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: c12eb4948b575804b8eaeac6a32ec517