feat: initial commit
This commit is contained in:
3
Assets/Common/Infrastructure.meta
Normal file
3
Assets/Common/Infrastructure.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: f44b2cdebe2740b68f5b0015217c7c8d
|
||||
timeCreated: 1763912104
|
||||
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
|
||||
8
Assets/Common/Serialization.meta
Normal file
8
Assets/Common/Serialization.meta
Normal file
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b29cafa929b60184fa189b4cb045828b
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
61
Assets/Common/Serialization/.gitignore
vendored
Normal file
61
Assets/Common/Serialization/.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/Serialization/CHANGELOG.md
Normal file
12
Assets/Common/Serialization/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] - 2025-11-24
|
||||
|
||||
### Added
|
||||
|
||||
- Initial release of the Unity package.
|
||||
7
Assets/Common/Serialization/CHANGELOG.md.meta
Normal file
7
Assets/Common/Serialization/CHANGELOG.md.meta
Normal file
@@ -0,0 +1,7 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 6aab4fa9da7c1124a96e9c0838a1b12e
|
||||
TextScriptImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
21
Assets/Common/Serialization/LICENSE.md
Normal file
21
Assets/Common/Serialization/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.
|
||||
7
Assets/Common/Serialization/LICENSE.md.meta
Normal file
7
Assets/Common/Serialization/LICENSE.md.meta
Normal file
@@ -0,0 +1,7 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 6333dc7bfd551ae4c82c1586a89dbc41
|
||||
TextScriptImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
8
Assets/Common/Serialization/README.md
Normal file
8
Assets/Common/Serialization/README.md
Normal file
@@ -0,0 +1,8 @@
|
||||
# com.joesiu.common.serialization
|
||||
|
||||
## 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/Serialization`
|
||||
7
Assets/Common/Serialization/README.md.meta
Normal file
7
Assets/Common/Serialization/README.md.meta
Normal file
@@ -0,0 +1,7 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 9396f0bf3b325ff48ba7029d107c30fa
|
||||
TextScriptImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
8
Assets/Common/Serialization/Runtime.meta
Normal file
8
Assets/Common/Serialization/Runtime.meta
Normal file
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 98bbeb42106339546b8f985f042e2a8f
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,14 @@
|
||||
{
|
||||
"name": "Common.Serialization.Runtime",
|
||||
"rootNamespace": "JoeSiu",
|
||||
"references": [],
|
||||
"includePlatforms": [],
|
||||
"excludePlatforms": [],
|
||||
"allowUnsafeCode": false,
|
||||
"overrideReferences": false,
|
||||
"precompiledReferences": [],
|
||||
"autoReferenced": true,
|
||||
"defineConstraints": [],
|
||||
"versionDefines": [],
|
||||
"noEngineReferences": false
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d7f6647327e7fa54faec2c8e2a49621d
|
||||
AssemblyDefinitionImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
138
Assets/Common/Serialization/Runtime/SerializableInterface.cs
Normal file
138
Assets/Common/Serialization/Runtime/SerializableInterface.cs
Normal file
@@ -0,0 +1,138 @@
|
||||
#region License and Information
|
||||
|
||||
/*****
|
||||
* Provides a serializable wrapper that allows you to store a reference to
|
||||
* a serialized UnityEngine.Object that implement a certain interface.
|
||||
* The "Instance" property provides direct easy access to the serialized reference.
|
||||
* This should work with Components as well as ScriptableObjects.
|
||||
* It ships with a convenient property drawer that should streamline the usage.
|
||||
*
|
||||
* Copyright (c) 2023 Bunny83
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
*****/
|
||||
|
||||
#endregion License and Information
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
using Object = UnityEngine.Object;
|
||||
|
||||
namespace JoeSiu.Common.Serialization.Runtime
|
||||
{
|
||||
[Serializable]
|
||||
public class SerializableInterface<T> where T : class
|
||||
{
|
||||
[SerializeField]
|
||||
private Object _obj;
|
||||
private T _value;
|
||||
|
||||
public SerializableInterface()
|
||||
{
|
||||
}
|
||||
|
||||
public SerializableInterface(T value)
|
||||
{
|
||||
SetValue(value);
|
||||
}
|
||||
|
||||
public T Value
|
||||
{
|
||||
get => GetValue();
|
||||
set => SetValue(value);
|
||||
}
|
||||
|
||||
public T GetValue()
|
||||
{
|
||||
if (_value != null && (object)_value == _obj) return _value;
|
||||
if (_obj == null) SetValue(null);
|
||||
else if (_obj is T inst || _obj is GameObject go && go.TryGetComponent(out inst)) _value = inst;
|
||||
else SetValue(null);
|
||||
|
||||
return _value;
|
||||
}
|
||||
|
||||
private void SetValue(T value)
|
||||
{
|
||||
_value = value;
|
||||
_obj = _value as Object;
|
||||
}
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
[CustomPropertyDrawer(typeof(SerializableInterface<>), true)]
|
||||
public class SerializableInterfacePropertyDrawer : PropertyDrawer
|
||||
{
|
||||
private Type _genericType;
|
||||
private readonly List<Component> _list = new();
|
||||
|
||||
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
|
||||
{
|
||||
if (_genericType == null)
|
||||
{
|
||||
var fieldType = fieldInfo.FieldType;
|
||||
if (fieldType.IsGenericType && fieldType.GetGenericTypeDefinition() == typeof(List<>))
|
||||
// when used in a List<>, grab the actual type from the generic argument of the List
|
||||
fieldType = fieldInfo.FieldType.GetGenericArguments()[0];
|
||||
else if (fieldType.IsArray)
|
||||
// when used in an array, grab the actual type from the element type.
|
||||
fieldType = fieldType.GetElementType();
|
||||
|
||||
var types = fieldType?.GetGenericArguments();
|
||||
if (types is { Length: 1 }) _genericType = types[0];
|
||||
}
|
||||
|
||||
var obj = property.FindPropertyRelative("_obj");
|
||||
EditorGUI.BeginChangeCheck();
|
||||
var newObj = EditorGUI.ObjectField(position, label, obj.objectReferenceValue, typeof(Object), true);
|
||||
if (!EditorGUI.EndChangeCheck()) return;
|
||||
if (newObj == null) obj.objectReferenceValue = null;
|
||||
else if (_genericType != null && _genericType.IsAssignableFrom(newObj.GetType())) obj.objectReferenceValue = newObj;
|
||||
else if (newObj is GameObject go)
|
||||
{
|
||||
_list.Clear();
|
||||
go.GetComponents(_genericType, _list);
|
||||
if (_list.Count == 1)
|
||||
{
|
||||
obj.objectReferenceValue = _list[0];
|
||||
}
|
||||
else
|
||||
{
|
||||
var m = new GenericMenu();
|
||||
var n = 1;
|
||||
foreach (var item in _list)
|
||||
m.AddItem(new GUIContent(n++ + " " + item.GetType().Name), false, a =>
|
||||
{
|
||||
obj.objectReferenceValue = (Object)a;
|
||||
obj.serializedObject.ApplyModifiedProperties();
|
||||
}, item);
|
||||
m.ShowAsContext();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogWarning("Dragged object is not compatible with " + _genericType?.Name);
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c8df361f2765f214eba36fc0fb21a063
|
||||
16
Assets/Common/Serialization/package.json
Normal file
16
Assets/Common/Serialization/package.json
Normal file
@@ -0,0 +1,16 @@
|
||||
{
|
||||
"name": "com.joesiu.common.serialization",
|
||||
"version": "1.0.0",
|
||||
"description": "Common scripts for serialization.",
|
||||
"displayName": "Serialization",
|
||||
"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"
|
||||
}
|
||||
7
Assets/Common/Serialization/package.json.meta
Normal file
7
Assets/Common/Serialization/package.json.meta
Normal file
@@ -0,0 +1,7 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 10f90d0d9e83df748a3118d0fd3a1560
|
||||
TextScriptImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
8
Assets/Common/Timer.meta
Normal file
8
Assets/Common/Timer.meta
Normal file
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 9337a80e883c06b43bb8045f50ce8fc8
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
61
Assets/Common/Timer/.gitignore
vendored
Normal file
61
Assets/Common/Timer/.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/Timer/CHANGELOG.md
Normal file
12
Assets/Common/Timer/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] - 2025-11-24
|
||||
|
||||
### Added
|
||||
|
||||
- Initial release of the Unity package.
|
||||
7
Assets/Common/Timer/CHANGELOG.md.meta
Normal file
7
Assets/Common/Timer/CHANGELOG.md.meta
Normal file
@@ -0,0 +1,7 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b59e8b83170e79e49bb382cbd8bb2e47
|
||||
TextScriptImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
0
Assets/Common/Timer/Documentation~/common.timer.md
Normal file
0
Assets/Common/Timer/Documentation~/common.timer.md
Normal file
21
Assets/Common/Timer/LICENSE.md
Normal file
21
Assets/Common/Timer/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.
|
||||
7
Assets/Common/Timer/LICENSE.md.meta
Normal file
7
Assets/Common/Timer/LICENSE.md.meta
Normal file
@@ -0,0 +1,7 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 26de4860f9f29e146a42655c17acb7a8
|
||||
TextScriptImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
8
Assets/Common/Timer/README.md
Normal file
8
Assets/Common/Timer/README.md
Normal file
@@ -0,0 +1,8 @@
|
||||
# com.joesiu.common.timer
|
||||
|
||||
## 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/Timer`
|
||||
7
Assets/Common/Timer/README.md.meta
Normal file
7
Assets/Common/Timer/README.md.meta
Normal file
@@ -0,0 +1,7 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 5ba592ce62241954c91a14eb83c32366
|
||||
TextScriptImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
8
Assets/Common/Timer/Runtime.meta
Normal file
8
Assets/Common/Timer/Runtime.meta
Normal file
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 2fab66aac1f04504db2f990f40887a73
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
20
Assets/Common/Timer/Runtime/IStopwatch.cs
Normal file
20
Assets/Common/Timer/Runtime/IStopwatch.cs
Normal file
@@ -0,0 +1,20 @@
|
||||
using System;
|
||||
|
||||
namespace Common.Timer.Runtime
|
||||
{
|
||||
public interface IStopwatch : ITimer
|
||||
{
|
||||
public float Duration { get; set; }
|
||||
public bool DeactivateOnTimeReached { get; set; }
|
||||
public float RemainingTime { get; set; }
|
||||
public float Progress { get; set; }
|
||||
public bool IsTimeReached => CurrentTime >= Duration;
|
||||
|
||||
public event Action OnTimeReached;
|
||||
|
||||
public void SetDuration(float value);
|
||||
public void SetRemainingTime(float value);
|
||||
public void SetProgress(float value);
|
||||
public void EndTime();
|
||||
}
|
||||
}
|
||||
2
Assets/Common/Timer/Runtime/IStopwatch.cs.meta
Normal file
2
Assets/Common/Timer/Runtime/IStopwatch.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 7d400d4cccd55e94088df52dfd4cbb96
|
||||
23
Assets/Common/Timer/Runtime/ITimer.cs
Normal file
23
Assets/Common/Timer/Runtime/ITimer.cs
Normal file
@@ -0,0 +1,23 @@
|
||||
using System;
|
||||
|
||||
namespace Common.Timer.Runtime
|
||||
{
|
||||
public interface ITimer
|
||||
{
|
||||
public bool IsActive { get; }
|
||||
public float CurrentTime { get; set; }
|
||||
public float DeltaTime { get; }
|
||||
|
||||
public void Tick();
|
||||
public void Activate(bool resetTime = false);
|
||||
public void Deactivate();
|
||||
public void ResetTime();
|
||||
|
||||
public void SetTime(float value);
|
||||
|
||||
public event Action OnTick;
|
||||
public event Action OnActivate;
|
||||
public event Action OnDeactivate;
|
||||
public event Action OnTimeReset;
|
||||
}
|
||||
}
|
||||
2
Assets/Common/Timer/Runtime/ITimer.cs.meta
Normal file
2
Assets/Common/Timer/Runtime/ITimer.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 166ad9d07eff7ce409885714f6cddd55
|
||||
20
Assets/Common/Timer/Runtime/JoeSiu.Common.Timer.asmdef
Normal file
20
Assets/Common/Timer/Runtime/JoeSiu.Common.Timer.asmdef
Normal file
@@ -0,0 +1,20 @@
|
||||
{
|
||||
"name": "Common.Timer.Runtime",
|
||||
"rootNamespace": "JoeSiu",
|
||||
"references": [
|
||||
"GUID:75469ad4d38634e559750d17036d5f7c",
|
||||
"GUID:6055be8ebefd69e48b49212b09b47b2f",
|
||||
"GUID:324caed91501a9c47a04ebfd87b68794",
|
||||
"GUID:d7f6647327e7fa54faec2c8e2a49621d",
|
||||
"GUID:70e478b7eb804446bc79813af716d369"
|
||||
],
|
||||
"includePlatforms": [],
|
||||
"excludePlatforms": [],
|
||||
"allowUnsafeCode": false,
|
||||
"overrideReferences": false,
|
||||
"precompiledReferences": [],
|
||||
"autoReferenced": true,
|
||||
"defineConstraints": [],
|
||||
"versionDefines": [],
|
||||
"noEngineReferences": false
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 4867374f8b89f5b4587d9170bed9e856
|
||||
AssemblyDefinitionImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
104
Assets/Common/Timer/Runtime/MonoStopwatch.cs
Normal file
104
Assets/Common/Timer/Runtime/MonoStopwatch.cs
Normal file
@@ -0,0 +1,104 @@
|
||||
using System;
|
||||
using TriInspector;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Common.Timer.Runtime
|
||||
{
|
||||
public class MonoStopwatch : MonoTimer, IStopwatch
|
||||
{
|
||||
public override float CurrentTime
|
||||
{
|
||||
get
|
||||
{
|
||||
currentTime = Mathf.Clamp(currentTime, 0f, Duration);
|
||||
return currentTime;
|
||||
}
|
||||
set => currentTime = Mathf.Clamp(value, 0f, Duration);
|
||||
}
|
||||
|
||||
[Title("Settings (Stopwatch)")]
|
||||
[SerializeField]
|
||||
protected float duration;
|
||||
[Group("Internal"), ShowInInspector, DisableInEditMode]
|
||||
public virtual float Duration
|
||||
{
|
||||
get
|
||||
{
|
||||
duration = Mathf.Max(0.001f, duration);
|
||||
return duration;
|
||||
}
|
||||
set => duration = Mathf.Max(0.001f, value);
|
||||
}
|
||||
[SerializeField]
|
||||
protected bool deactivateOnTimeReached;
|
||||
public bool DeactivateOnTimeReached
|
||||
{
|
||||
get => deactivateOnTimeReached;
|
||||
set => deactivateOnTimeReached = value;
|
||||
}
|
||||
|
||||
[Group("Internal"), ShowInInspector, DisableInEditMode]
|
||||
public virtual float RemainingTime
|
||||
{
|
||||
get => Mathf.Clamp(Duration - CurrentTime, 0f, Duration);
|
||||
set => CurrentTime = Mathf.Clamp(Duration - value, 0f, Duration);
|
||||
}
|
||||
[Group("Internal"), ShowInInspector, DisableInEditMode]
|
||||
public virtual float Progress
|
||||
{
|
||||
get => Duration == 0f ? 0f : Mathf.Clamp(CurrentTime / Duration, 0f, 1f);
|
||||
set => CurrentTime = Mathf.Clamp(Duration * value, 0f, Duration);
|
||||
}
|
||||
[Group("Internal"), ReadOnly, ShowInInspector, DisableInEditMode]
|
||||
public virtual bool IsTimeReached => CurrentTime >= Duration;
|
||||
|
||||
public event Action OnTimeReached;
|
||||
|
||||
public override void Tick()
|
||||
{
|
||||
if (IsTimeReached) return;
|
||||
|
||||
base.Tick();
|
||||
if (!IsActive) return;
|
||||
|
||||
if (CurrentTime >= Duration) EndTime();
|
||||
}
|
||||
|
||||
public virtual void SetDuration(float value)
|
||||
{
|
||||
Duration = value;
|
||||
if (IsTimeReached) EndTime();
|
||||
}
|
||||
|
||||
public virtual void SetRemainingTime(float value)
|
||||
{
|
||||
RemainingTime = value;
|
||||
if (IsTimeReached) EndTime();
|
||||
}
|
||||
|
||||
public virtual void SetProgress(float value)
|
||||
{
|
||||
Progress = value;
|
||||
if (IsTimeReached) EndTime();
|
||||
}
|
||||
|
||||
public virtual void EndTime()
|
||||
{
|
||||
currentTime = Duration;
|
||||
InvokeOnTimeReached();
|
||||
if (DeactivateOnTimeReached) Deactivate();
|
||||
}
|
||||
|
||||
protected virtual void InvokeOnTimeReached()
|
||||
{
|
||||
OnTimeReached?.Invoke();
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
|
||||
[Group("Debug"), Button("End Time"), HideInEditMode]
|
||||
private void EndTimer_Editor() => EndTime();
|
||||
|
||||
#endif
|
||||
}
|
||||
}
|
||||
2
Assets/Common/Timer/Runtime/MonoStopwatch.cs.meta
Normal file
2
Assets/Common/Timer/Runtime/MonoStopwatch.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 7152de60fabb33c41aebca0a58b2096b
|
||||
109
Assets/Common/Timer/Runtime/MonoTimer.cs
Normal file
109
Assets/Common/Timer/Runtime/MonoTimer.cs
Normal file
@@ -0,0 +1,109 @@
|
||||
using System;
|
||||
using TriInspector;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Common.Timer.Runtime
|
||||
{
|
||||
[DeclareBoxGroup("Debug")]
|
||||
[DeclareBoxGroup("Internal")]
|
||||
// TODO: Create a MonoTimerBase similar to TimerBaseSO instead
|
||||
public class MonoTimer : MonoBehaviour, ITimer
|
||||
{
|
||||
[Title("Settings")]
|
||||
[SerializeField]
|
||||
protected bool isActive;
|
||||
public virtual bool IsActive
|
||||
{
|
||||
get => isActive;
|
||||
protected set => isActive = value;
|
||||
}
|
||||
|
||||
protected float currentTime;
|
||||
[Group("Internal"), ShowInInspector, DisableInEditMode]
|
||||
public virtual float CurrentTime
|
||||
{
|
||||
get
|
||||
{
|
||||
currentTime = Mathf.Max(currentTime, 0f);
|
||||
return currentTime;
|
||||
}
|
||||
set => currentTime = Mathf.Max(value, 0f);
|
||||
}
|
||||
[Group("Internal"), ShowInInspector, DisableInEditMode]
|
||||
public virtual float DeltaTime => Time.deltaTime;
|
||||
|
||||
public event Action OnTick;
|
||||
public event Action OnActivate;
|
||||
public event Action OnDeactivate;
|
||||
public event Action OnTimeReset;
|
||||
|
||||
protected virtual void Update()
|
||||
{
|
||||
Tick();
|
||||
}
|
||||
|
||||
#region Public Methods
|
||||
|
||||
public virtual void Tick()
|
||||
{
|
||||
if (!IsActive) return;
|
||||
|
||||
CurrentTime += DeltaTime;
|
||||
InvokeOnTick();
|
||||
}
|
||||
|
||||
public virtual void Activate(bool resetTime = false)
|
||||
{
|
||||
IsActive = true;
|
||||
InvokeOnActivate();
|
||||
if (resetTime) ResetTime();
|
||||
}
|
||||
|
||||
public virtual void Deactivate()
|
||||
{
|
||||
IsActive = false;
|
||||
InvokeOnDeactivate();
|
||||
}
|
||||
|
||||
public virtual void ResetTime()
|
||||
{
|
||||
CurrentTime = 0f;
|
||||
InvokeOnTimeReset();
|
||||
}
|
||||
|
||||
public virtual void SetTime(float value)
|
||||
{
|
||||
CurrentTime = value;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Protected Methods
|
||||
|
||||
protected virtual void InvokeOnTick() => OnTick?.Invoke();
|
||||
protected virtual void InvokeOnActivate() => OnActivate?.Invoke();
|
||||
protected virtual void InvokeOnDeactivate() => OnDeactivate?.Invoke();
|
||||
protected virtual void InvokeOnTimeReset() => OnTimeReset?.Invoke();
|
||||
|
||||
#endregion
|
||||
|
||||
#if UNITY_EDITOR
|
||||
|
||||
[Group("Debug"), Button("Activate"), HideInEditMode]
|
||||
private void Activate_Editor() => Activate(false);
|
||||
|
||||
[Group("Debug"), Button("Activate (Reset Time)"), HideInEditMode]
|
||||
private void ActivateResetTime_Editor() => Activate(true);
|
||||
|
||||
[Group("Debug"), Button("Deactivate"), HideInEditMode]
|
||||
private void Deactivate_Editor() => Deactivate();
|
||||
|
||||
[Group("Debug"), Button("Tick"), HideInEditMode]
|
||||
private void Tick_Editor() => Tick();
|
||||
|
||||
[Group("Debug"), Button("Reset Time"), HideInEditMode]
|
||||
private void ResetTime_Editor() => ResetTime();
|
||||
|
||||
#endif
|
||||
}
|
||||
}
|
||||
2
Assets/Common/Timer/Runtime/MonoTimer.cs.meta
Normal file
2
Assets/Common/Timer/Runtime/MonoTimer.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 34fac720fb4287843aea355b0594eda1
|
||||
8
Assets/Common/Timer/Runtime/ScriptableObjects.meta
Normal file
8
Assets/Common/Timer/Runtime/ScriptableObjects.meta
Normal file
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 84762aa781ebe084290160322bc621e3
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
62
Assets/Common/Timer/Runtime/ScriptableObjects/StopwatchSO.cs
Normal file
62
Assets/Common/Timer/Runtime/ScriptableObjects/StopwatchSO.cs
Normal file
@@ -0,0 +1,62 @@
|
||||
using System;
|
||||
using TriInspector;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Common.Timer.Runtime.ScriptableObjects
|
||||
{
|
||||
[CreateAssetMenu(fileName = "Stopwatch", menuName = "JoeSiu/Common/Stopwatch")]
|
||||
public class StopwatchSO : TimerBaseSO, IStopwatch
|
||||
{
|
||||
protected override ITimer Timer => _stopwatch;
|
||||
|
||||
[SerializeField, InlineProperty, HideLabel]
|
||||
[InfoBox("The stopwatch in ScriptableObject will be deactivated by default")]
|
||||
private Stopwatch _stopwatch = new(false, 10f);
|
||||
|
||||
public float Duration
|
||||
{
|
||||
get => _stopwatch.Duration;
|
||||
set => _stopwatch.Duration = value;
|
||||
}
|
||||
public float RemainingTime
|
||||
{
|
||||
get => _stopwatch.RemainingTime;
|
||||
set => _stopwatch.RemainingTime = value;
|
||||
}
|
||||
public float Progress
|
||||
{
|
||||
get => _stopwatch.Progress;
|
||||
set => _stopwatch.Progress = value;
|
||||
}
|
||||
public bool IsTimeReached => _stopwatch.IsTimeReached;
|
||||
public bool DeactivateOnTimeReached
|
||||
{
|
||||
get => _stopwatch.DeactivateOnTimeReached;
|
||||
set => _stopwatch.DeactivateOnTimeReached = value;
|
||||
}
|
||||
|
||||
public event Action OnTimeReached
|
||||
{
|
||||
add => _stopwatch.OnTimeReached += value;
|
||||
remove => _stopwatch.OnTimeReached -= value;
|
||||
}
|
||||
|
||||
protected override void Activate_Internal(bool resetTime = false)
|
||||
{
|
||||
_stopwatch.Activate(resetTime);
|
||||
}
|
||||
|
||||
public virtual void SetDuration(float value) => _stopwatch.SetDuration(value);
|
||||
|
||||
public virtual void SetRemainingTime(float value) => _stopwatch.SetRemainingTime(value);
|
||||
|
||||
public virtual void SetProgress(float value) => _stopwatch.SetProgress(value);
|
||||
|
||||
public void EndTime() => _stopwatch.EndTime();
|
||||
|
||||
public override void Reinitialize()
|
||||
{
|
||||
_stopwatch = new(false, _stopwatch.Duration);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: bef3fff7f3888464aab61a599e9a460a
|
||||
113
Assets/Common/Timer/Runtime/ScriptableObjects/TimerBaseSO.cs
Normal file
113
Assets/Common/Timer/Runtime/ScriptableObjects/TimerBaseSO.cs
Normal file
@@ -0,0 +1,113 @@
|
||||
using System;
|
||||
using TriInspector;
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Common.Timer.Runtime.ScriptableObjects
|
||||
{
|
||||
public abstract class TimerBaseSO : ScriptableObject, ITimer
|
||||
{
|
||||
protected abstract ITimer Timer { get; }
|
||||
|
||||
public bool IsActive => Timer.IsActive;
|
||||
public float CurrentTime
|
||||
{
|
||||
get => Timer.CurrentTime;
|
||||
set => Timer.CurrentTime = value;
|
||||
}
|
||||
public float DeltaTime => Timer.DeltaTime;
|
||||
|
||||
public void Tick() => Timer.Tick();
|
||||
|
||||
public void Activate(bool resetTime = false)
|
||||
{
|
||||
AddToManager();
|
||||
Activate_Internal(resetTime);
|
||||
}
|
||||
|
||||
public void Deactivate() => Timer.Deactivate();
|
||||
|
||||
public void ResetTime() => Timer.ResetTime();
|
||||
|
||||
public virtual void SetTime(float value)
|
||||
{
|
||||
CurrentTime = value;
|
||||
}
|
||||
|
||||
public event Action OnTick
|
||||
{
|
||||
add => Timer.OnTick += value;
|
||||
remove => Timer.OnTick -= value;
|
||||
}
|
||||
public event Action OnActivate
|
||||
{
|
||||
add => Timer.OnActivate += value;
|
||||
remove => Timer.OnActivate -= value;
|
||||
}
|
||||
public event Action OnDeactivate
|
||||
{
|
||||
add => Timer.OnDeactivate += value;
|
||||
remove => Timer.OnDeactivate -= value;
|
||||
}
|
||||
public event Action OnTimeReset
|
||||
{
|
||||
add => Timer.OnTimeReset += value;
|
||||
remove => Timer.OnTimeReset -= value;
|
||||
}
|
||||
|
||||
protected abstract void Activate_Internal(bool resetTime = false);
|
||||
|
||||
private void AddToManager()
|
||||
{
|
||||
if (!TimerManager.GetInstance()) TimerManager.CreateInstance();
|
||||
TimerManager.Instance.Add(this);
|
||||
}
|
||||
|
||||
private void RemoveFromManager()
|
||||
{
|
||||
if (!TimerManager.GetInstance()) return;
|
||||
TimerManager.Instance.Remove(this);
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
protected virtual void OnPlayModeStateChanged(PlayModeStateChange state)
|
||||
{
|
||||
if (state == PlayModeStateChange.ExitingPlayMode)
|
||||
{
|
||||
Reinitialize();
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
#if UNITY_EDITOR
|
||||
protected virtual void OnValidate()
|
||||
{
|
||||
if (!EditorApplication.isPlaying) Timer.Deactivate();
|
||||
}
|
||||
#endif
|
||||
|
||||
protected virtual void OnEnable()
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
EditorApplication.playModeStateChanged += OnPlayModeStateChanged;
|
||||
#endif
|
||||
}
|
||||
|
||||
protected virtual void OnDisable()
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
EditorApplication.playModeStateChanged -= OnPlayModeStateChanged;
|
||||
#endif
|
||||
}
|
||||
|
||||
protected virtual void OnDestroy()
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
Reinitialize();
|
||||
#endif
|
||||
}
|
||||
|
||||
[Button]
|
||||
public abstract void Reinitialize();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 312c8c00ee578df428063b28e6322aed
|
||||
25
Assets/Common/Timer/Runtime/ScriptableObjects/TimerSO.cs
Normal file
25
Assets/Common/Timer/Runtime/ScriptableObjects/TimerSO.cs
Normal file
@@ -0,0 +1,25 @@
|
||||
using TriInspector;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Common.Timer.Runtime.ScriptableObjects
|
||||
{
|
||||
[CreateAssetMenu(fileName = "Timer", menuName = "JoeSiu/Common/Timer")]
|
||||
public class TimerSO : TimerBaseSO
|
||||
{
|
||||
protected override ITimer Timer => _timer;
|
||||
|
||||
[SerializeField, InlineProperty, HideLabel]
|
||||
[InfoBox("The timer in ScriptableObject will be deactivated by default")]
|
||||
private Timer _timer = new(false);
|
||||
|
||||
protected override void Activate_Internal(bool resetTime = false)
|
||||
{
|
||||
_timer.Activate(resetTime);
|
||||
}
|
||||
|
||||
public override void Reinitialize()
|
||||
{
|
||||
_timer = new(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: cc4e8df518cd7244dbdfc3464c604bed
|
||||
120
Assets/Common/Timer/Runtime/Stopwatch.cs
Normal file
120
Assets/Common/Timer/Runtime/Stopwatch.cs
Normal file
@@ -0,0 +1,120 @@
|
||||
using System;
|
||||
using TriInspector;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Serialization;
|
||||
|
||||
namespace Common.Timer.Runtime
|
||||
{
|
||||
[Serializable]
|
||||
public class Stopwatch : Timer, IStopwatch
|
||||
{
|
||||
public override float CurrentTime
|
||||
{
|
||||
get
|
||||
{
|
||||
currentTime = Mathf.Clamp(currentTime, 0f, Duration);
|
||||
return currentTime;
|
||||
}
|
||||
set => currentTime = Mathf.Clamp(value, 0f, Duration);
|
||||
}
|
||||
|
||||
[Title("Settings (Stopwatch)")]
|
||||
[SerializeField]
|
||||
[FormerlySerializedAs("_duration")]
|
||||
protected float duration;
|
||||
[Group("Internal"), ShowInInspector, DisableInEditMode]
|
||||
public virtual float Duration
|
||||
{
|
||||
get
|
||||
{
|
||||
duration = Mathf.Max(0.001f, duration);
|
||||
return duration;
|
||||
}
|
||||
set => duration = Mathf.Max(0.001f, value);
|
||||
}
|
||||
[SerializeField]
|
||||
[FormerlySerializedAs("_deactivateOnTimeReached")]
|
||||
protected bool deactivateOnTimeReached;
|
||||
public bool DeactivateOnTimeReached
|
||||
{
|
||||
get => deactivateOnTimeReached;
|
||||
set => deactivateOnTimeReached = value;
|
||||
}
|
||||
|
||||
[Group("Internal"), ShowInInspector, DisableInEditMode]
|
||||
public virtual float RemainingTime
|
||||
{
|
||||
get => Mathf.Clamp(Duration - CurrentTime, 0f, Duration);
|
||||
set => CurrentTime = Mathf.Clamp(Duration - value, 0f, Duration);
|
||||
}
|
||||
[Group("Internal"), ShowInInspector, DisableInEditMode]
|
||||
public virtual float Progress
|
||||
{
|
||||
get => Duration == 0f ? 0f : Mathf.Clamp(CurrentTime / Duration, 0f, 1f);
|
||||
set => CurrentTime = Mathf.Clamp(Duration * value, 0f, Duration);
|
||||
}
|
||||
[Group("Internal"), ReadOnly, ShowInInspector, DisableInEditMode]
|
||||
public virtual bool IsTimeReached => CurrentTime >= Duration;
|
||||
|
||||
public event Action OnTimeReached;
|
||||
|
||||
public Stopwatch(bool isActive, float duration, bool deactivateOnTimeReached = false) : base(isActive)
|
||||
{
|
||||
this.duration = duration;
|
||||
this.deactivateOnTimeReached = deactivateOnTimeReached;
|
||||
}
|
||||
|
||||
public override void Tick()
|
||||
{
|
||||
if (IsTimeReached) return;
|
||||
|
||||
base.Tick();
|
||||
if (!IsActive) return;
|
||||
|
||||
if (IsTimeReached) EndTime();
|
||||
}
|
||||
|
||||
public override void SetTime(float value)
|
||||
{
|
||||
CurrentTime = value;
|
||||
if (IsTimeReached) EndTime();
|
||||
}
|
||||
|
||||
public virtual void SetDuration(float value)
|
||||
{
|
||||
Duration = value;
|
||||
if (IsTimeReached) EndTime();
|
||||
}
|
||||
|
||||
public virtual void SetRemainingTime(float value)
|
||||
{
|
||||
RemainingTime = value;
|
||||
if (IsTimeReached) EndTime();
|
||||
}
|
||||
|
||||
public virtual void SetProgress(float value)
|
||||
{
|
||||
Progress = value;
|
||||
if (IsTimeReached) EndTime();
|
||||
}
|
||||
|
||||
public virtual void EndTime()
|
||||
{
|
||||
currentTime = Duration;
|
||||
InvokeOnTimeReached();
|
||||
if (DeactivateOnTimeReached) Deactivate();
|
||||
}
|
||||
|
||||
protected virtual void InvokeOnTimeReached()
|
||||
{
|
||||
OnTimeReached?.Invoke();
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
|
||||
[Group("Debug"), Button("End Time"), HideInEditMode]
|
||||
private void EndTimer_Editor() => EndTime();
|
||||
|
||||
#endif
|
||||
}
|
||||
}
|
||||
2
Assets/Common/Timer/Runtime/Stopwatch.cs.meta
Normal file
2
Assets/Common/Timer/Runtime/Stopwatch.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: deafc70d1d6a826429baf2f506837603
|
||||
114
Assets/Common/Timer/Runtime/Timer.cs
Normal file
114
Assets/Common/Timer/Runtime/Timer.cs
Normal file
@@ -0,0 +1,114 @@
|
||||
using System;
|
||||
using TriInspector;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Serialization;
|
||||
|
||||
namespace Common.Timer.Runtime
|
||||
{
|
||||
/// <summary>
|
||||
/// C# implementation of a timer. Remember to call Tick() on this Timer object via script!
|
||||
/// </summary>
|
||||
[Serializable]
|
||||
[DeclareBoxGroup("Debug")]
|
||||
[DeclareBoxGroup("Internal")]
|
||||
public class Timer : ITimer
|
||||
{
|
||||
[Title("Settings")]
|
||||
[SerializeField]
|
||||
[FormerlySerializedAs("_isActive")]
|
||||
protected bool isActive;
|
||||
public virtual bool IsActive
|
||||
{
|
||||
get => isActive;
|
||||
protected set => isActive = value;
|
||||
}
|
||||
|
||||
protected float currentTime;
|
||||
[Group("Internal"), ShowInInspector, DisableInEditMode]
|
||||
public virtual float CurrentTime
|
||||
{
|
||||
get
|
||||
{
|
||||
currentTime = Mathf.Max(currentTime, 0f);
|
||||
return currentTime;
|
||||
}
|
||||
set => currentTime = Mathf.Max(value, 0f);
|
||||
}
|
||||
[Group("Internal"), ShowInInspector, DisableInEditMode]
|
||||
public virtual float DeltaTime => Time.deltaTime;
|
||||
|
||||
public event Action OnTick;
|
||||
public event Action OnActivate;
|
||||
public event Action OnDeactivate;
|
||||
public event Action OnTimeReset;
|
||||
|
||||
public Timer(bool isActive)
|
||||
{
|
||||
this.isActive = isActive;
|
||||
}
|
||||
|
||||
#region Public Methods
|
||||
|
||||
public virtual void Tick()
|
||||
{
|
||||
if (!IsActive) return;
|
||||
|
||||
CurrentTime += DeltaTime;
|
||||
InvokeOnTick();
|
||||
}
|
||||
|
||||
public virtual void Activate(bool resetTime = false)
|
||||
{
|
||||
IsActive = true;
|
||||
InvokeOnActivate();
|
||||
if (resetTime) ResetTime();
|
||||
}
|
||||
|
||||
public virtual void Deactivate()
|
||||
{
|
||||
IsActive = false;
|
||||
InvokeOnDeactivate();
|
||||
}
|
||||
|
||||
public virtual void ResetTime()
|
||||
{
|
||||
CurrentTime = 0f;
|
||||
InvokeOnTimeReset();
|
||||
}
|
||||
|
||||
public virtual void SetTime(float value)
|
||||
{
|
||||
CurrentTime = value;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Protected Methods
|
||||
|
||||
protected virtual void InvokeOnTick() => OnTick?.Invoke();
|
||||
protected virtual void InvokeOnActivate() => OnActivate?.Invoke();
|
||||
protected virtual void InvokeOnDeactivate() => OnDeactivate?.Invoke();
|
||||
protected virtual void InvokeOnTimeReset() => OnTimeReset?.Invoke();
|
||||
|
||||
#endregion
|
||||
|
||||
#if UNITY_EDITOR
|
||||
|
||||
[Group("Debug"), Button("Activate"), HideInEditMode]
|
||||
private void Activate_Editor() => Activate(false);
|
||||
|
||||
[Group("Debug"), Button("Activate (Reset Time)"), HideInEditMode]
|
||||
private void ActivateResetTime_Editor() => Activate(true);
|
||||
|
||||
[Group("Debug"), Button("Deactivate"), HideInEditMode]
|
||||
private void Deactivate_Editor() => Deactivate();
|
||||
|
||||
[Group("Debug"), Button("Tick"), HideInEditMode]
|
||||
private void Tick_Editor() => Tick();
|
||||
|
||||
[Group("Debug"), Button("Reset Time"), HideInEditMode]
|
||||
private void ResetTime_Editor() => ResetTime();
|
||||
|
||||
#endif
|
||||
}
|
||||
}
|
||||
2
Assets/Common/Timer/Runtime/Timer.cs.meta
Normal file
2
Assets/Common/Timer/Runtime/Timer.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 64b5ad6c3a3b3934892167377d2ff419
|
||||
41
Assets/Common/Timer/Runtime/TimerManager.cs
Normal file
41
Assets/Common/Timer/Runtime/TimerManager.cs
Normal file
@@ -0,0 +1,41 @@
|
||||
using System.Collections.Generic;
|
||||
using Common.Timer.Runtime.ScriptableObjects;
|
||||
using JoeSiu.Common.Infrastructure.Runtime;
|
||||
using TriInspector;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Common.Timer.Runtime
|
||||
{
|
||||
public class TimerManager : MonoSingleton<TimerManager>
|
||||
{
|
||||
[InfoBox("As ScriptableObject doesn't have Update() function, use this class to control the ScriptableObject timer's tick")]
|
||||
[SerializeField]
|
||||
private List<TimerBaseSO> _timerSOs = new();
|
||||
public IReadOnlyList<TimerBaseSO> TimerSOs => _timerSOs.AsReadOnly();
|
||||
|
||||
protected override void Awake()
|
||||
{
|
||||
base.Awake();
|
||||
DontDestroyOnLoad(this);
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
_timerSOs.ForEach(so => so?.Tick());
|
||||
}
|
||||
|
||||
public void Add(TimerBaseSO timerBaseSO)
|
||||
{
|
||||
if (_timerSOs.Contains(timerBaseSO)) return;
|
||||
_timerSOs.Add(timerBaseSO);
|
||||
Debug.Log($"Added TimerBaseSO '{timerBaseSO.name}' to {nameof(TimerManager)}", this);
|
||||
}
|
||||
|
||||
public void Remove(TimerBaseSO timerBaseSO)
|
||||
{
|
||||
if (!_timerSOs.Contains(timerBaseSO)) return;
|
||||
_timerSOs.Remove(timerBaseSO);
|
||||
Debug.Log( $"Removed TimerBaseSO '{timerBaseSO.name}' from {nameof(TimerManager)}", this);
|
||||
}
|
||||
}
|
||||
}
|
||||
2
Assets/Common/Timer/Runtime/TimerManager.cs.meta
Normal file
2
Assets/Common/Timer/Runtime/TimerManager.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 477f0034741527640a767bbcb0c42a01
|
||||
8
Assets/Common/Timer/Runtime/UI.meta
Normal file
8
Assets/Common/Timer/Runtime/UI.meta
Normal file
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 6346ab71a8da1784c84e99bf10a88384
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
68
Assets/Common/Timer/Runtime/UI/TimerDebugText.cs
Normal file
68
Assets/Common/Timer/Runtime/UI/TimerDebugText.cs
Normal file
@@ -0,0 +1,68 @@
|
||||
using System;
|
||||
using JoeSiu.Common.Serialization.Runtime;
|
||||
using TMPro;
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Common.Timer.Runtime.UI
|
||||
{
|
||||
public class TimerDebugText : MonoBehaviour
|
||||
{
|
||||
[SerializeField]
|
||||
private SerializableInterface<ITimer> _timer = new();
|
||||
public TMP_Text text;
|
||||
public string prefix;
|
||||
public int decimalPlaces = 2;
|
||||
|
||||
public ITimer Timer
|
||||
{
|
||||
get => _timer.Value;
|
||||
set => _timer.Value = value;
|
||||
}
|
||||
|
||||
private void Reset()
|
||||
{
|
||||
text = GetComponent<TMP_Text>();
|
||||
}
|
||||
|
||||
private void OnValidate()
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
if (!EditorApplication.isPlaying && Selection.Contains(gameObject)) UpdateText(Timer);
|
||||
#endif
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
UpdateText(Timer);
|
||||
}
|
||||
|
||||
public void UpdateText(ITimer timer)
|
||||
{
|
||||
if (!text) return;
|
||||
if (timer == null) return;
|
||||
|
||||
var format = $"F{decimalPlaces}";
|
||||
var info = string.IsNullOrEmpty(prefix) ? "" : $"{prefix}\n";
|
||||
if (timer is Behaviour behaviour) info += $"enabled: {behaviour.enabled}\n";
|
||||
info += $"active: {timer.IsActive}\n";
|
||||
|
||||
switch (timer)
|
||||
{
|
||||
// Check if timer implements IStopwatch
|
||||
case IStopwatch stopwatch:
|
||||
info += $"isTimeReached: {stopwatch.IsTimeReached}\n";
|
||||
info += $"{Math.Round(stopwatch.CurrentTime, decimalPlaces).ToString(format)}s / " +
|
||||
$"{Math.Round(stopwatch.Duration, decimalPlaces).ToString(format)}s " +
|
||||
$"(remain: {Math.Round(stopwatch.RemainingTime, decimalPlaces).ToString(format)}s)\n";
|
||||
break;
|
||||
// Fallback for base Timer class
|
||||
default:
|
||||
info += $"{Math.Round(timer.CurrentTime, decimalPlaces).ToString(format)}s\n";
|
||||
break;
|
||||
}
|
||||
|
||||
text.text = info;
|
||||
}
|
||||
}
|
||||
}
|
||||
2
Assets/Common/Timer/Runtime/UI/TimerDebugText.cs.meta
Normal file
2
Assets/Common/Timer/Runtime/UI/TimerDebugText.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 9449292013eda4945bf3f8bc3ba3b52d
|
||||
79
Assets/Common/Timer/Runtime/UI/TimerDurationText.cs
Normal file
79
Assets/Common/Timer/Runtime/UI/TimerDurationText.cs
Normal file
@@ -0,0 +1,79 @@
|
||||
using System;
|
||||
using JoeSiu.Common.Serialization.Runtime;
|
||||
using TMPro;
|
||||
using TriInspector;
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Common.Timer.Runtime.UI
|
||||
{
|
||||
[ExecuteAlways]
|
||||
public class TimerDurationText : MonoBehaviour
|
||||
{
|
||||
public TMP_Text text;
|
||||
[SerializeField]
|
||||
private SerializableInterface<ITimer> _timer = new();
|
||||
#if UNITY_EDITOR
|
||||
[ShowIf(nameof(IsStopwatch))]
|
||||
#endif
|
||||
public bool useRemainingTimeForStopwatch = true;
|
||||
[Range(0, 15)]
|
||||
public int precision = 0;
|
||||
[Range(0, 15)]
|
||||
public int padLeft = 0;
|
||||
[Range(0, 15)]
|
||||
public int padRight = 0;
|
||||
|
||||
#if UNITY_EDITOR
|
||||
private bool IsStopwatch => Timer is IStopwatch;
|
||||
#endif
|
||||
|
||||
public ITimer Timer
|
||||
{
|
||||
get => _timer?.Value;
|
||||
set => _timer.Value = value;
|
||||
}
|
||||
|
||||
private void Reset()
|
||||
{
|
||||
text = GetComponent<TMP_Text>();
|
||||
}
|
||||
|
||||
private void OnValidate()
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
if (!EditorApplication.isPlaying && Selection.Contains(gameObject)) UpdateText(Timer);
|
||||
#endif
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
UpdateText(Timer);
|
||||
}
|
||||
|
||||
public void UpdateText(ITimer timer)
|
||||
{
|
||||
if (!text) return;
|
||||
if (timer == null) return;
|
||||
|
||||
float time;
|
||||
|
||||
if (useRemainingTimeForStopwatch && timer is IStopwatch stopwatch)
|
||||
{
|
||||
time = stopwatch.RemainingTime;
|
||||
}
|
||||
else
|
||||
{
|
||||
time = timer.CurrentTime;
|
||||
}
|
||||
|
||||
time = (float)Math.Round(time, precision);
|
||||
var timeString = time.ToString();
|
||||
// TODO: PadLeft / PadRight doesn't work in all cases (e.g., given time of 1, it can't turn into 01.00)
|
||||
// TODO: Use a custom user defined formatter instead?
|
||||
if (padLeft > 0) timeString = timeString.PadLeft(padLeft, '0');
|
||||
if (padRight > 0) timeString = timeString.PadRight(padRight, '0');
|
||||
text.SetText(timeString);
|
||||
}
|
||||
}
|
||||
}
|
||||
2
Assets/Common/Timer/Runtime/UI/TimerDurationText.cs.meta
Normal file
2
Assets/Common/Timer/Runtime/UI/TimerDurationText.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a872f72d4d5d8e94895cff23a747e3f0
|
||||
20
Assets/Common/Timer/package.json
Normal file
20
Assets/Common/Timer/package.json
Normal file
@@ -0,0 +1,20 @@
|
||||
{
|
||||
"name": "com.joesiu.common.timer",
|
||||
"version": "1.0.0",
|
||||
"description": "Set of timer and stopwatch components.",
|
||||
"displayName": "Timer",
|
||||
"unity": "2019.4",
|
||||
"author": {
|
||||
"name": "Joe Siu",
|
||||
"url": "https://github.com/JoeSiu"
|
||||
},
|
||||
"changelogUrl": "https://github.com/JoeSiu/UnityPackages/blob/main/CHANGELOG.md",
|
||||
"dependencies": {
|
||||
"com.codewriter.triinspector": "https://github.com/codewriter-packages/Tri-Inspector.git",
|
||||
"com.joesiu.common.infrastructure": "https://github.com/JoeSiu/UnityPackages.git?path=Assets/Common/Infrastructure",
|
||||
"com.joesiu.common.serialization": "https://github.com/JoeSiu/UnityPackages.git?path=Assets/Common/Serialization"
|
||||
},
|
||||
"documentationUrl": "https://github.com/JoeSiu/UnityPackages/",
|
||||
"keywords": [],
|
||||
"licensesUrl": "https://github.com/JoeSiu/UnityPackages/blob/main/LICENSE.md"
|
||||
}
|
||||
7
Assets/Common/Timer/package.json.meta
Normal file
7
Assets/Common/Timer/package.json.meta
Normal file
@@ -0,0 +1,7 @@
|
||||
fileFormatVersion: 2
|
||||
guid: cb89a7fa74c613b41b84b47ad22e88aa
|
||||
TextScriptImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
61
Assets/Common/update-template.bat~
Normal file
61
Assets/Common/update-template.bat~
Normal file
@@ -0,0 +1,61 @@
|
||||
@echo off
|
||||
setlocal enabledelayedexpansion
|
||||
|
||||
:: Default values
|
||||
set "company_name=joesiu"
|
||||
set "package_name=common.package-name"
|
||||
set "package_display_name=Package Display Name"
|
||||
set "package_description=This is an example package description."
|
||||
set "git_url=https://github.com/JoeSiu/UnityPackages"
|
||||
set "package_path=Assets/Common/"
|
||||
set "author_name=Joe Siu"
|
||||
set "author_url=https://github.com/JoeSiu"
|
||||
|
||||
:: Get user input with defaults
|
||||
echo.
|
||||
set /p "input_company_name=Enter company_name [%company_name%]: "
|
||||
if not "!input_company_name!"=="" set "company_name=!input_company_name!"
|
||||
|
||||
echo.
|
||||
set /p "input_package_name=Enter package_name [%package_name%]: "
|
||||
if not "!input_package_name!"=="" set "package_name=!input_package_name!"
|
||||
|
||||
echo.
|
||||
set /p "input_package_display_name=Enter package_display_name [%package_display_name%]: "
|
||||
if not "!input_package_display_name!"=="" set "package_display_name=!input_package_display_name!"
|
||||
|
||||
echo.
|
||||
set /p "input_package_description=Enter package_description [%package_description%]: "
|
||||
if not "!input_package_description!"=="" set "package_description=!input_package_description!"
|
||||
|
||||
echo.
|
||||
set /p "input_git_url=Enter git_url [%git_url%]: "
|
||||
if not "!input_git_url!"=="" set "git_url=!input_git_url!"
|
||||
|
||||
echo.
|
||||
set /p "input_package_path=Enter package_path [%package_path%]: "
|
||||
if not "!input_package_path!"=="" set "package_path=!input_package_path!"
|
||||
|
||||
echo.
|
||||
set /p "input_author_name=Enter author_name [%author_name%]: "
|
||||
if not "!input_author_name!"=="" set "author_name=!input_author_name!"
|
||||
|
||||
echo.
|
||||
set /p "input_author_url=Enter author_url [%author_url%]: "
|
||||
if not "!input_author_url!"=="" set "author_url=!input_author_url!"
|
||||
|
||||
:: Update files
|
||||
echo.
|
||||
echo Updating variables ^& filenames...
|
||||
|
||||
:: Use PowerShell to update file contents (excluding .git folder and batch files)
|
||||
powershell -Command "$companyName='%company_name%'; $packageName='%package_name%'; $displayName='%package_display_name%'; $description='%package_description%'; $gitUrl='%git_url%'; $packagePath='%package_path%'; $authorName='%author_name%'; $authorUrl='%author_url%'; Get-ChildItem -Recurse -File | Where-Object { $_.FullName -notmatch '\\.git' -and $_.Extension -ne '.bat' } | ForEach-Object { $content = [System.IO.File]::ReadAllText($_.FullName); $content = $content -replace '\[company-name\]', $companyName -replace '\[package-name\]', $packageName -replace '\[package-display-name\]', $displayName -replace '\[package-description\]', $description -replace '\[git-url\]', $gitUrl -replace '\[package-path\]', $packagePath -replace '\[author-name\]', $authorName -replace '\[author_name\]', $authorName -replace '\[author-url\]', $authorUrl; [System.IO.File]::WriteAllText($_.FullName, $content) }"
|
||||
|
||||
:: Rename files (excluding .git folder and batch files)
|
||||
powershell -Command "$companyName='%company_name%'; $packageName='%package_name%'; Get-ChildItem -Recurse -File | Where-Object { $_.FullName -notmatch '\\.git' -and $_.Extension -ne '.bat' -and ($_.Name -match '\[company-name\]\.\[package-name\]' -or $_.Name -match '^\[package-name\]') } | ForEach-Object { $dir = $_.DirectoryName; $newName = $_.Name -replace '\[company-name\]\.\[package-name\]', ($companyName + '.' + $packageName) -replace '^\[package-name\]', $packageName; Rename-Item -LiteralPath $_.FullName -NewName $newName }"
|
||||
|
||||
echo.
|
||||
echo DONE!
|
||||
echo.
|
||||
|
||||
pause
|
||||
Reference in New Issue
Block a user