feat: initial commit
This commit is contained in:
61
Assets/Common/Infrastructure/.gitignore
vendored
Normal file
61
Assets/Common/Infrastructure/.gitignore
vendored
Normal file
@@ -0,0 +1,61 @@
|
||||
# This .gitignore file should be placed at the root of your Unity project directory
|
||||
#
|
||||
# Get latest from https://github.com/github/gitignore/blob/master/Unity.gitignore
|
||||
#
|
||||
/[Ll]ibrary/
|
||||
/[Tt]emp/
|
||||
/[Oo]bj/
|
||||
/[Bb]uild/
|
||||
/[Bb]uilds/
|
||||
/[Ll]ogs/
|
||||
/[Mm]emoryCaptures/
|
||||
|
||||
# Asset meta data should only be ignored when the corresponding asset is also ignored
|
||||
!/[Aa]ssets/**/*.meta
|
||||
|
||||
# Uncomment this line if you wish to ignore the asset store tools plugin
|
||||
# /[Aa]ssets/AssetStoreTools*
|
||||
|
||||
# Autogenerated Jetbrains Rider plugin
|
||||
[Aa]ssets/Plugins/Editor/JetBrains*
|
||||
|
||||
# Visual Studio cache directory
|
||||
.vs/
|
||||
.config
|
||||
|
||||
# Gradle cache directory
|
||||
.gradle/
|
||||
|
||||
# Autogenerated VS/MD/Consulo solution and project files
|
||||
ExportedObj/
|
||||
.consulo/
|
||||
*.csproj
|
||||
*.unityproj
|
||||
*.sln
|
||||
*.suo
|
||||
*.tmp
|
||||
*.user
|
||||
*.userprefs
|
||||
*.pidb
|
||||
*.booproj
|
||||
*.svd
|
||||
*.pdb
|
||||
*.mdb
|
||||
*.opendb
|
||||
*.VC.db
|
||||
|
||||
# Unity3D generated meta files
|
||||
*.pidb.meta
|
||||
*.pdb.meta
|
||||
*.mdb.meta
|
||||
|
||||
# Unity3D generated file on crash reports
|
||||
sysinfo.txt
|
||||
|
||||
# Builds
|
||||
*.apk
|
||||
*.unitypackage
|
||||
|
||||
# Crashlytics generated file
|
||||
crashlytics-build.properties
|
||||
|
||||
12
Assets/Common/Infrastructure/CHANGELOG.md
Normal file
12
Assets/Common/Infrastructure/CHANGELOG.md
Normal file
@@ -0,0 +1,12 @@
|
||||
# Changelog
|
||||
|
||||
All notable changes to this project will be documented in this file.
|
||||
|
||||
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
|
||||
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
||||
|
||||
## [1.0.0] - 20XX-XX-XX
|
||||
|
||||
### Added
|
||||
|
||||
- Initial release of the Unity package.
|
||||
3
Assets/Common/Infrastructure/CHANGELOG.md.meta
Normal file
3
Assets/Common/Infrastructure/CHANGELOG.md.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c45fd6fc3f2b416ca5f97e7d1603e504
|
||||
timeCreated: 1763912104
|
||||
21
Assets/Common/Infrastructure/LICENSE.md
Normal file
21
Assets/Common/Infrastructure/LICENSE.md
Normal file
@@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2025 Joe Siu
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
3
Assets/Common/Infrastructure/LICENSE.md.meta
Normal file
3
Assets/Common/Infrastructure/LICENSE.md.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: af50d1333b664215ba84f17d6ce050d3
|
||||
timeCreated: 1763912104
|
||||
8
Assets/Common/Infrastructure/README.md
Normal file
8
Assets/Common/Infrastructure/README.md
Normal file
@@ -0,0 +1,8 @@
|
||||
# com.joesiu.common.infrastructure
|
||||
|
||||
## Installation
|
||||
|
||||
1. Open `Package Manager`
|
||||
2. Click `+`
|
||||
3. Select `Add package from git URL...`
|
||||
4. Paste `https://github.com/JoeSiu/UnityPackages.git?path=Assets/Common/Infrastructure`
|
||||
3
Assets/Common/Infrastructure/README.md.meta
Normal file
3
Assets/Common/Infrastructure/README.md.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c83dd7f87dd941cc9eaac8240b529a90
|
||||
timeCreated: 1763912104
|
||||
3
Assets/Common/Infrastructure/Runtime.meta
Normal file
3
Assets/Common/Infrastructure/Runtime.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: bd1d08d357a7453ea2c6973bf965d749
|
||||
timeCreated: 1763912104
|
||||
12
Assets/Common/Infrastructure/Runtime/DontDestroyOnLoad.cs
Normal file
12
Assets/Common/Infrastructure/Runtime/DontDestroyOnLoad.cs
Normal file
@@ -0,0 +1,12 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace JoeSiu.Common.Infrastructure.Runtime
|
||||
{
|
||||
public class DontDestroyOnLoad : MonoBehaviour
|
||||
{
|
||||
private void Awake()
|
||||
{
|
||||
DontDestroyOnLoad(gameObject);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e6bbe0c45eed41ba9edaeb03c27d1af7
|
||||
timeCreated: 1763915394
|
||||
@@ -0,0 +1,14 @@
|
||||
{
|
||||
"name": "Common.Infrastructure.Runtime",
|
||||
"rootNamespace": "JoeSiu",
|
||||
"references": [],
|
||||
"includePlatforms": [],
|
||||
"excludePlatforms": [],
|
||||
"allowUnsafeCode": false,
|
||||
"overrideReferences": false,
|
||||
"precompiledReferences": [],
|
||||
"autoReferenced": true,
|
||||
"defineConstraints": [],
|
||||
"versionDefines": [],
|
||||
"noEngineReferences": false
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 70e478b7eb804446bc79813af716d369
|
||||
timeCreated: 1763912104
|
||||
87
Assets/Common/Infrastructure/Runtime/MonoSingleton.cs
Normal file
87
Assets/Common/Infrastructure/Runtime/MonoSingleton.cs
Normal file
@@ -0,0 +1,87 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace JoeSiu.Common.Infrastructure.Runtime
|
||||
{
|
||||
public abstract class MonoSingleton<T> : MonoBehaviour where T : MonoSingleton<T>
|
||||
{
|
||||
private static T _instance;
|
||||
public static T Instance
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_instance) return _instance;
|
||||
|
||||
var objs = FindObjectsByType<T>(FindObjectsSortMode.None);
|
||||
|
||||
if (objs.Length > 0)
|
||||
{
|
||||
AssignInstance(objs[0]);
|
||||
}
|
||||
|
||||
if (objs.Length > 1)
|
||||
{
|
||||
for (var index = 1; index < objs.Length; index++)
|
||||
{
|
||||
var obj = objs[index];
|
||||
DestroyDuplicate(obj);
|
||||
}
|
||||
}
|
||||
|
||||
if (!_instance)
|
||||
{
|
||||
Debug.LogWarning(
|
||||
$"[MonoSingleton] No instance of type '{typeof(T).Name}' is found, make sure to add it in the scene!");
|
||||
}
|
||||
|
||||
return _instance;
|
||||
}
|
||||
}
|
||||
|
||||
public static T GetInstance() => _instance;
|
||||
|
||||
public static void CreateInstance(bool dontDestroyOnLoad = true)
|
||||
{
|
||||
var go = new GameObject(typeof(T).Name);
|
||||
var instance = go.AddComponent<T>();
|
||||
if (dontDestroyOnLoad) DontDestroyOnLoad(go);
|
||||
Debug.Log(
|
||||
$"[MonoSingleton] Created game object '{go.name}' {(dontDestroyOnLoad ? "(DontDestroyOnLoad)" : "")} for type '{typeof(T).Name}'",
|
||||
go);
|
||||
AssignInstance(instance);
|
||||
}
|
||||
|
||||
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
|
||||
public static void RemoveInstance()
|
||||
{
|
||||
_instance = null;
|
||||
Debug.Log($"[MonoSingleton] Removed instance of type '{typeof(T).Name}'");
|
||||
}
|
||||
|
||||
protected virtual void Awake()
|
||||
{
|
||||
if (!_instance) AssignInstance(this as T);
|
||||
else if (_instance != this) DestroyDuplicate(this as T);
|
||||
}
|
||||
|
||||
private static void AssignInstance(T instance)
|
||||
{
|
||||
if (!instance) return;
|
||||
_instance = instance;
|
||||
Debug.Log($"[MonoSingleton] Assigned instance of type '{typeof(T).Name}' to object '{instance.gameObject.name}'",
|
||||
instance.gameObject);
|
||||
}
|
||||
|
||||
private static void DestroyDuplicate(T instance)
|
||||
{
|
||||
if (!_instance) return;
|
||||
if (!instance) return;
|
||||
|
||||
Debug.LogError(
|
||||
$"[MonoSingleton] Instance of type '{typeof(T).Name}' is already assigned to object '{_instance.gameObject.name}', destroying duplicate instance '{instance.gameObject.name}'...",
|
||||
instance.gameObject);
|
||||
|
||||
if (Application.isPlaying) Destroy(instance);
|
||||
else DestroyImmediate(instance);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 7fdcac9d0a5a415c8fb702b32809e649
|
||||
timeCreated: 1763913277
|
||||
@@ -0,0 +1,57 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace JoeSiu.Common.Infrastructure.Runtime
|
||||
{
|
||||
/// <summary>
|
||||
/// <see href="https://youtu.be/6kWUGEQiMUI"/>
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
public abstract class ScriptableObjectSingleton<T> : ScriptableObject where T : ScriptableObjectSingleton<T>
|
||||
{
|
||||
private static T _instance;
|
||||
public static T Instance
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_instance != null) return _instance;
|
||||
|
||||
var assets = Resources.LoadAll<T>("");
|
||||
|
||||
if (assets.Length > 0)
|
||||
{
|
||||
AssignInstance(assets[0]);
|
||||
}
|
||||
|
||||
if (assets.Length > 1)
|
||||
{
|
||||
Debug.LogError(
|
||||
$"[ScriptableObjectSingleton] More than 1 instance of Singleton Scriptable Object of type: {typeof(T)} found");
|
||||
}
|
||||
|
||||
if (!_instance)
|
||||
{
|
||||
Debug.LogWarning(
|
||||
$"[ScriptableObjectSingleton] No instance of type '{typeof(T).Name}' is found, make sure to add it to the Resources folder!");
|
||||
}
|
||||
|
||||
return _instance;
|
||||
}
|
||||
}
|
||||
|
||||
public static T GetInstance() => _instance;
|
||||
|
||||
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
|
||||
public static void RemoveInstance()
|
||||
{
|
||||
_instance = null;
|
||||
Debug.Log($"[ScriptableObjectSingleton] Removed instance of type '{typeof(T).Name}'");
|
||||
}
|
||||
|
||||
private static void AssignInstance(T instance)
|
||||
{
|
||||
if (!instance) return;
|
||||
_instance = instance;
|
||||
Debug.Log($"[ScriptableObjectSingleton] Assigned instance of type '{typeof(T).Name}' to object '{instance.name}'", instance);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e2616e8d08c645498cb1486552014786
|
||||
timeCreated: 1763915451
|
||||
16
Assets/Common/Infrastructure/package.json
Normal file
16
Assets/Common/Infrastructure/package.json
Normal file
@@ -0,0 +1,16 @@
|
||||
{
|
||||
"name": "com.joesiu.common.infrastructure",
|
||||
"version": "1.0.0",
|
||||
"description": "Common scripts for core infrastructure.",
|
||||
"displayName": "Infrastructure",
|
||||
"unity": "2019.4",
|
||||
"author": {
|
||||
"name": "Joe Siu",
|
||||
"url": "https://github.com/JoeSiu"
|
||||
},
|
||||
"changelogUrl": "https://github.com/JoeSiu/UnityPackages/blob/main/CHANGELOG.md",
|
||||
"dependencies": {},
|
||||
"documentationUrl": "https://github.com/JoeSiu/UnityPackages/",
|
||||
"keywords": [],
|
||||
"licensesUrl": "https://github.com/JoeSiu/UnityPackages/blob/main/LICENSE.md"
|
||||
}
|
||||
3
Assets/Common/Infrastructure/package.json.meta
Normal file
3
Assets/Common/Infrastructure/package.json.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c0c0741ec91f4e83b31c3346f9ba5e6a
|
||||
timeCreated: 1763912104
|
||||
Reference in New Issue
Block a user