Unity Package Structure
This commit is contained in:
61
Runtime/FactoryBase.cs
Normal file
61
Runtime/FactoryBase.cs
Normal file
@@ -0,0 +1,61 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Syntriax.Modules.Factory
|
||||
{
|
||||
public abstract class FactoryBase : MonoBehaviour, IFactory
|
||||
{
|
||||
public bool IsInitialized { get; private set; } = false;
|
||||
|
||||
public Action<IFactory> OnInitialized { get; set; } = null;
|
||||
public Action<IFactory> OnReset { get; set; } = null;
|
||||
public Action<IFactory> OnStateChanged { get; set; } = null;
|
||||
|
||||
public void Initialize()
|
||||
{
|
||||
if (IsInitialized)
|
||||
return;
|
||||
|
||||
IsInitialized = true;
|
||||
|
||||
try
|
||||
{
|
||||
OnFactoryInitialize();
|
||||
|
||||
OnInitialized?.Invoke(this);
|
||||
OnStateChanged?.Invoke(this);
|
||||
}
|
||||
catch (System.Exception e)
|
||||
{
|
||||
Debug.LogError(e);
|
||||
Reset();
|
||||
}
|
||||
}
|
||||
|
||||
public void Reset()
|
||||
{
|
||||
if (!IsInitialized)
|
||||
return;
|
||||
|
||||
IsInitialized = false;
|
||||
|
||||
try
|
||||
{
|
||||
OnFactoryReset();
|
||||
|
||||
OnReset?.Invoke(this);
|
||||
OnStateChanged?.Invoke(this);
|
||||
}
|
||||
catch (System.Exception e)
|
||||
{
|
||||
Debug.LogError(e);
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Initialize"/>
|
||||
protected abstract void OnFactoryInitialize();
|
||||
|
||||
/// <inheritdoc cref="Reset"/>
|
||||
protected abstract void OnFactoryReset();
|
||||
}
|
||||
}
|
11
Runtime/FactoryBase.cs.meta
Normal file
11
Runtime/FactoryBase.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: f03dc034a54c734448dde62aa338c24e
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
25
Runtime/FactorySingleton.cs
Normal file
25
Runtime/FactorySingleton.cs
Normal file
@@ -0,0 +1,25 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace Syntriax.Modules.Factory
|
||||
{
|
||||
public abstract class FactorySingleton<T> : MonoBehaviour where T : FactoryBase
|
||||
{
|
||||
public const string ParentName = "Factories";
|
||||
private static T _instance = null;
|
||||
public static T Instance
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_instance == null)
|
||||
{
|
||||
GameObject factoriesGO = GameObject.Find(ParentName) ?? new GameObject(ParentName);
|
||||
_instance = new GameObject(typeof(T).Name).AddComponent<T>();
|
||||
_instance.transform.SetParent(factoriesGO.transform);
|
||||
DontDestroyOnLoad(factoriesGO);
|
||||
}
|
||||
|
||||
return _instance;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
11
Runtime/FactorySingleton.cs.meta
Normal file
11
Runtime/FactorySingleton.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 3541837e6dc3675418eaa6408d8c81cb
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
26
Runtime/IFactory.cs
Normal file
26
Runtime/IFactory.cs
Normal file
@@ -0,0 +1,26 @@
|
||||
using System;
|
||||
|
||||
namespace Syntriax.Modules.Factory
|
||||
{
|
||||
public interface IFactory
|
||||
{
|
||||
/// <summary>
|
||||
/// If the <see cref="IFactory"/> is initialized
|
||||
/// </summary>
|
||||
bool IsInitialized { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Initializes the <see cref="IFactory"/>
|
||||
/// </summary>
|
||||
void Initialize();
|
||||
|
||||
/// <summary>
|
||||
/// Resets the <see cref="IFactory"/>
|
||||
/// </summary>
|
||||
void Reset();
|
||||
|
||||
Action<IFactory> OnInitialized { get; set; }
|
||||
Action<IFactory> OnReset { get; set; }
|
||||
Action<IFactory> OnStateChanged { get; set; }
|
||||
}
|
||||
}
|
11
Runtime/IFactory.cs.meta
Normal file
11
Runtime/IFactory.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ae08bf6242ccc1941839a7c16f7037be
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
3
Runtime/Syntriax.Modules.Factory.asmdef
Normal file
3
Runtime/Syntriax.Modules.Factory.asmdef
Normal file
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"name": "Syntriax.Modules.Factory"
|
||||
}
|
7
Runtime/Syntriax.Modules.Factory.asmdef.meta
Normal file
7
Runtime/Syntriax.Modules.Factory.asmdef.meta
Normal file
@@ -0,0 +1,7 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d4c952ed5f59c5a449cda1b0080ed841
|
||||
AssemblyDefinitionImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
Reference in New Issue
Block a user