20 changed files with 447 additions and 9 deletions
Binary file not shown.
Binary file not shown.
@ -0,0 +1,130 @@ |
|||||
|
using System; |
||||
|
using System.Collections.Generic; |
||||
|
using System.Linq; |
||||
|
using UnityEngine; |
||||
|
|
||||
|
namespace Core.Tools.Saves |
||||
|
{ |
||||
|
internal sealed class SaveData |
||||
|
{ |
||||
|
[Serializable] |
||||
|
private class IntData |
||||
|
{ |
||||
|
public string key = ""; |
||||
|
public int value = 0; |
||||
|
} |
||||
|
|
||||
|
[Serializable] |
||||
|
private class FloatData |
||||
|
{ |
||||
|
public string key = ""; |
||||
|
public float value = 0f; |
||||
|
} |
||||
|
|
||||
|
[Serializable] |
||||
|
private class StringData |
||||
|
{ |
||||
|
public string key = ""; |
||||
|
public string value = ""; |
||||
|
} |
||||
|
|
||||
|
[Serializable] |
||||
|
private class BoolData |
||||
|
{ |
||||
|
public string key = ""; |
||||
|
public bool value = false; |
||||
|
} |
||||
|
|
||||
|
[SerializeField] private List<IntData> _intData = new List<IntData>(); |
||||
|
|
||||
|
[SerializeField] private List<FloatData> _floatData = new List<FloatData>(); |
||||
|
|
||||
|
[SerializeField] private List<StringData> _stringData = new List<StringData>(); |
||||
|
|
||||
|
[SerializeField] private List<BoolData> _boolData = new List<BoolData>(); |
||||
|
|
||||
|
public bool HasIntKey(string key) => |
||||
|
_intData.Where(d => d.key == key).Count() > 0; |
||||
|
|
||||
|
public void SetInt(string key, int value) |
||||
|
{ |
||||
|
if (HasIntKey(key)) |
||||
|
_intData.Where(d => d.key == key).First().value = value; |
||||
|
else |
||||
|
_intData.Add(new IntData { key = key, value = value }); |
||||
|
|
||||
|
SaveManager.Save(); |
||||
|
} |
||||
|
|
||||
|
public int GetInt(string key, int defaultValue = 0) |
||||
|
{ |
||||
|
if (HasIntKey(key)) |
||||
|
return _intData.Where(d => d.key == key).First().value; |
||||
|
else |
||||
|
return defaultValue; |
||||
|
} |
||||
|
|
||||
|
public bool HasFloatKey(string key) => |
||||
|
_floatData.Where(d => d.key == key).Count() > 0; |
||||
|
|
||||
|
public void SetFloat(string key, float value) |
||||
|
{ |
||||
|
if (HasFloatKey(key)) |
||||
|
_floatData.Where(d => d.key == key).First().value = value; |
||||
|
else |
||||
|
_floatData.Add(new FloatData { key = key, value = value }); |
||||
|
|
||||
|
SaveManager.Save(); |
||||
|
} |
||||
|
|
||||
|
public float GetFloat(string key, float defaultValue = 0f) |
||||
|
{ |
||||
|
if (HasFloatKey(key)) |
||||
|
return _floatData.Where(d => d.key == key).First().value; |
||||
|
else |
||||
|
return defaultValue; |
||||
|
} |
||||
|
|
||||
|
public bool HasStringKey(string key) => |
||||
|
_stringData.Where(d => d.key == key).Count() > 0; |
||||
|
|
||||
|
public void SetString(string key, string value) |
||||
|
{ |
||||
|
if (HasStringKey(key)) |
||||
|
_stringData.Where(d => d.key == key).First().value = value; |
||||
|
else |
||||
|
_stringData.Add(new StringData { key = key, value = value }); |
||||
|
|
||||
|
SaveManager.Save(); |
||||
|
} |
||||
|
|
||||
|
public string GetString(string key, string defaultValue = "") |
||||
|
{ |
||||
|
if (HasStringKey(key)) |
||||
|
return _stringData.Where(d => d.key == key).First().value; |
||||
|
else |
||||
|
return defaultValue; |
||||
|
} |
||||
|
|
||||
|
public bool HasBoolKey(string key) => |
||||
|
_boolData.Where(d => d.key == key).Count() > 0; |
||||
|
|
||||
|
public void SetBool(string key, bool value) |
||||
|
{ |
||||
|
if (HasBoolKey(key)) |
||||
|
_boolData.Where(d => d.key == key).First().value = value; |
||||
|
else |
||||
|
_boolData.Add(new BoolData { key = key, value = value }); |
||||
|
|
||||
|
SaveManager.Save(); |
||||
|
} |
||||
|
|
||||
|
public bool GetBool(string key, bool defaultValue = false) |
||||
|
{ |
||||
|
if (HasBoolKey(key)) |
||||
|
return _boolData.Where(d => d.key == key).First().value; |
||||
|
else |
||||
|
return defaultValue; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,103 @@ |
|||||
|
using Core.Tools.Saves.Utils; |
||||
|
using System.IO; |
||||
|
using UnityEngine; |
||||
|
|
||||
|
namespace Core.Tools.Saves |
||||
|
{ |
||||
|
public static class SaveManager |
||||
|
{ |
||||
|
private static SaveData _save = null; |
||||
|
|
||||
|
private const string SAVE_KEY = "{@rew_O39U*#H{EFSF"; |
||||
|
|
||||
|
public static string GetPathToSave() |
||||
|
{ |
||||
|
if (Application.isEditor || Application.isMobilePlatform) |
||||
|
return Application.persistentDataPath + "/save.dat"; |
||||
|
else |
||||
|
return Application.dataPath + "/save.dat"; |
||||
|
} |
||||
|
|
||||
|
public static void Load() |
||||
|
{ |
||||
|
string path = GetPathToSave(); |
||||
|
|
||||
|
if (!File.Exists(path)) |
||||
|
{ |
||||
|
_save = new SaveData(); |
||||
|
Save(); |
||||
|
} |
||||
|
else |
||||
|
{ |
||||
|
StreamReader stream = new StreamReader(path); |
||||
|
|
||||
|
string json = Encrypter.Decrypt(stream.ReadToEnd(), SAVE_KEY); |
||||
|
|
||||
|
stream.Close(); |
||||
|
|
||||
|
_save = JsonUtility.FromJson<SaveData>(json); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public static void Save() |
||||
|
{ |
||||
|
string path = GetPathToSave(); |
||||
|
|
||||
|
string json = JsonUtility.ToJson(_save); |
||||
|
|
||||
|
StreamWriter stream; |
||||
|
|
||||
|
if (!File.Exists(path)) |
||||
|
stream = File.CreateText(path); |
||||
|
else |
||||
|
stream = new StreamWriter(path); |
||||
|
|
||||
|
stream.Write(Encrypter.Encrypt(json, SAVE_KEY)); |
||||
|
stream.Close(); |
||||
|
} |
||||
|
|
||||
|
public static void Clear() |
||||
|
{ |
||||
|
string path = GetPathToSave(); |
||||
|
|
||||
|
if (File.Exists(path)) |
||||
|
File.Delete(path); |
||||
|
} |
||||
|
|
||||
|
public static bool HasIntKey(string key) => |
||||
|
_save.HasIntKey(key); |
||||
|
|
||||
|
public static void SetInt(string key, int value) => |
||||
|
_save.SetInt(key, value); |
||||
|
|
||||
|
public static int GetInt(string key, int defaultValue = 0) => |
||||
|
_save.GetInt(key, defaultValue); |
||||
|
|
||||
|
public static bool HasFloatKey(string key) => |
||||
|
_save.HasFloatKey(key); |
||||
|
|
||||
|
public static void SetFloat(string key, float value) => |
||||
|
_save.SetFloat(key, value); |
||||
|
|
||||
|
public static float GetFloat(string key, float defaultValue = 0f) => |
||||
|
_save.GetFloat(key, defaultValue); |
||||
|
|
||||
|
public static bool HasStringKey(string key) => |
||||
|
_save.HasStringKey(key); |
||||
|
|
||||
|
public static void SetString(string key, string value) => |
||||
|
_save.SetString(key, value); |
||||
|
|
||||
|
public static string GetString(string key, string defaultValue = "") => |
||||
|
_save.GetString(key, defaultValue); |
||||
|
|
||||
|
public static bool HasBoolKey(string key) => |
||||
|
_save.HasBoolKey(key); |
||||
|
|
||||
|
public static void SetBool(string key, bool value) => |
||||
|
_save.SetBool(key, value); |
||||
|
|
||||
|
public static bool GetBool(string key, bool defaultValue = false) => |
||||
|
_save.GetBool(key, defaultValue); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,53 @@ |
|||||
|
using System; |
||||
|
using System.IO; |
||||
|
using System.Security.Cryptography; |
||||
|
using System.Text; |
||||
|
|
||||
|
namespace Core.Tools.Saves.Utils |
||||
|
{ |
||||
|
public static class Encrypter |
||||
|
{ |
||||
|
public static string Encrypt(string text, string key) |
||||
|
{ |
||||
|
byte[] clearBytes = Encoding.Unicode.GetBytes(text); |
||||
|
using (Aes encryptor = Aes.Create()) |
||||
|
{ |
||||
|
Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(key, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 }); |
||||
|
encryptor.Key = pdb.GetBytes(32); |
||||
|
encryptor.IV = pdb.GetBytes(16); |
||||
|
using (MemoryStream ms = new MemoryStream()) |
||||
|
{ |
||||
|
using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateEncryptor(), CryptoStreamMode.Write)) |
||||
|
{ |
||||
|
cs.Write(clearBytes, 0, clearBytes.Length); |
||||
|
cs.Close(); |
||||
|
} |
||||
|
text = Convert.ToBase64String(ms.ToArray()); |
||||
|
} |
||||
|
} |
||||
|
return text; |
||||
|
} |
||||
|
|
||||
|
public static string Decrypt(string text, string key) |
||||
|
{ |
||||
|
text = text.Replace(" ", "+"); |
||||
|
byte[] cipherBytes = Convert.FromBase64String(text); |
||||
|
using (Aes encryptor = Aes.Create()) |
||||
|
{ |
||||
|
Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(key, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 }); |
||||
|
encryptor.Key = pdb.GetBytes(32); |
||||
|
encryptor.IV = pdb.GetBytes(16); |
||||
|
using (MemoryStream ms = new MemoryStream()) |
||||
|
{ |
||||
|
using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateDecryptor(), CryptoStreamMode.Write)) |
||||
|
{ |
||||
|
cs.Write(cipherBytes, 0, cipherBytes.Length); |
||||
|
cs.Close(); |
||||
|
} |
||||
|
text = Encoding.Unicode.GetString(ms.ToArray()); |
||||
|
} |
||||
|
} |
||||
|
return text; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,78 @@ |
|||||
|
using UnityEngine; |
||||
|
using UnityEngine.Rendering; |
||||
|
|
||||
|
namespace Core.Tools.Sprites |
||||
|
{ |
||||
|
[ExecuteInEditMode] |
||||
|
public class DynamicSpriteSorting : MonoBehaviour |
||||
|
{ |
||||
|
public enum TargetRenderer |
||||
|
{ |
||||
|
SpriteRenderer = 0, |
||||
|
SortingGroup = 1, |
||||
|
} |
||||
|
|
||||
|
[SerializeField] private TargetRenderer _targetRenderer = TargetRenderer.SpriteRenderer; |
||||
|
[SerializeField] private SpriteRenderer _renderer = null; |
||||
|
[SerializeField] private SortingGroup _group = null; |
||||
|
[SerializeField] private float _offsetY = 0f; |
||||
|
|
||||
|
private float _lastPos = 0f; |
||||
|
private float _lastOffsetY = 0f; |
||||
|
private TargetRenderer _lastTargetRenderer = 0f; |
||||
|
|
||||
|
private void Reset() |
||||
|
{ |
||||
|
_renderer = GetComponent<SpriteRenderer>(); |
||||
|
_group = GetComponent<SortingGroup>(); |
||||
|
UpdateOrder(); |
||||
|
} |
||||
|
|
||||
|
private void Awake() |
||||
|
{ |
||||
|
_lastPos = transform.position.y + _offsetY; |
||||
|
UpdateOrder(); |
||||
|
} |
||||
|
|
||||
|
private void Update() |
||||
|
{ |
||||
|
float pos = transform.position.y + _offsetY; |
||||
|
|
||||
|
if (pos != _lastPos || _lastOffsetY != _offsetY || _lastTargetRenderer != _targetRenderer) |
||||
|
{ |
||||
|
_lastPos = pos; |
||||
|
_lastOffsetY = _offsetY; |
||||
|
_lastTargetRenderer = _targetRenderer; |
||||
|
|
||||
|
UpdateOrder(); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
private void UpdateOrder() |
||||
|
{ |
||||
|
float pos = transform.position.y + _offsetY; |
||||
|
|
||||
|
if (_targetRenderer == TargetRenderer.SpriteRenderer) |
||||
|
{ |
||||
|
if (_renderer != null) |
||||
|
_renderer.sortingOrder = (int)(-pos * 100f); |
||||
|
} |
||||
|
else |
||||
|
{ |
||||
|
if (_group != null) |
||||
|
_group.sortingOrder = (int)(-pos * 100f); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
private void OnDrawGizmosSelected() |
||||
|
{ |
||||
|
Vector3 position = transform.position + (Vector3.up * _offsetY); |
||||
|
|
||||
|
Gizmos.color = new Color(1f, 1f, 1f, 1f); |
||||
|
Gizmos.DrawSphere(position, 0.15f); |
||||
|
|
||||
|
Gizmos.color = new Color(1f, 0f, 1f, 1f); |
||||
|
Gizmos.DrawSphere(position, 0.1f); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
Binary file not shown.
@ -1 +1 @@ |
|||||
d1c5eb68f3ce5b18f962b2e20f3968ce352e152d |
049145f5314f464185eb4974b4434a906bbba395 |
||||
|
|||||
Binary file not shown.
@ -0,0 +1,17 @@ |
|||||
|
using Core.Tools.Saves; |
||||
|
using UnityEditor; |
||||
|
using UnityEngine; |
||||
|
|
||||
|
namespace CoreEditor.Tools |
||||
|
{ |
||||
|
internal static class SaveManagerEditor |
||||
|
{ |
||||
|
[MenuItem("Prototype/Show path to save", false, 100)] |
||||
|
private static void ShowPathToSave() => |
||||
|
Debug.Log("Path to save: " + SaveManager.GetPathToSave()); |
||||
|
|
||||
|
[MenuItem("Prototype/Clear Save", false, 101)] |
||||
|
private static void ClearSave() => |
||||
|
SaveManager.Clear(); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,36 @@ |
|||||
|
using Core.Tools.Sprites; |
||||
|
using UnityEditor; |
||||
|
using UnityEngine; |
||||
|
using UnityEngine.Rendering; |
||||
|
|
||||
|
using static Core.Tools.Sprites.DynamicSpriteSorting; |
||||
|
|
||||
|
namespace CoreEditor.Tools.Sprites |
||||
|
{ |
||||
|
[CustomEditor(typeof(DynamicSpriteSorting))] |
||||
|
public class DynamicSpriteSortingEditor : Editor |
||||
|
{ |
||||
|
public override void OnInspectorGUI() |
||||
|
{ |
||||
|
serializedObject.Update(); |
||||
|
|
||||
|
SerializedProperty targetRenderer = serializedObject.FindProperty("_targetRenderer"); |
||||
|
SerializedProperty renderer = serializedObject.FindProperty("_renderer"); |
||||
|
SerializedProperty group = serializedObject.FindProperty("_group"); |
||||
|
SerializedProperty offsetY = serializedObject.FindProperty("_offsetY"); |
||||
|
|
||||
|
targetRenderer.enumValueIndex = EditorGUILayout.Popup("Target renderer:", targetRenderer.enumValueIndex, targetRenderer.enumNames); |
||||
|
|
||||
|
if (targetRenderer.enumValueIndex == (int)TargetRenderer.SpriteRenderer) |
||||
|
renderer.objectReferenceValue = (SpriteRenderer)EditorGUILayout.ObjectField("Sprite renderer:", (SpriteRenderer)renderer.objectReferenceValue, typeof(SpriteRenderer), true); |
||||
|
else |
||||
|
group.objectReferenceValue = (SortingGroup)EditorGUILayout.ObjectField("Sorting group:", (SortingGroup)group.objectReferenceValue, typeof(SortingGroup), true); |
||||
|
|
||||
|
EditorGUILayout.Space(2); |
||||
|
|
||||
|
offsetY.floatValue = EditorGUILayout.FloatField("Offset Y:", offsetY.floatValue); |
||||
|
|
||||
|
serializedObject.ApplyModifiedProperties(); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
Binary file not shown.
@ -1 +1 @@ |
|||||
303f7c18877e6d0c94d1ea8312a22f962649285d |
c0285597973193b3af84a2311133c085240ec7ab |
||||
|
|||||
Binary file not shown.
Binary file not shown.
Loading…
Reference in new issue