Skip to content

Commit

Permalink
Added auto-save
Browse files Browse the repository at this point in the history
  • Loading branch information
GerardGascon authored Oct 10, 2022
1 parent 0997367 commit 8857a11
Show file tree
Hide file tree
Showing 8 changed files with 206 additions and 21 deletions.
31 changes: 18 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

This package contains simple tools to use in your project.

This package will be updated once I find another useful tool or someone suggest me one.

## Features

- **AudioManager** with Play, Pause and most of the other basic things, as well as some effects like FadeIn or FadeOut.
Expand All @@ -12,6 +10,8 @@ This package will be updated once I find another useful tool or someone suggest
- Basic menu with **music and SFX sliders** as well as **resolution and quality dropdowns.**
- An **object pooler** with the ability to create pools with an undetermined size.
- A basic **scene manager** with a loading screen with progress bar.
- A simple **timer** that allows you to easily create clocks, countdowns and stopwatches with TextMeshPro
- An **auto-save** feature to reduce the chances of loosing data on crashes.

All of that comes with some editor menu items for creating all of that as fast as possible.

Expand All @@ -33,7 +33,7 @@ Download latest package from the Release section Import SimpleTools.unitypackage

## Usage

### AudioManager
### **AudioManager**

```csharp
using SimpleTools.AudioManager;
Expand All @@ -42,6 +42,7 @@ AudioManager.instance.Play("Name"); //Plays the sound with that name
AudioManager.instance.Play("Name", 1f); //Starts playing the sound "Name" in 1 second
AudioManager.instance.PlayOneShot("Name"); //Plays one shot of that sound (Useful for repeated sounds)
AudioManager.instance.PlayWithIntro("Intro", "Loop"); //Plays the intro and then the loop
AudioManager.instance.PlayRandomSound("Name1", "Name2", "Name3"); // Plays one shot of a random sound
AudioManager.instance.Pause("Name"); //Pauses the sound
AudioManager.instance.UnPause("Name"); //Unpauses the sound
Expand All @@ -59,7 +60,7 @@ AudioManager.instance.FadeMutedIn("Name", 1f); //Fade In a muted sound with a sp
AudioManager.instance.FadeMutedOut("Name", 1f); //Fade Out a sound without stopping it
```

### ObjectPooler
### **ObjectPooler**

The SpawnFromPool function always return a GameObject

Expand All @@ -80,21 +81,21 @@ Pooler.SpawnFromPool("Name", Vector3.zero, transform, true); //Spawn into a spec
Pooler.SpawnFromPool("Name", Vector3.zero, Quaternion.identity, transform, true); //Spawn into a specific position, rotation, parent and instantiate in worldSpace or not
```

### Dialogue System
### **Dialogue System**

The Dialogue function returns a bool (true if it's talking, false if it has ended)

```csharp
using SimpleTools.DialogueSystem;

Dialogue dialogue; //The dialogue scriptable object goes here
DialogueSystem.instance.Dialogue(dialogue); //Start/Continue the dialogue
DialogueSystem.instance.Dialogue(dialogue, "Sound1", "Sound2"); //Start/Continue the dialogue with a random set of sounds for the text reveal
DialogueManager.instance.Dialogue(dialogue); //Start/Continue the dialogue
DialogueManager.instance.Dialogue(dialogue, "Sound1", "Sound2"); //Start/Continue the dialogue with a random set of sounds for the text reveal
```

Text commands:

```html
```
<color=color></color> --> Sets font color within tags
<size=percentage></size> --> Sets font size within tags
<sprite=index> --> Draws a sprite from the TextMeshPro
Expand All @@ -106,7 +107,7 @@ Text commands:
<playmsc:name,time> --> Fades a music in
```

### SceneManager
### **SceneManager**

```csharp
using SimpleTools.SceneManagement;
Expand All @@ -115,15 +116,15 @@ Loader.Load(0); //Loads a scene with a specific build index
Loader.Load("Scene"); //Loads a scene with a specific name
```

