Unity Game Session MVP
This commit is contained in:
@@ -0,0 +1,89 @@
|
||||
using System;
|
||||
using System.Reflection;
|
||||
using UnityEngine;
|
||||
|
||||
namespace BrewMonster
|
||||
{
|
||||
public class AutoInitializer : MonoBehaviour
|
||||
{
|
||||
private void Awake()
|
||||
{
|
||||
Initialize();
|
||||
}
|
||||
|
||||
|
||||
private void Initialize()
|
||||
{
|
||||
var interfaceType = typeof(IAutoInitialize);
|
||||
|
||||
foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
|
||||
{
|
||||
Type[] typesInAssembly;
|
||||
try
|
||||
{
|
||||
typesInAssembly = assembly.GetTypes();
|
||||
}
|
||||
catch (ReflectionTypeLoadException e)
|
||||
{
|
||||
typesInAssembly = e.Types;
|
||||
}
|
||||
|
||||
if (typesInAssembly == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
foreach (var type in typesInAssembly)
|
||||
{
|
||||
if (type == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (type.IsAbstract || type.IsInterface)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!interfaceType.IsAssignableFrom(type))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
object instance = null;
|
||||
try
|
||||
{
|
||||
if (typeof(ScriptableObject).IsAssignableFrom(type))
|
||||
{
|
||||
instance = ScriptableObject.CreateInstance(type);
|
||||
}
|
||||
else if (typeof(MonoBehaviour).IsAssignableFrom(type))
|
||||
{
|
||||
var go = new GameObject(type.Name);
|
||||
go.transform.SetParent(transform, worldPositionStays: false);
|
||||
instance = go.AddComponent(type);
|
||||
}
|
||||
else
|
||||
{
|
||||
instance = Activator.CreateInstance(type);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Debug.LogError($"AutoInitializer: Failed to create instance of {type.FullName}: {ex}");
|
||||
continue;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
(instance as IAutoInitialize)?.Initialize();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Debug.LogError($"AutoInitializer: Failed to initialize {type.FullName}: {ex}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ddf01aaf66ec43f2bc9deda891a457d3
|
||||
timeCreated: 1757239452
|
||||
@@ -0,0 +1,9 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace BrewMonster
|
||||
{
|
||||
public class CoroutineRunner : MonoSingleton<CoroutineRunner>
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c8a86796c56d63447aa1df962fb3abf6
|
||||
@@ -0,0 +1,7 @@
|
||||
namespace BrewMonster
|
||||
{
|
||||
public interface IAutoInitialize
|
||||
{
|
||||
void Initialize();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 64cd9342d8974df18056b12aeb6f2651
|
||||
timeCreated: 1757239258
|
||||
@@ -0,0 +1,32 @@
|
||||
#define ENALBE_LOGGING
|
||||
using UnityEngine;
|
||||
|
||||
|
||||
namespace BrewMonster
|
||||
{
|
||||
public class Logger
|
||||
{
|
||||
public static void Log(string message)
|
||||
{
|
||||
#if ENALBE_LOGGING
|
||||
Debug.Log(message);
|
||||
#endif
|
||||
}
|
||||
|
||||
public static void LogError(string message)
|
||||
{
|
||||
#if ENALBE_LOGGING
|
||||
Debug.LogError(message);
|
||||
#endif
|
||||
}
|
||||
|
||||
public static void LogWarning(string message)
|
||||
{
|
||||
#if ENALBE_LOGGING
|
||||
Debug.LogWarning(message);
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 4e4b9f6cd855f4a4c9b02f681b12af2d
|
||||
@@ -0,0 +1,38 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace BrewMonster
|
||||
{
|
||||
public class MonoSingleton<T> : MonoBehaviour where T : MonoBehaviour
|
||||
{
|
||||
private static T _instance;
|
||||
public static T Instance
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_instance == null)
|
||||
{
|
||||
_instance = FindFirstObjectByType<T>();
|
||||
|
||||
if (_instance == null)
|
||||
{
|
||||
GameObject obj = new GameObject(typeof(T).Name);
|
||||
_instance = obj.AddComponent<T>();
|
||||
}
|
||||
}
|
||||
return _instance;
|
||||
}
|
||||
}
|
||||
|
||||
protected virtual void Awake()
|
||||
{
|
||||
_instance = this as T;
|
||||
Initialize();
|
||||
}
|
||||
|
||||
/// <summary>Override this method to initialize the singleton</summary>
|
||||
protected virtual void Initialize()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 139eb655406d7d447ba2d30309a84e00
|
||||
Reference in New Issue
Block a user