From ab7c3c1372c70d84e30b007b0111c8ae1126ec85 Mon Sep 17 00:00:00 2001 From: Ricardo Date: Tue, 20 Jun 2023 23:37:06 +0100 Subject: [PATCH] Timer manager, function to easily get icon from sprite --- .../MustBeAssignedAttributeDrawer.cs | 2 +- .../Settings/RicUtils_EditorSettings.cs | 2 +- .../Editor/Utilities/ToolUtilities.cs | 11 ++++ .../Editor/Windows/GenericEditorWindow.cs | 37 +++++++++-- .../Scripts/Managers/SingletonCreation.cs | 21 ++++--- .../Runtime/Scripts/Managers/TimerManager.cs | 35 +++++++++++ .../Scripts/Managers/TimerManager.cs.meta | 11 ++++ .../Runtime/Scripts/Utilities/GameTimer.cs | 63 +++++++++++++++++++ .../Scripts/Utilities/GameTimer.cs.meta | 11 ++++ .../Runtime/Scripts/Utilities/RicUtilities.cs | 12 ++++ Assets/RicUtils/package.json | 2 +- 11 files changed, 192 insertions(+), 15 deletions(-) create mode 100644 Assets/RicUtils/Runtime/Scripts/Managers/TimerManager.cs create mode 100644 Assets/RicUtils/Runtime/Scripts/Managers/TimerManager.cs.meta create mode 100644 Assets/RicUtils/Runtime/Scripts/Utilities/GameTimer.cs create mode 100644 Assets/RicUtils/Runtime/Scripts/Utilities/GameTimer.cs.meta diff --git a/Assets/RicUtils/Editor/AttributeDrawer/MustBeAssignedAttributeDrawer.cs b/Assets/RicUtils/Editor/AttributeDrawer/MustBeAssignedAttributeDrawer.cs index cead3b9..48c3ffd 100644 --- a/Assets/RicUtils/Editor/AttributeDrawer/MustBeAssignedAttributeDrawer.cs +++ b/Assets/RicUtils/Editor/AttributeDrawer/MustBeAssignedAttributeDrawer.cs @@ -50,7 +50,7 @@ private static void Assert() foreach (var field in fields) { - if (!field.IsDefined(typeof(MustBeAssignedAttribute), false) && !field.IsDefined(typeof(SerializeField), false)) continue; + if (!field.IsDefined(typeof(MustBeAssignedAttribute), false) || !field.IsDefined(typeof(SerializeField), false)) continue; var error = GetError(field, behaviour); diff --git a/Assets/RicUtils/Editor/Settings/RicUtils_EditorSettings.cs b/Assets/RicUtils/Editor/Settings/RicUtils_EditorSettings.cs index b3d100f..22142c4 100644 --- a/Assets/RicUtils/Editor/Settings/RicUtils_EditorSettings.cs +++ b/Assets/RicUtils/Editor/Settings/RicUtils_EditorSettings.cs @@ -12,7 +12,7 @@ internal class RicUtils_EditorSettings : ScriptableObject public static string version { - get { return "1.6.0"; } + get { return "1.6.1"; } } public static ScriptableEditor[] scriptableEditors diff --git a/Assets/RicUtils/Editor/Utilities/ToolUtilities.cs b/Assets/RicUtils/Editor/Utilities/ToolUtilities.cs index cd404b8..2fa8f7a 100644 --- a/Assets/RicUtils/Editor/Utilities/ToolUtilities.cs +++ b/Assets/RicUtils/Editor/Utilities/ToolUtilities.cs @@ -118,5 +118,16 @@ public static void AddStylesheet(this VisualElement root, params string[] styleS root.styleSheets.Add(stylesheet); } } + + public static Texture2D RenderStaticPreview(Sprite sprite, int width, int height) + { + if (sprite == null) return null; + var spriteUtilityType = System.Type.GetType("UnityEditor.SpriteUtility,UnityEditor.CoreModule"); + if (spriteUtilityType == null) return null; + var ret = spriteUtilityType + .GetMethod("RenderStaticPreview", new System.Type[] { typeof(Sprite), typeof(Color), typeof(int), typeof(int) }) + .Invoke(null, new object[] { sprite, Color.white, width, height }); + return ret as Texture2D; + } } } diff --git a/Assets/RicUtils/Editor/Windows/GenericEditorWindow.cs b/Assets/RicUtils/Editor/Windows/GenericEditorWindow.cs index dbb7493..4bf37ab 100644 --- a/Assets/RicUtils/Editor/Windows/GenericEditorWindow.cs +++ b/Assets/RicUtils/Editor/Windows/GenericEditorWindow.cs @@ -29,6 +29,10 @@ public abstract class GenericEditorWindow : Edit private System.Action onLoad; + public new VisualElement rootVisualElement => _container; + + private VisualElement _container; + protected virtual void OnEnable() { serializedObject = new SerializedObject(this); @@ -36,8 +40,14 @@ protected virtual void OnEnable() private void CreateGUI() { - rootVisualElement.AddCommonStylesheet(); + base.rootVisualElement.AddCommonStylesheet(); + _container = new ScrollView() + { + showHorizontal = false, showVertical = true, + }; + base.rootVisualElement.Add(_container); + onLoad = null; soObjectField = rootVisualElement.AddObjectField(scriptableObject, "Scriptable Object", () => { @@ -228,10 +238,22 @@ private void CheckCompletion() public void RegisterCheckCompletion(INotifyValueChanged control) { - control.RegisterValueChangedCallback(callback => - { - CheckCompletion(); - }); + control.RegisterValueChangedCallback(CheckCompletionCallback); + } + + public void UnregisterCheckCompletion(INotifyValueChanged control) + { + control.UnregisterValueChangedCallback(CheckCompletionCallback); + } + + private void CheckCompletionCallback(ChangeEvent callback) + { + CheckCompletion(); + } + + public void RegisterCheckCompletion(Button button) + { + button.clicked += CheckCompletion; } public void RegisterLoadChange(BaseField element, EditorContainer editorContainer) @@ -257,6 +279,11 @@ public void RegisterLoadChange(ObjectField element, EditorContainer< element.value = editorContainer.Value; }; } + + public void RegisterLoadChange(System.Action onLoad) + { + this.onLoad += onLoad; + } } public class CompleteCriteria diff --git a/Assets/RicUtils/Runtime/Scripts/Managers/SingletonCreation.cs b/Assets/RicUtils/Runtime/Scripts/Managers/SingletonCreation.cs index 4cbc094..b5f1f75 100644 --- a/Assets/RicUtils/Runtime/Scripts/Managers/SingletonCreation.cs +++ b/Assets/RicUtils/Runtime/Scripts/Managers/SingletonCreation.cs @@ -1,3 +1,4 @@ +using RicUtils.ScriptableObjects; using RicUtils.Settings; using RicUtils.Utilities; using System.Reflection; @@ -10,6 +11,7 @@ internal static class SingletonCreation [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)] private static void OnLoad() { + CreateManager(typeof(TimerManager)); foreach (var singletonManager in RicUtils_RuntimeSettings.singletonManagers) { var type = singletonManager.manager.Type; @@ -18,14 +20,19 @@ private static void OnLoad() Debug.LogWarning($"Could not find type: {singletonManager.manager.TypeNameAndAssembly}"); continue; } - var method = type.GetMethod("CreateManager", BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.InvokeMethod | BindingFlags.FlattenHierarchy); - var manager = method.Invoke(null, new object[] { }); - if (RicUtilities.IsSubclassOfRawGeneric(typeof(DataGenericManager<,>), type)) - { - type.GetField("data", BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy).SetValue(manager, singletonManager.data); - } - type.GetMethod("OnCreation", BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.InvokeMethod | BindingFlags.FlattenHierarchy).Invoke(manager, new object[] { }); + CreateManager(type, singletonManager.data); + } + } + + private static void CreateManager(System.Type type, DataManagerScriptableObject data=null) + { + var method = type.GetMethod("CreateManager", BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.InvokeMethod | BindingFlags.FlattenHierarchy); + var manager = method.Invoke(null, new object[] { }); + if (RicUtilities.IsSubclassOfRawGeneric(typeof(DataGenericManager<,>), type)) + { + type.GetField("data", BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy).SetValue(manager, data); } + type.GetMethod("OnCreation", BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.InvokeMethod | BindingFlags.FlattenHierarchy).Invoke(manager, new object[] { }); } } } diff --git a/Assets/RicUtils/Runtime/Scripts/Managers/TimerManager.cs b/Assets/RicUtils/Runtime/Scripts/Managers/TimerManager.cs new file mode 100644 index 0000000..a52b5e3 --- /dev/null +++ b/Assets/RicUtils/Runtime/Scripts/Managers/TimerManager.cs @@ -0,0 +1,35 @@ +using RicUtils.Managers; +using RicUtils.Utilities; +using System.Collections; +using System.Collections.Generic; +using UnityEngine; +using UnityEngine.SceneManagement; + +namespace RicUtils +{ + public class TimerManager : SingletonGenericManager + { + private List timers = new List(); + + public GameTimer CreateTimer(float timer, bool repeat = true) + { + GameTimer gameTimer = new GameTimer(timer, repeat); + + timers.Add(gameTimer); + + return gameTimer; + } + + private void Update() + { + timers.RemoveAll(g => g == null); + + foreach(var timer in timers) + { + timer.Update(); + } + + timers.RemoveAll(g => g.ended); + } + } +} diff --git a/Assets/RicUtils/Runtime/Scripts/Managers/TimerManager.cs.meta b/Assets/RicUtils/Runtime/Scripts/Managers/TimerManager.cs.meta new file mode 100644 index 0000000..338e53b --- /dev/null +++ b/Assets/RicUtils/Runtime/Scripts/Managers/TimerManager.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 3326e56cd2e477e46916986812c616fd +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/RicUtils/Runtime/Scripts/Utilities/GameTimer.cs b/Assets/RicUtils/Runtime/Scripts/Utilities/GameTimer.cs new file mode 100644 index 0000000..21ff50b --- /dev/null +++ b/Assets/RicUtils/Runtime/Scripts/Utilities/GameTimer.cs @@ -0,0 +1,63 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +namespace RicUtils.Utilities +{ + public sealed class GameTimer + { + public bool Repeat { get; set; } + + public float Timer { get; set; } + + internal bool ended; + + private bool started; + + private float time; + + public bool Paused => !started; + + public event System.Action onTick; + + internal GameTimer(float timer, bool repeat) + { + Timer = timer; + Repeat = repeat; + } + + internal void Update() + { + if(ended || !started) return; + + time += Time.deltaTime; + if(time >= Timer) + { + onTick?.Invoke(); + if(!Repeat) ended = true; + time -= Timer; + } + } + + public void Start() + { + started = true; + time = 0; + } + + public void Pause() + { + started = false; + } + + public void Unpause() + { + started = true; + } + + public void Remove() + { + ended = true; + } + } +} diff --git a/Assets/RicUtils/Runtime/Scripts/Utilities/GameTimer.cs.meta b/Assets/RicUtils/Runtime/Scripts/Utilities/GameTimer.cs.meta new file mode 100644 index 0000000..4b91be9 --- /dev/null +++ b/Assets/RicUtils/Runtime/Scripts/Utilities/GameTimer.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 64fdff21be28933428f7360857439a1e +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/RicUtils/Runtime/Scripts/Utilities/RicUtilities.cs b/Assets/RicUtils/Runtime/Scripts/Utilities/RicUtilities.cs index ac7c94a..dc9be80 100644 --- a/Assets/RicUtils/Runtime/Scripts/Utilities/RicUtilities.cs +++ b/Assets/RicUtils/Runtime/Scripts/Utilities/RicUtilities.cs @@ -2,6 +2,9 @@ using System.IO; using System.Text.RegularExpressions; using UnityEditor; +using System.Collections; +using System.Collections.Generic; +using System.Linq; using UnityEngine; namespace RicUtils.Utilities @@ -85,5 +88,14 @@ public static void CreateAssetFolder(string folderPath) } } #endif + public static T GetRandomElement(this IEnumerable array) + { + return array.ElementAt(Random.Range(0, array.Count())); + } + + public static T GetRandomElement(this System.Array array) + { + return (T)array.GetValue(Random.Range(0, array.Length)); + } } } diff --git a/Assets/RicUtils/package.json b/Assets/RicUtils/package.json index 59b2a5a..d466e9b 100644 --- a/Assets/RicUtils/package.json +++ b/Assets/RicUtils/package.json @@ -1,6 +1,6 @@ { "name": "io.github.app24.ricutils", - "version": "1.6.0", + "version": "1.6.1", "displayName": "RicUtils", "dependencies": { "com.solidalloy.type-references": "2.16.0"