### ScreenShake
### **ScreenShake**

```csharp
using SimpleTools.Cinemachine;

ScreenShake.Shake(1f, .25f); //Shakes the camera with an intensity and duration
```

### Timer
### **Timer**

```csharp
using SimpleTools.Timer;
Expand All @@ -141,8 +142,12 @@ timer.ResetTimer(); //Pause and sets the time to the default one
timer.Restart(); //Restarts the timer
```

### Editor
### **Auto-save**

To enable auto-save you have access the menu from the top bar, *Simple Tools>Auto Save Configuration.* You can auto-save every several minutes or auto-save every time you enter play mode.

### **Editor**

You can easily set up some things by right clicking in your Project Tab and then selecting Tools and clicking on the one you want to create.

Also you can right click in the Hierarchy for easily creating some GameObjects with the Tools in it.
Also you can right click in the Hierarchy for easily creating some GameObjects with the Tools in it.
21 changes: 21 additions & 0 deletions Tools/AudioManager/AudioManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ void Awake() {
/// <summary>Use this to play a sound with a specific name
/// <para>It has to be in the Sound asset referenced in the AudioManager instance</para>
/// </summary>
/// <param name="name" type="string">The name of the sound</param>
public void Play(string name) {
Sounds.List s = Array.Find(soundList.sounds, sound => sound.name == name);
if (s == null) {
Expand All @@ -60,6 +61,8 @@ public void Play(string name) {
/// <summary>Use this to play a sound with a specific name and with a certain delay
/// <para>It has to be in the Sound asset referenced in the AudioManager instance</para>
/// </summary>
/// <param name="name" type="string">The name of the sound</param>
/// <param name="delay" type="float">The delay in seconds</param>
public void Play(string name, float delay) {
Sounds.List s = Array.Find(soundList.sounds, sound => sound.name == name);
if (s == null) {
Expand All @@ -73,6 +76,7 @@ public void Play(string name, float delay) {
/// <summary>Use this to play one shot of a sound with a specific name
/// <para>It has to be in the Sound asset referenced in the AudioManager instance</para>
/// </summary>
/// <param name="name" type="string">The name of the sound</param>
public void PlayOneShot(string name) {
Sounds.List s = Array.Find(soundList.sounds, sound => sound.name == name);
if (s == null) {
Expand All @@ -86,6 +90,8 @@ public void PlayOneShot(string name) {
/// <summary>Use this to play an intro song and then start playing the song loop
/// <para>It has to be in the Sound asset referenced in the AudioManager instance</para>
/// </summary>
/// <param name="intro" type="string">The name of the intro song</param>
/// <param name="song" type="string">The name of the song loop</param>
public void PlayWithIntro(string intro, string song) {
Sounds.List s = Array.Find(soundList.sounds, sound => sound.name == intro);
if (s == null) {
Expand All @@ -102,6 +108,7 @@ public void PlayWithIntro(string intro, string song) {
/// <summary>Use this to play one shot of a random sound within a list
/// <para>They have to be in the Sound asset referenced in the AudioManager instance</para>
/// </summary>
/// <param name="names" type="string[]">The names of the sounds</param>
public void PlayRandomSound(params string[] names) {
int random = UnityEngine.Random.Range(0, names.Length);
PlayOneShot(names[random]);
Expand All @@ -111,6 +118,7 @@ public void PlayRandomSound(params string[] names) {
/// <summary>Use this to pause a sound with a specific name
/// <para>It has to be in the Sound asset referenced in the AudioManager instance</para>
/// </summary>
/// <param name="name" type="string">The name of the sound</param>
public void Pause(string name) {
Sounds.List s = Array.Find(soundList.sounds, sound => sound.name == name);
if (s == null) {
Expand All @@ -124,6 +132,7 @@ public void Pause(string name) {
/// <summary>Use this to unpause a sound with a specific name
/// <para>It has to be in the Sound asset referenced in the AudioManager instance</para>
/// </summary>
/// <param name="name" type="string">The name of the sound</param>
public void UnPause(string name) {
Sounds.List s = Array.Find(soundList.sounds, sound => sound.name == name);
if (s == null) {
Expand All @@ -139,6 +148,7 @@ public void UnPause(string name) {
/// <summary>Use this to stop a sound with a specific name
/// <para>It has to be in the Sound asset referenced in the AudioManager instance</para>
/// </summary>
/// <param name="name" type="string">The name of the sound</param>
public void Stop(string name) {
Sounds.List s = Array.Find(soundList.sounds, sound => sound.name == name);
if (s == null) {
Expand All @@ -163,6 +173,8 @@ public void StopAll() {
/// <summary>This function returns the AudioSource that contains a specific sound
/// <para>It has to be in the Sound asset referenced in the AudioManager instance</para>
/// </summary>
/// <param name="name">The name of the sound</param>
/// <returns>The AudioSource in the scene</returns>
public AudioSource GetSource(string name) {
Sounds.List s = Array.Find(soundList.sounds, sound => sound.name == name);
if (s == null) {
Expand All @@ -175,6 +187,8 @@ public AudioSource GetSource(string name) {
/// <summary>Use this to start playing a sound with a fade in
/// <para>It has to be in the Sound asset referenced in the AudioManager instance</para>
/// </summary>
/// <param name="name" type="string">The name of the sound</param>
/// <param name="duration" type="float">The duration of the fade in</param>
public void FadeIn(string name, float duration) {
StartCoroutine(FadeInCoroutine(name, duration));
}
Expand All @@ -195,6 +209,8 @@ IEnumerator FadeInCoroutine(string name, float fadeTime) {
/// <summary>Use this to stop playing a sound with a fade out
/// <para>It has to be in the Sound asset referenced in the AudioManager instance</para>
/// </summary>
/// <param name="name" type="string">The name of the sound</param>
/// <param name="duration" type="float">The duration of the fade out</param>
public void FadeOut(string name, float duration) {
StartCoroutine(FadeOutCoroutine(name, duration));
}
Expand All @@ -217,6 +233,7 @@ IEnumerator FadeOutCoroutine(string name, float fadeTime) {
/// <summary>Use this to start playing a sound muted
/// <para>It has to be in the Sound asset referenced in the AudioManager instance</para>
/// </summary>
/// <param name="name" type="string">The name of the sound</param>
public void PlayMuted(string name) {
Sounds.List s = Array.Find(soundList.sounds, sound => sound.name == name);
if (s == null) {
Expand All @@ -231,6 +248,8 @@ public void PlayMuted(string name) {
/// <para>It has to be in the Sound asset referenced in the AudioManager instance</para>
/// <para>WARNING: If the PlayMuted hasn't been called before, this function won't work</para>
/// </summary>
/// <param name="name" type="string">The name of the sound</param>
/// <param name="duration">The duration of the fade in</param>
public void FadeMutedIn(string name, float duration) {
StartCoroutine(FadeMutedInCoroutine(name, duration));
}
Expand All @@ -250,6 +269,8 @@ IEnumerator FadeMutedInCoroutine(string name, float fadeTime) {
/// <summary>Use this to fade out a sound and keep playing that muted
/// <para>It has to be in the Sound asset referenced in the AudioManager instance</para>
/// </summary>
/// <param name="name" type="string">The name of the sound</param>
/// <param name="duration">The duration of the fade out</param>
public void FadeMutedOut(string name, float duration) {
StartCoroutine(FadeMutedOutCoroutine(name, duration));
}
Expand Down
8 changes: 8 additions & 0 deletions Tools/AutoSave.meta

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

124 changes: 124 additions & 0 deletions Tools/AutoSave/AutoSaveConfig.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
#if UNITY_EDITOR
using UnityEngine;
using UnityEditor;
using System;
using System.Threading;
using UnityEditor.SceneManagement;
using System.Threading.Tasks;

namespace SimpleTools.AutoSave {
public class AutoSaveConfig : EditorWindow {

[MenuItem("Simple Tools/Auto Save Configuration")]
public static void ShowWindow(){
EditorWindow w = GetWindow<AutoSaveConfig>("Auto-save Configuration");
w.position = new Rect(w.position.position, new Vector2(400, 150));
var data = EditorPrefs.GetString("AutoSave", JsonUtility.ToJson(w, false));
JsonUtility.FromJsonOverwrite(data, w);
}

[InitializeOnLoadMethod]
static void OnInitialize(){
int _index = EditorPrefs.GetInt("Index", 0);
bool _logging = EditorPrefs.GetBool("Logging", false);
ChangeAutoSaveMode(_index, _logging);
}

protected void OnEnable() {
OnInitialize();
}

protected void OnDisable() {
var data = JsonUtility.ToJson(this, false);
EditorPrefs.SetString("AutoSave", data);
EditorPrefs.SetInt("Index", index);
EditorPrefs.SetBool("Logging", logging);
}

readonly static string[] options = new string[] { "Disabled", "On Play", "1 Minute", "10 Minutes", "1 Hour" };
public static int index;
public static bool enabled;
public static bool logging;

void OnGUI() {
GUILayout.Label("Select auto-save mode:", EditorStyles.boldLabel);
int i = EditorGUILayout.Popup(index, options);
if (i != index) ChangeAutoSaveMode(i, logging);

GUILayout.Label("Log a message every time a the scene gets saved.");
if (logging) {
if (GUILayout.Button("Disable Logging")){
logging ^= true;
ChangeAutoSaveMode(i, logging);
}

} else {
if (GUILayout.Button("Enable Logging")) {
logging ^= true;
ChangeAutoSaveMode(i, logging);
}
}
}

static CancellationTokenSource _tokenSource;
static Task _task;
static int frequency;
static void ChangeAutoSaveMode(int mode, bool log){
index = mode;
logging = log;
CancelTask();
enabled = true;
EditorApplication.playModeStateChanged -= AutoSaveWhenPlayModeStarts;

switch(index){
case 0:
enabled = false;
return;
case 1:
EditorApplication.playModeStateChanged += AutoSaveWhenPlayModeStarts;
return;
case 2:
frequency = 1 * 60 * 1000;
break;
case 3:
frequency = 10 * 60 * 1000;
break;
case 4:
frequency = 60 * 60 * 1000;
break;
}

_tokenSource = new CancellationTokenSource();
_task = SaveInterval(_tokenSource.Token);
}

static void AutoSaveWhenPlayModeStarts(PlayModeStateChange state){
if(state == PlayModeStateChange.ExitingEditMode){
EditorSceneManager.SaveOpenScenes();
AssetDatabase.SaveAssets();
if (logging) Debug.Log($"Auto-saved at {DateTime.Now:h:mm:ss tt}");
}
}

static void CancelTask() {
if (_task == null) return;
_tokenSource.Cancel();
}

static async Task SaveInterval(CancellationToken token) {
while (!token.IsCancellationRequested) {
await Task.Delay(frequency, token);

if (token.IsCancellationRequested) break;

if (!enabled || Application.isPlaying || BuildPipeline.isBuildingPlayer || EditorApplication.isCompiling) return;
if (!UnityEditorInternal.InternalEditorUtility.isApplicationActive) return;

EditorSceneManager.SaveOpenScenes();
AssetDatabase.SaveAssets();
if (logging) Debug.Log($"Auto-saved at {DateTime.Now:h:mm:ss tt}");
}
}
}
}
#endif
11 changes: 11 additions & 0 deletions Tools/AutoSave/AutoSaveConfig.cs.meta

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

Loading

0 comments on commit 8857a11

Please sign in to comment.