Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[REG-1639] New bot system #142

Merged
merged 13 commits into from
Mar 11, 2024
8 changes: 8 additions & 0 deletions src/gg.regression.unity.bots/Editor/Scripts/RGOverlay.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
@@ -0,0 +1,92 @@
using UnityEditor;
using UnityEditor.Callbacks;
using UnityEngine;
using System.Linq;
using System.Collections.Generic;

namespace RegressionGames.Editor
{
[InitializeOnLoad]
public class IRGBotFinder : UnityEditor.Editor
{
static IRGBotFinder()
{
// Subscribe to the play mode state changed event
EditorApplication.playModeStateChanged -= OnPlayModeStateChanged;
EditorApplication.playModeStateChanged += OnPlayModeStateChanged;
}

private static void OnPlayModeStateChanged(PlayModeStateChange state)
{
// Check if Unity has entered play mode
if (state == PlayModeStateChange.EnteredPlayMode)
{
OnScriptsReloaded();
}
}

[DidReloadScripts]
private static void OnScriptsReloaded()
{
// Find the IRGBotList ScriptableObject in your project
IRGBotList botList = LoadBotList();

if (botList == null)
{
return;
}

// Use TypeCache to find all types that implement IRGBot
var botTypes = TypeCache.GetTypesDerivedFrom<IRGBot>()
.Where(t => typeof(MonoBehaviour).IsAssignableFrom(t) && !t.IsAbstract);

List<IRGBotEntry> entries = new List<IRGBotEntry>();

foreach (var type in botTypes)
{
var qualifiedName = type.AssemblyQualifiedName;
if (!string.IsNullOrEmpty(qualifiedName))
{
entries.Add(new IRGBotEntry
{
botName = type.Name,
qualifiedName = qualifiedName,
});
}
}

botList.botEntries = entries.ToArray();

// Mark the ScriptableObject as dirty to ensure it saves
EditorUtility.SetDirty(botList);
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
}

private static IRGBotList LoadBotList()
{
string packagePath = "Packages/gg.regression.unity.bots/Runtime/Resources/RGBotList.asset";
IRGBotList botList = AssetDatabase.LoadAssetAtPath<IRGBotList>(packagePath);

// Ensure the target SO directory exists
string targetDirPath = "Assets/RegressionGames/Resources";
RGEditorUtils.CreateAllAssetFolders(targetDirPath);

// Define the path for the new asset
string targetAssetPath = $"{targetDirPath}/RGBots.asset";

// Check if the asset already exists to avoid overwriting it
if (AssetDatabase.LoadAssetAtPath<IRGBotList>(targetAssetPath) == null)
{
// Copy the asset
AssetDatabase.CopyAsset(packagePath, targetAssetPath);
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
}

botList = AssetDatabase.LoadAssetAtPath<IRGBotList>(targetAssetPath);
AssetDatabase.Refresh();
return botList;
}
}
}

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

4 changes: 2 additions & 2 deletions src/gg.regression.unity.bots/Runtime/Fonts/Regression.asset
Git LFS file not shown
Loading
Loading