浏览代码

Base work on Game Save Manager

/main
Thomas ICHÉ 6 年前
当前提交
6141e63c
共有 4 个文件被更改,包括 507 次插入0 次删除
  1. 264
      Resources/Default_GameSaveManager.prefab
  2. 7
      Resources/Default_GameSaveManager.prefab.meta
  3. 225
      Runtime/Managers/Implementations/GameSaveManager.cs
  4. 11
      Runtime/Managers/Implementations/GameSaveManager.cs.meta

264
Resources/Default_GameSaveManager.prefab


%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!1 &4126731527077413761
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 4126731527077413760}
- component: {fileID: 4126731527077413766}
- component: {fileID: 4126731527077413767}
m_Layer: 5
m_Name: Text
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!224 &4126731527077413760
RectTransform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 4126731527077413761}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 4126731527289085647}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 1, y: 0}
m_AnchorMax: {x: 1, y: 0}
m_AnchoredPosition: {x: -32, y: 32}
m_SizeDelta: {x: 512, y: 32}
m_Pivot: {x: 1, y: 0}
--- !u!222 &4126731527077413766
CanvasRenderer:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 4126731527077413761}
m_CullTransparentMesh: 0
--- !u!114 &4126731527077413767
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 4126731527077413761}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 708705254, guid: f70555f144d8491a825f0804e09c671c, type: 3}
m_Name:
m_EditorClassIdentifier:
m_Material: {fileID: 0}
m_Color: {r: 1, g: 1, b: 1, a: 1}
m_RaycastTarget: 1
m_OnCullStateChanged:
m_PersistentCalls:
m_Calls: []
m_TypeName: UnityEngine.UI.MaskableGraphic+CullStateChangedEvent, UnityEngine.UI,
Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
m_FontData:
m_Font: {fileID: 10102, guid: 0000000000000000e000000000000000, type: 0}
m_FontSize: 14
m_FontStyle: 0
m_BestFit: 1
m_MinSize: 1
m_MaxSize: 40
m_Alignment: 2
m_AlignByGeometry: 0
m_RichText: 1
m_HorizontalOverflow: 0
m_VerticalOverflow: 0
m_LineSpacing: 1
m_Text: Accessing Data... Do not turn off computer or quit game.
--- !u!1 &4126731527289085640
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 4126731527289085647}
- component: {fileID: 4126731527289085645}
- component: {fileID: 4126731527289085646}
m_Layer: 5
m_Name: Canvas
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 0
--- !u!224 &4126731527289085647
RectTransform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 4126731527289085640}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 0, y: 0, z: 0}
m_Children:
- {fileID: 4126731527077413760}
m_Father: {fileID: 4126731527728584135}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0, y: 0}
m_AnchorMax: {x: 0, y: 0}
m_AnchoredPosition: {x: 0, y: 0}
m_SizeDelta: {x: 0, y: 0}
m_Pivot: {x: 0, y: 0}
--- !u!223 &4126731527289085645
Canvas:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 4126731527289085640}
m_Enabled: 1
serializedVersion: 3
m_RenderMode: 0
m_Camera: {fileID: 0}
m_PlaneDistance: 100
m_PixelPerfect: 0
m_ReceivesEvents: 1
m_OverrideSorting: 0
m_OverridePixelPerfect: 0
m_SortingBucketNormalizedSize: 0
m_AdditionalShaderChannelsFlag: 0
m_SortingLayerID: 0
m_SortingOrder: 0
m_TargetDisplay: 0
--- !u!114 &4126731527289085646
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 4126731527289085640}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 1980459831, guid: f70555f144d8491a825f0804e09c671c, type: 3}
m_Name:
m_EditorClassIdentifier:
m_UiScaleMode: 0
m_ReferencePixelsPerUnit: 100
m_ScaleFactor: 1
m_ReferenceResolution: {x: 800, y: 600}
m_ScreenMatchMode: 0
m_MatchWidthOrHeight: 0
m_PhysicalUnit: 3
m_FallbackScreenDPI: 96
m_DefaultSpriteDPI: 96
m_DynamicPixelsPerUnit: 1
--- !u!1 &4126731527728584129
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 4126731527728584135}
- component: {fileID: 4126731527728584128}
- component: {fileID: 4126731527728584132}
- component: {fileID: 4126731527728584133}
- component: {fileID: 4126731527728584134}
m_Layer: 0
m_Name: Default_GameSaveManager
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!4 &4126731527728584135
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 4126731527728584129}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children:
- {fileID: 4126731527289085647}
m_Father: {fileID: 0}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &4126731527728584128
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 4126731527728584129}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 4a94c60800d516b4980b9f68a5ab6cb5, type: 3}
m_Name:
m_EditorClassIdentifier:
OnLoad:
- {fileID: 4126731527728584133}
- {fileID: 4126731527728584132}
OnSave:
- {fileID: 4126731527728584133}
- {fileID: 4126731527728584132}
--- !u!114 &4126731527728584132
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 4126731527728584129}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 0ce142628ca19c2489cf2183fc2d934f, type: 3}
m_Name:
m_EditorClassIdentifier:
Name: DelayedLogic
Delay: 3
OnDelayComplete:
- {fileID: 4126731527728584134}
OnCanceled: []
--- !u!114 &4126731527728584133
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 4126731527728584129}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 846d5f33f7af4014ea15adf7a92dc953, type: 3}
m_Name:
m_EditorClassIdentifier:
Name: ToggleGameObjectAction
Targets:
- GameObject: {fileID: 4126731527289085640}
State: 1
--- !u!114 &4126731527728584134
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 4126731527728584129}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 846d5f33f7af4014ea15adf7a92dc953, type: 3}
m_Name:
m_EditorClassIdentifier:
Name: ToggleGameObjectAction
Targets:
- GameObject: {fileID: 4126731527289085640}
State: 0

