From 750d7f6b8810cf6ecab655cd4e2ee25cedb1a69b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gerard=20Gasc=C3=B3n?= <52170489+GerardGascon@users.noreply.github.com> Date: Fri, 11 Jun 2021 19:34:49 +0200 Subject: [PATCH] Added Timer New version featuring a timer --- README.md | 4 +- Tools/AudioManager/Sounds.cs | 2 +- Tools/DialogueSystem/Dialogue.cs | 2 +- Tools/DialogueSystem/TMP_Animated.cs | 15 ++++-- Tools/Editor/ToolsEditor.cs | 24 ++++----- Tools/ObjectPooler/Pool.cs | 2 +- Tools/Timer.meta | 8 +++ Tools/Timer/Timer.cs | 79 ++++++++++++++++++++++++++++ Tools/Timer/Timer.cs.meta | 11 ++++ Tools/Timer/TimerType.cs | 7 +++ Tools/Timer/TimerType.cs.meta | 11 ++++ Tools/Timer/TimerUtility.cs | 29 ++++++++++ Tools/Timer/TimerUtility.cs.meta | 11 ++++ package.json | 4 +- 14 files changed, 186 insertions(+), 23 deletions(-) create mode 100644 Tools/Timer.meta create mode 100644 Tools/Timer/Timer.cs create mode 100644 Tools/Timer/Timer.cs.meta create mode 100644 Tools/Timer/TimerType.cs create mode 100644 Tools/Timer/TimerType.cs.meta create mode 100644 Tools/Timer/TimerUtility.cs create mode 100644 Tools/Timer/TimerUtility.cs.meta diff --git a/README.md b/README.md index c9fa094..854060e 100644 --- a/README.md +++ b/README.md @@ -21,11 +21,11 @@ First install the TextMeshPro and Cinemachine into your Unity project ### Git Installation (Best way to get latest version) -If you have git in your computer, you can open Package Manager inside Unity, select "Add package from Git url...", and paste link [https://github.com/GerardGascon/SimpleTools.git](https://github.com/GerardGascon/SimpleTools.git) +If you have git in your computer, you can open Package Manager inside Unity, select "Add package from Git url...", and paste link [https://github.com/Geri8/SimpleTools.git](https://github.com/Geri8/SimpleTools.git) or -Open the manifest.json file of your Unity project. Add "com.geri.simpletools": "[https://github.com/GerardGascon/SimpleTools.git](https://github.com/GerardGascon/SimpleTools.git)" +Open the manifest.json file of your Unity project. Add "com.geri.simpletools": "[https://github.com/Geri8/SimpleTools.git](https://github.com/Geri8/SimpleTools.git)" ### Manual Installation diff --git a/Tools/AudioManager/Sounds.cs b/Tools/AudioManager/Sounds.cs index 366dc67..9f9a5e4 100644 --- a/Tools/AudioManager/Sounds.cs +++ b/Tools/AudioManager/Sounds.cs @@ -1,7 +1,7 @@ using UnityEngine; using UnityEngine.Audio; -[CreateAssetMenu(fileName = "Sounds", menuName = "Tools/Sounds", order = 0)] +[CreateAssetMenu(fileName = "Sounds", menuName = "Simple Tools/Sounds", order = 11)] public class Sounds : ScriptableObject{ [Tooltip("The music mixer.")] diff --git a/Tools/DialogueSystem/Dialogue.cs b/Tools/DialogueSystem/Dialogue.cs index 6df4ba6..b7674d0 100644 --- a/Tools/DialogueSystem/Dialogue.cs +++ b/Tools/DialogueSystem/Dialogue.cs @@ -1,6 +1,6 @@ using UnityEngine; -[CreateAssetMenu(fileName = "New Character", menuName = "Tools/Character", order = 0)] +[CreateAssetMenu(fileName = "New Character", menuName = "Simple Tools/Character", order = 11)] public class Dialogue : ScriptableObject{ public bool displayName; diff --git a/Tools/DialogueSystem/TMP_Animated.cs b/Tools/DialogueSystem/TMP_Animated.cs index b85b257..904041b 100644 --- a/Tools/DialogueSystem/TMP_Animated.cs +++ b/Tools/DialogueSystem/TMP_Animated.cs @@ -8,12 +8,17 @@ namespace TMPro{ public class TMP_Animated : TextMeshProUGUI{ - float speed; + float speed = 20; public TextRevealEvent onTextReveal; public DialogueEvent onDialogueFinish; - public void ReadText(string newText){ + void Update(){ + /*if(Application.isPlaying) + this.Animate();*/ + } + + public void ReadText(string newText){ text = string.Empty; string[] subTexts = newText.Split('<', '>'); @@ -27,7 +32,7 @@ namespace TMPro{ } bool isCustomTag(string tag){ - return tag.StartsWith("speed=") || tag.StartsWith("pause="); + return tag.StartsWith("speed=") || tag.StartsWith("pause=") || tag.StartsWith("wave") || tag.StartsWith("rainbow") || tag.StartsWith("shake"); } text = displayText; @@ -45,7 +50,9 @@ namespace TMPro{ onTextReveal.Invoke(subTexts[subCounter][visibleCounter]); visibleCounter++; maxVisibleCharacters++; - yield return new WaitForSeconds(1f / speed); + //this.Animate(); + + yield return new WaitForSeconds(text[maxVisibleCharacters - 1] == ' ' ? 0 : (1f / speed)); } visibleCounter = 0; } diff --git a/Tools/Editor/ToolsEditor.cs b/Tools/Editor/ToolsEditor.cs index a7fe40a..b28a17b 100644 --- a/Tools/Editor/ToolsEditor.cs +++ b/Tools/Editor/ToolsEditor.cs @@ -1,4 +1,3 @@ -#if UNITY_EDITOR using System.Collections; using System.Collections.Generic; using UnityEngine; @@ -14,13 +13,13 @@ using System; public class ToolsEditor{ - [MenuItem("GameObject/Tools/AudioManager", false, 10)] + [MenuItem("GameObject/Simple Tools/AudioManager", false, 10)] static void CreateAudioManager(){ GameObject audioManager = new GameObject("AudioManager"); audioManager.AddComponent(); } - [MenuItem("GameObject/Tools/Dialogue System", false, 10)] + [MenuItem("GameObject/Simple Tools/Dialogue System", false, 10)] static void CreateDialogueSystem(){ GameObject dialogueCanvas = new GameObject("DialogueCanvas"); dialogueCanvas.AddComponent(); @@ -51,7 +50,7 @@ public class ToolsEditor{ dialogueSystem.nameField = name; } - [MenuItem("GameObject/Tools/Camera Trigger/2D", false, 10)] + [MenuItem("GameObject/Simple Tools/Camera Trigger/2D", false, 10)] static void CreateCameraTrigger2D(){ GameObject cameraTrigger = new GameObject("CameraTrigger2D"); cameraTrigger.AddComponent(); @@ -64,7 +63,7 @@ public class ToolsEditor{ cam.m_Lens.Orthographic = true; } - [MenuItem("GameObject/Tools/Camera Trigger/3D", false, 10)] + [MenuItem("GameObject/Simple Tools/Camera Trigger/3D", false, 10)] static void CreateCameraTrigger3D(){ GameObject cameraTrigger = new GameObject("CameraTrigger3D"); cameraTrigger.AddComponent(); @@ -78,7 +77,7 @@ public class ToolsEditor{ } #if CINEMACHINE_271_OR_NEWER - [MenuItem("GameObject/Tools/ScreenShake Camera/2D", false, 10)] + [MenuItem("GameObject/Simple Tools/ScreenShake Camera/2D", false, 10)] static void CreateScreenShakeCamera2d(){ GameObject screenShakeCamera = new GameObject("ScreenShakeCamera"); CinemachineVirtualCamera vCam = screenShakeCamera.AddComponent(); @@ -92,7 +91,7 @@ public class ToolsEditor{ shake.m_AmplitudeGain = 0f; shake.m_FrequencyGain = 1f; } - [MenuItem("GameObject/Tools/ScreenShake Camera/3D", false, 10)] + [MenuItem("GameObject/Simple Tools/ScreenShake Camera/3D", false, 10)] static void CreateScreenShakeCamera3d(){ GameObject screenShakeCamera = new GameObject("ScreenShakeCamera"); CinemachineVirtualCamera vCam = screenShakeCamera.AddComponent(); @@ -107,7 +106,7 @@ public class ToolsEditor{ shake.m_FrequencyGain = 1f; } #else - [MenuItem("GameObject/Tools/ScreenShake Camera", false, 10)] + [MenuItem("GameObject/Simple Tools/ScreenShake Camera", false, 10)] static void CreateScreenShakeCamera2d(){ GameObject screenShakeCamera = new GameObject("ScreenShakeCamera"); CinemachineVirtualCamera vCam = screenShakeCamera.AddComponent(); @@ -122,7 +121,8 @@ public class ToolsEditor{ } #endif - [MenuItem("Assets/Create/Tools/Create Loading Scene")] + [MenuItem("Assets/Create/Simple Tools/Create Loading Scene")] + [MenuItem("Simple Tools/Create Loading Scene")] static void CreateLoadingScene(){ EditorSceneManager.SaveOpenScenes(); @@ -163,7 +163,7 @@ public class ToolsEditor{ progressBarTransform.anchoredPosition = Vector2.zero; progressBarTransform.sizeDelta = new Vector2Int(900, 20); - progressBar.sprite = (Sprite)AssetDatabase.LoadAssetAtPath("Packages/com.geri.simpletools/Tools/Editor/Square.png", typeof(Sprite)); + progressBar.sprite = (Sprite)AssetDatabase.LoadAssetAtPath("Packages/com.geri.simpletools/Simple Tools/Editor/Square.png", typeof(Sprite)); progressBar.type = Image.Type.Filled; progressBar.fillMethod = Image.FillMethod.Horizontal; progressBar.fillOrigin = (int)Image.OriginHorizontal.Left; @@ -172,7 +172,8 @@ public class ToolsEditor{ } #if UNITY_2019_3_OR_NEWER - [MenuItem("Assets/Create/Tools/Create Menu Scene")] + [MenuItem("Assets/Create/Simple Tools/Create Menu Scene")] + [MenuItem("Simple Tools/Create Menu Scene")] static void CreateMenuScene(){ EditorSceneManager.SaveOpenScenes(); @@ -532,4 +533,3 @@ public class ToolsEditor{ #endregion #endif } -#endif diff --git a/Tools/ObjectPooler/Pool.cs b/Tools/ObjectPooler/Pool.cs index 1e99a63..50ed704 100644 --- a/Tools/ObjectPooler/Pool.cs +++ b/Tools/ObjectPooler/Pool.cs @@ -1,7 +1,7 @@ using System.Collections.Generic; using UnityEngine; -[CreateAssetMenu(fileName = "New Pool", menuName = "Tools/Pool", order = 0)] +[CreateAssetMenu(fileName = "New Pool", menuName = "Simple Tools/Pool", order = 11)] public class Pool : ScriptableObject{ public List pools; diff --git a/Tools/Timer.meta b/Tools/Timer.meta new file mode 100644 index 0000000..4762356 --- /dev/null +++ b/Tools/Timer.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: b904c9938b1a3df4e83b1103e3085400 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Tools/Timer/Timer.cs b/Tools/Timer/Timer.cs new file mode 100644 index 0000000..0524682 --- /dev/null +++ b/Tools/Timer/Timer.cs @@ -0,0 +1,79 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; +using System; +using TMPro; + +namespace SimpleTools.Timer{ + public class Timer : MonoBehaviour{ + + float elapsedTime; + public float ElapsedTime { get { return elapsedTime; } } + bool isPaused; + public bool IsPaused { get { return isPaused; } } + TimeSpan timePlaying; + public TimeSpan TimePlaying { get { return timePlaying; } } + TMP_Text timer; + public TMP_Text TimerText { get { return timer; } } + TimerType timerType; + public TimerType TimerType { get { return timerType; } } + + public void Setup(float elapsedTime, bool isPaused, TimeSpan timePlaying, TMP_Text timer, TimerType timerType, string text){ + this.elapsedTime = elapsedTime; + this.isPaused = isPaused; + this.timePlaying = timePlaying; + this.timer = timer; + this.timerType = timerType; + timer.text = text; + } + + IEnumerator UpdateTimer(){ + while (!isPaused){ + if(timerType == TimerType.Clock){ + timer.text = DateTime.Now.ToString("HH:mm:ss"); + }else{ + switch (timerType){ + case TimerType.Countdown: + elapsedTime -= Time.deltaTime; + if(elapsedTime < 0f){ + elapsedTime = 0f; + isPaused = true; + } + break; + case TimerType.Stopwatch: + elapsedTime += Time.deltaTime; + break; + } + timePlaying = TimeSpan.FromSeconds(elapsedTime); + timer.text = timePlaying.ToString("m':'ss'.'ff"); + } + yield return null; + } + } + + public void Play(){ + isPaused = false; + StartCoroutine(UpdateTimer()); + } + + public void Stop(){ + isPaused = true; + } + + public void ResetTimer(){ + isPaused = true; + elapsedTime = 0f; + timePlaying = TimeSpan.FromSeconds(elapsedTime); + timer.text = timePlaying.ToString("m':'ss'.'ff"); + } + + public void Restart(){ + isPaused = false; + elapsedTime = 0f; + timePlaying = TimeSpan.FromSeconds(elapsedTime); + timer.text = timePlaying.ToString("m':'ss'.'ff"); + StopAllCoroutines(); + StartCoroutine(UpdateTimer()); + } + } +} diff --git a/Tools/Timer/Timer.cs.meta b/Tools/Timer/Timer.cs.meta new file mode 100644 index 0000000..af559b2 --- /dev/null +++ b/Tools/Timer/Timer.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 6daeed0a6935f484f85fc0ec1871344f +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Tools/Timer/TimerType.cs b/Tools/Timer/TimerType.cs new file mode 100644 index 0000000..a758dbb --- /dev/null +++ b/Tools/Timer/TimerType.cs @@ -0,0 +1,7 @@ +namespace SimpleTools.Timer{ + public enum TimerType{ + Countdown, + Stopwatch, + Clock + } +} diff --git a/Tools/Timer/TimerType.cs.meta b/Tools/Timer/TimerType.cs.meta new file mode 100644 index 0000000..3de6eec --- /dev/null +++ b/Tools/Timer/TimerType.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: f285456223780f946b49c6131d493c01 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Tools/Timer/TimerUtility.cs b/Tools/Timer/TimerUtility.cs new file mode 100644 index 0000000..90ba52e --- /dev/null +++ b/Tools/Timer/TimerUtility.cs @@ -0,0 +1,29 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; +using System; +using TMPro; + +namespace SimpleTools.Timer{ + public static class TimerUtility { + public static Timer SetupTimer(this TMP_Text container, TimerType timerType, float countdownTime = 60f){ + Timer t = container.gameObject.AddComponent(); + float elapsedTime = 0f; + string text = string.Empty; + TimeSpan timePlaying = TimeSpan.Zero; + switch (timerType){ + case TimerType.Countdown: + elapsedTime = countdownTime; + timePlaying = TimeSpan.FromSeconds(elapsedTime); + text = timePlaying.ToString("m':'ss'.'ff"); + break; + case TimerType.Clock: + text = DateTime.Now.ToString("HH:mm:ss"); + break; + } + t.Setup(elapsedTime, true, timePlaying, container, timerType, text); + + return t; + } + } +} diff --git a/Tools/Timer/TimerUtility.cs.meta b/Tools/Timer/TimerUtility.cs.meta new file mode 100644 index 0000000..1b46b33 --- /dev/null +++ b/Tools/Timer/TimerUtility.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 38076483429281e438cee653b26bb03d +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/package.json b/package.json index 79a0b24..c13a739 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "com.geri.simpletools", - "version": "1.0.2", + "version": "1.1.0", "displayName": "Simple Tools", "description": "This package contains simple tools to use in your project.", "unity": "2018.4", @@ -20,4 +20,4 @@ "url": "https://geri8.itch.io/" }, "type": "commonjs" -} +} \ No newline at end of file