Skip to content

Commit

Permalink
Timer manager, function to easily get icon from sprite
Browse files Browse the repository at this point in the history
  • Loading branch information
App24 committed Jun 20, 2023
1 parent 763d530 commit ab7c3c1
Show file tree
Hide file tree
Showing 11 changed files with 192 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion Assets/RicUtils/Editor/Settings/RicUtils_EditorSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions Assets/RicUtils/Editor/Utilities/ToolUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
37 changes: 32 additions & 5 deletions Assets/RicUtils/Editor/Windows/GenericEditorWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,25 @@ public abstract class GenericEditorWindow<GenericSoType, AvailableSoType> : Edit

private System.Action onLoad;

public new VisualElement rootVisualElement => _container;

private VisualElement _container;

protected virtual void OnEnable()
{
serializedObject = new SerializedObject(this);
}

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", () =>
{
Expand Down Expand Up @@ -228,10 +238,22 @@ private void CheckCompletion()

public void RegisterCheckCompletion<TValueType>(INotifyValueChanged<TValueType> control)
{
control.RegisterValueChangedCallback(callback =>
{
CheckCompletion();
});
control.RegisterValueChangedCallback(CheckCompletionCallback);
}

public void UnregisterCheckCompletion<TValueType>(INotifyValueChanged<TValueType> control)
{
control.UnregisterValueChangedCallback(CheckCompletionCallback);
}

private void CheckCompletionCallback<TValueType>(ChangeEvent<TValueType> callback)
{
CheckCompletion();
}

public void RegisterCheckCompletion(Button button)
{
button.clicked += CheckCompletion;
}

public void RegisterLoadChange<TValueType>(BaseField<TValueType> element, EditorContainer<TValueType> editorContainer)
Expand All @@ -257,6 +279,11 @@ public void RegisterLoadChange<TValueType>(ObjectField element, EditorContainer<
element.value = editorContainer.Value;
};
}

public void RegisterLoadChange(System.Action onLoad)
{
this.onLoad += onLoad;
}
}

public class CompleteCriteria
Expand Down
21 changes: 14 additions & 7 deletions Assets/RicUtils/Runtime/Scripts/Managers/SingletonCreation.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using RicUtils.ScriptableObjects;
using RicUtils.Settings;
using RicUtils.Utilities;
using System.Reflection;
Expand All @@ -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;
Expand All @@ -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[] { });
}
}
}
35 changes: 35 additions & 0 deletions Assets/RicUtils/Runtime/Scripts/Managers/TimerManager.cs
Original file line number Diff line number Diff line change
@@ -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<TimerManager>
{
private List<GameTimer> timers = new List<GameTimer>();

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);
}
}
}
11 changes: 11 additions & 0 deletions Assets/RicUtils/Runtime/Scripts/Managers/TimerManager.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

63 changes: 63 additions & 0 deletions Assets/RicUtils/Runtime/Scripts/Utilities/GameTimer.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
11 changes: 11 additions & 0 deletions Assets/RicUtils/Runtime/Scripts/Utilities/GameTimer.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions Assets/RicUtils/Runtime/Scripts/Utilities/RicUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -85,5 +88,14 @@ public static void CreateAssetFolder(string folderPath)
}
}
#endif
public static T GetRandomElement<T>(this IEnumerable<T> array)
{
return array.ElementAt(Random.Range(0, array.Count()));
}

public static T GetRandomElement<T>(this System.Array array)
{
return (T)array.GetValue(Random.Range(0, array.Length));
}
}
}
2 changes: 1 addition & 1 deletion Assets/RicUtils/package.json
Original file line number Diff line number Diff line change
@@ -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"
Expand Down

0 comments on commit ab7c3c1

Please sign in to comment.