7
Resources/Default_GameSaveManager.prefab.meta


fileFormatVersion: 2
guid: 5029b31b802e7244287466485f911326
PrefabImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

225
Runtime/Managers/Implementations/GameSaveManager.cs


using NaughtyAttributes;
using System.IO;
using System.Collections.Generic;
using UnityEngine;
using System.Text;
namespace GameplayIngredients
{
[ManagerDefaultPrefab("GameSaveManager")]
public class GameSaveManager : Manager
{
private const string systemSaveName = "system.sav";
private const string userSaveName = "user{0}.sav";
Dictionary<string, System.Object> systemSaveEntries;
Dictionary<string, System.Object> currentUserSaveEntries;
[ReorderableList]
public Callable[] OnLoad;
[ReorderableList]
public Callable[] OnSave;
void Awake()
{
systemSaveEntries = new Dictionary<string, System.Object>();
currentUserSaveEntries = new Dictionary<string, System.Object>();
}
private void OnEnable()
{
// Load System Save
LoadSystemSave();
//TestFunc();
}
void TestFunc()
{
SetValue("Toto", Location.System, 1.0f);
SetValue("Tata", Location.System, 10);
SetValue("Titi", Location.System, "1.0");
SetValue("Tutu", Location.System, false);
SaveSystemSave();
}
#region SAVE/LOAD
public void LoadSystemSave()
{
systemSaveEntries = LoadFile(systemSaveName);
Callable.Call(OnLoad);
}
public void SaveSystemSave()
{
SaveFile(systemSaveName, systemSaveEntries);
Callable.Call(OnSave);
}
private byte currentUserIndex = 0;
public void LoadUserSave(byte index)
{
currentUserIndex = index;
Callable.Call(OnLoad);
currentUserSaveEntries = LoadFile(string.Format(userSaveName, index));
}
public void SaveUserSave()
{
SaveUserSave(currentUserIndex);
}
public void SaveUserSave(byte index)
{
// Save
currentUserIndex = index;
Callable.Call(OnSave);
SaveFile(string.Format(userSaveName, index), currentUserSaveEntries);
}
#endregion
#region VALUES
public enum Location
{
System = 0,
User = 1,
}
Dictionary<string, object> GetEntriesFor(Location location)
{
if (location == Location.System)
return systemSaveEntries;
else
return currentUserSaveEntries;
}
public bool HasValue<T>(string name, Location location)
{
var dict = GetEntriesFor(location);
return dict.ContainsKey(name) && dict[name] is T;
}
public T GetValue<T>(string name, Location location)
{
if (HasValue<T>(name, location))
{
var dict = GetEntriesFor(location);
return (T)dict[name];
}
else
return default(T);
}
void SetValue<T>(string name, Location location, T value)
{
var dict = GetEntriesFor(location);
if (HasValue<T>(name, location))
{
dict[name] = value;
}
else if (dict.ContainsKey(name)) // bad type
{
Debug.LogWarning(string.Format("GameSaveManager : {0} entry '{1}' changed type to {2}", location, name, typeof(T)));
dict[name] = value;
}
else
{
dict.Add(name, value);
}
}
#endregion
#region SERIALIZATION
Dictionary<string, object> LoadFile(string fileName)
{
if(!System.IO.File.Exists(Application.dataPath+fileName))
{
SaveFile(fileName, new Dictionary<string, object>());
}
var dict = new Dictionary<string, System.Object>();
string contents= File.ReadAllText(Application.dataPath + fileName);
SerializableOutput data = JsonUtility.FromJson<SerializableOutput>(contents);
for(int i = 0; i < data.keys.Length; i++)
{
string val = data.values[i];
object value;
if (data.types[i] == SerializableOutput.ValueType.Bool)
value = bool.Parse(val);
else if (data.types[i] == SerializableOutput.ValueType.Int)
value = int.Parse(val);
else if (data.types[i] == SerializableOutput.ValueType.Float)
value = float.Parse(val);
else
value = val;
dict.Add(data.keys[i], value);
}
return dict;
}
void SaveFile(string filename, Dictionary<string, System.Object> entries)
{
int count = entries.Count;
SerializableOutput data = new SerializableOutput();
data.keys = new string[count];
data.values = new string[count];
data.types = new SerializableOutput.ValueType[count];
int i = 0;
foreach (var kvp in entries)
{
data.keys[i] = kvp.Key;
object value = kvp.Value;
if (value is bool)
data.types[i] = SerializableOutput.ValueType.Bool;
else if (value is int)
data.types[i] = SerializableOutput.ValueType.Int;
else if (value is float)
data.types[i] = SerializableOutput.ValueType.Float;
else
data.types[i] = SerializableOutput.ValueType.String;
data.values[i] = kvp.Value.ToString();
i++;
}
File.WriteAllText(Application.dataPath+filename, JsonUtility.ToJson(data));
}
[System.Serializable]
class SerializableOutput
{
public string[] keys;
public string[] values;
public ValueType[] types;
[System.Serializable]
public enum ValueType
{
Bool = 0,
Int = 1,
Float = 2,
String = 3
}
}
#endregion
}
}

11
Runtime/Managers/Implementations/GameSaveManager.cs.meta


fileFormatVersion: 2
guid: 4a94c60800d516b4980b9f68a5ab6cb5
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
正在加载...
取消
保存