Unity Package Structure

This commit is contained in:
2022-12-15 19:21:56 +03:00
parent c266d7bf2e
commit 06a61e093f
11 changed files with 12 additions and 4 deletions

61
Runtime/FactoryBase.cs Normal file
View 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();
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: f03dc034a54c734448dde62aa338c24e
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View 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;
}
}
}
}

View 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
View 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
View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: ae08bf6242ccc1941839a7c16f7037be
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,3 @@
{
"name": "Syntriax.Modules.Factory"
}

View File

@@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: d4c952ed5f59c5a449cda1b0080ed841
AssemblyDefinitionImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant: