Skip to content

Commit 108db46

Browse files
committed
feat(core): add tutorial 46 code
1 parent 0c162fd commit 108db46

11 files changed

Lines changed: 239 additions & 42 deletions

Assets/Scenes/GameScene.unity

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1213,7 +1213,7 @@ RectTransform:
12131213
m_RootOrder: 0
12141214
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
12151215
m_AnchorMin: {x: 0, y: 0}
1216-
m_AnchorMax: {x: 0, y: 0}
1216+
m_AnchorMax: {x: 0.9999999, y: 1}
12171217
m_AnchoredPosition: {x: 0, y: 0}
12181218
m_SizeDelta: {x: 20, y: 20}
12191219
m_Pivot: {x: 0.5, y: 0.5}
@@ -1317,11 +1317,11 @@ MonoBehaviour:
13171317
m_FontSize: 14
13181318
m_FontStyle: 0
13191319
m_BestFit: 0
1320-
m_MinSize: 10
1320+
m_MinSize: 1
13211321
m_MaxSize: 40
13221322
m_Alignment: 4
13231323
m_AlignByGeometry: 0
1324-
m_RichText: 1
1324+
m_RichText: 0
13251325
m_HorizontalOverflow: 0
13261326
m_VerticalOverflow: 0
13271327
m_LineSpacing: 1
@@ -1351,7 +1351,7 @@ GameObject:
13511351
m_Icon: {fileID: 0}
13521352
m_NavMeshLayer: 0
13531353
m_StaticEditorFlags: 0
1354-
m_IsActive: 1
1354+
m_IsActive: 0
13551355
--- !u!224 &315611820
13561356
RectTransform:
13571357
m_ObjectHideFlags: 0
@@ -1656,9 +1656,9 @@ RectTransform:
16561656
m_RootOrder: 1
16571657
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
16581658
m_AnchorMin: {x: 0, y: 0}
1659-
m_AnchorMax: {x: 0, y: 0}
1659+
m_AnchorMax: {x: 1, y: 0}
16601660
m_AnchoredPosition: {x: 0, y: 0}
1661-
m_SizeDelta: {x: 0, y: 20}
1661+
m_SizeDelta: {x: -17, y: 20}
16621662
m_Pivot: {x: 0, y: 0}
16631663
--- !u!114 &350083801
16641664
MonoBehaviour:
@@ -1704,7 +1704,7 @@ MonoBehaviour:
17041704
m_HandleRect: {fileID: 300169160}
17051705
m_Direction: 0
17061706
m_Value: 0
1707-
m_Size: 1
1707+
m_Size: 0.9999999
17081708
m_NumberOfSteps: 0
17091709
m_OnValueChanged:
17101710
m_PersistentCalls:
@@ -1907,8 +1907,8 @@ MonoBehaviour:
19071907
m_PersistentCalls:
19081908
m_Calls:
19091909
- m_Target: {fileID: 1464451462}
1910-
m_TargetAssemblyTypeName:
1911-
m_MethodName: LoadGame
1910+
m_TargetAssemblyTypeName: UIManager, Assembly-CSharp
1911+
m_MethodName: SaveGame
19121912
m_Mode: 1
19131913
m_Arguments:
19141914
m_ObjectArgument: {fileID: 0}
@@ -4873,7 +4873,7 @@ GameObject:
48734873
m_Icon: {fileID: 0}
48744874
m_NavMeshLayer: 0
48754875
m_StaticEditorFlags: 0
4876-
m_IsActive: 0
4876+
m_IsActive: 1
48774877
--- !u!224 &979707387
48784878
RectTransform:
48794879
m_ObjectHideFlags: 0
@@ -4889,10 +4889,10 @@ RectTransform:
48894889
m_Father: {fileID: 1865968606}
48904890
m_RootOrder: 1
48914891
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
4892-
m_AnchorMin: {x: 0, y: 1}
4893-
m_AnchorMax: {x: 0, y: 1}
4894-
m_AnchoredPosition: {x: 429, y: -15}
4895-
m_SizeDelta: {x: 160, y: 24}
4892+
m_AnchorMin: {x: 0, y: 0}
4893+
m_AnchorMax: {x: 0, y: 0}
4894+
m_AnchoredPosition: {x: 0, y: 0}
4895+
m_SizeDelta: {x: 100, y: 0}
48964896
m_Pivot: {x: 0.5, y: 0.5}
48974897
--- !u!114 &979707388
48984898
MonoBehaviour:
@@ -6714,7 +6714,7 @@ RectTransform:
67146714
m_RootOrder: 0
67156715
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
67166716
m_AnchorMin: {x: 0, y: 0}
6717-
m_AnchorMax: {x: 0, y: 0}
6717+
m_AnchorMax: {x: 1, y: 1}
67186718
m_AnchoredPosition: {x: 0, y: 0}
67196719
m_SizeDelta: {x: 20, y: 20}
67206720
m_Pivot: {x: 0.5, y: 0.5}
@@ -7316,6 +7316,7 @@ GameObject:
73167316
m_Component:
73177317
- component: {fileID: 1464451459}
73187318
- component: {fileID: 1464451460}
7319+
- component: {fileID: 1464451470}
73197320
- component: {fileID: 1464451465}
73207321
- component: {fileID: 1464451461}
73217322
- component: {fileID: 1464451462}
@@ -7701,6 +7702,18 @@ MonoBehaviour:
77017702
m_Script: {fileID: 11500000, guid: ca525d3ed639b483daea446a3647e1f3, type: 3}
77027703
m_Name:
77037704
m_EditorClassIdentifier:
7705+
--- !u!114 &1464451470
7706+
MonoBehaviour:
7707+
m_ObjectHideFlags: 0
7708+
m_CorrespondingSourceObject: {fileID: 0}
7709+
m_PrefabInstance: {fileID: 0}
7710+
m_PrefabAsset: {fileID: 0}
7711+
m_GameObject: {fileID: 1464451458}
7712+
m_Enabled: 1
7713+
m_EditorHideFlags: 0
7714+
m_Script: {fileID: 11500000, guid: 9fda5c5f889af49e78ae9323a90871dd, type: 3}
7715+
m_Name:
7716+
m_EditorClassIdentifier:
77047717
--- !u!1 &1466245624
77057718
GameObject:
77067719
m_ObjectHideFlags: 0
@@ -7804,14 +7817,14 @@ MonoBehaviour:
78047817
m_Calls: []
78057818
m_FontData:
78067819
m_Font: {fileID: 12800000, guid: fa27db405805f4adfb4b57c49cc04d47, type: 3}
7807-
m_FontSize: 12
7820+
m_FontSize: 14
78087821
m_FontStyle: 0
78097822
m_BestFit: 0
78107823
m_MinSize: 1
78117824
m_MaxSize: 40
78127825
m_Alignment: 4
78137826
m_AlignByGeometry: 0
7814-
m_RichText: 1
7827+
m_RichText: 0
78157828
m_HorizontalOverflow: 0
78167829
m_VerticalOverflow: 0
78177830
m_LineSpacing: 1
@@ -8462,9 +8475,9 @@ RectTransform:
84628475
m_RootOrder: 0
84638476
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
84648477
m_AnchorMin: {x: 0, y: 0}
8465-
m_AnchorMax: {x: 0, y: 0}
8478+
m_AnchorMax: {x: 1, y: 1}
84668479
m_AnchoredPosition: {x: 0, y: 0}
8467-
m_SizeDelta: {x: 0, y: 0}
8480+
m_SizeDelta: {x: -17, y: -17}
84688481
m_Pivot: {x: 0, y: 1}
84698482
--- !u!114 &1606538953
84708483
MonoBehaviour:
@@ -11572,9 +11585,9 @@ RectTransform:
1157211585
m_RootOrder: 2
1157311586
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
1157411587
m_AnchorMin: {x: 1, y: 0}
11575-
m_AnchorMax: {x: 1, y: 0}
11588+
m_AnchorMax: {x: 1, y: 1}
1157611589
m_AnchoredPosition: {x: 0, y: 0}
11577-
m_SizeDelta: {x: 20, y: 0}
11590+
m_SizeDelta: {x: 20, y: -17}
1157811591
m_Pivot: {x: 1, y: 1}
1157911592
--- !u!114 &2006683674
1158011593
MonoBehaviour:

Assets/Scripts/GameData.cs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
using System.Runtime.Serialization;
2+
using UnityEngine;
3+
4+
[System.Serializable]
5+
public class GamePlayerUnitData : BinarySerializable
6+
{
7+
public string unitType;
8+
public string code;
9+
public string uid;
10+
public Vector3 position;
11+
public int currentHealth;
12+
public int level;
13+
public int[] production; // sorted by GAME_RESOURCES_KEYS
14+
public int attackDamage;
15+
public float attackRange;
16+
17+
// building-specific
18+
public int constructionHP;
19+
20+
}
21+
[System.Serializable]
22+
public class GamePlayerData
23+
{
24+
public GamePlayerUnitData[] units;
25+
public int[] resources; // sorted by GAME_RESOURCES_KEYS
26+
}
27+
28+
[System.Serializable]
29+
public class GameData : BinarySerializable
30+
{
31+
public static string gameUid;
32+
private static string _dataFileName = "GameData.data";
33+
34+
public GamePlayerData[] players;
35+
public Vector3 camPosition;
36+
37+
private static string _GetFilePath()
38+
=> System.IO.Path.Combine(
39+
Application.persistentDataPath,
40+
DATA_DIRECTORY,
41+
"Games",
42+
gameUid,
43+
_dataFileName);
44+
45+
public static void Save(GameData instance)
46+
{
47+
BinarySerializable.Save(_GetFilePath(), instance);
48+
}
49+
50+
}

Assets/Scripts/GameData.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Scripts/Tools/BinarySerializable.cs

Lines changed: 49 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,29 @@
11
using System;
22
using System.Collections;
3+
using System.IO;
4+
using System.Reflection;
35
using System.Runtime.Serialization;
6+
using System.Runtime.Serialization.Formatters.Binary;
47
using System.Security.Permissions;
5-
using System.Reflection;
8+
using UnityEngine;
69

710
[Serializable]
811
public class BinarySerializable : ISerializable
912
{
10-
public BinarySerializable() {}
13+
#if UNITY_EDITOR
14+
public static string DATA_DIRECTORY = "Data_Dev";
15+
#else
16+
public static string DATA_DIRECTORY = "Data";
17+
#endif
1118

12-
protected BinarySerializable(SerializationInfo info, StreamingContext context)
19+
public BinarySerializable() { }
20+
21+
protected static void Deserialize(
22+
BinarySerializable instance,
23+
SerializationInfo info,
24+
StreamingContext context)
1325
{
14-
Type T = GetType();
26+
Type T = instance.GetType();
1527
foreach (FieldInfo field in T.GetFields())
1628
{
1729
Type serializedType = BinarySerializableData.GetSerializedType(field);
@@ -25,22 +37,27 @@ protected BinarySerializable(SerializationInfo info, StreamingContext context)
2537

2638
object value;
2739
if (BinarySerializableData.Deserialize(field, deserializedValue, out value))
28-
field.SetValue(this, value);
40+
field.SetValue(instance, value);
2941
else
30-
UnityEngine.Debug.LogWarning($"Could not serialize field: {field.Name} - getting default values");
42+
Debug.LogWarning($"Could not serialize field: {field.Name} - getting default values");
3143
}
3244
else
3345
{
3446
object deserializedValue = info.GetValue(field.Name, field.FieldType);
3547
object value;
3648
if (BinarySerializableData.Deserialize(field, deserializedValue, out value))
37-
field.SetValue(this, value);
49+
field.SetValue(instance, value);
3850
else
39-
UnityEngine.Debug.LogWarning($"Could not serialize field: {field.Name} - getting default values");
51+
Debug.LogWarning($"Could not serialize field: {field.Name} - getting default values");
4052
}
4153
}
4254
}
4355

56+
protected BinarySerializable(SerializationInfo info, StreamingContext context)
57+
{
58+
Deserialize(this, info, context);
59+
}
60+
4461
[SecurityPermission(SecurityAction.Demand, SerializationFormatter = true)]
4562
public virtual void GetObjectData(SerializationInfo info, StreamingContext context)
4663
{
@@ -67,8 +84,31 @@ public virtual void GetObjectData(SerializationInfo info, StreamingContext conte
6784
}
6885
else
6986
{
70-
UnityEngine.Debug.LogWarning($"Could not serialize field: {field.Name} - ignoring");
87+
Debug.LogWarning($"Could not serialize field: {field.Name} - ignoring");
7188
}
7289
}
7390
}
91+
92+
public static BinarySerializable Load(string filePath)
93+
{
94+
if (!File.Exists(filePath))
95+
{
96+
Debug.LogWarning($"File \"{filePath}\" not found! Getting default values.");
97+
return null;
98+
}
99+
100+
IFormatter formatter = new BinaryFormatter();
101+
FileStream s = new FileStream(filePath, FileMode.Open);
102+
BinarySerializable d = (BinarySerializable)formatter.Deserialize(s);
103+
s.Close();
104+
return d;
105+
}
106+
107+
public static void Save(string filePath, BinarySerializable instance)
108+
{
109+
IFormatter formatter = new BinaryFormatter();
110+
FileStream s = new FileStream(filePath, FileMode.Create);
111+
formatter.Serialize(s, instance);
112+
s.Close();
113+
}
74114
}

Assets/Scripts/Tools/BinarySerializableData.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ public class BinarySerializableData
1414
typeof(string),
1515
typeof(InputBinding),
1616
typeof(PlayerData),
17+
typeof(GamePlayerUnitData),
18+
typeof(GamePlayerData),
1719
};
1820

1921
public Dictionary<string, object> properties;
@@ -72,6 +74,12 @@ private static bool _SerializeValue(Type T, object inValue, out object outValue)
7274
outValue = new float[] { c.r, c.g, c.b, c.a };
7375
return true;
7476
}
77+
else if (_IsOfType(T, typeof(Vector3)))
78+
{
79+
Vector3 v = (Vector3)inValue;
80+
outValue = new float[] { v.x, v.y, v.z };
81+
return true;
82+
}
7583

7684
outValue = null;
7785
return false;
@@ -96,6 +104,12 @@ public static bool Deserialize(FieldInfo field, object data, out object value)
96104
value = new Color(c[0], c[1], c[2], c[3]);
97105
return true;
98106
}
107+
else if (_IsOfType(T, typeof(Vector3)))
108+
{
109+
float[] v = (float[])data;
110+
value = new Vector3(v[0], v[1], v[2]);
111+
return true;
112+
}
99113

100114
value = null;
101115
return false;

Assets/Scripts/Tools/BinarySerializableScriptableObject.cs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,6 @@
88

99
public class BinarySerializableScriptableObject : ScriptableObject
1010
{
11-
#if UNITY_EDITOR
12-
private static string _scriptableObjectsDataDirectory = "ScriptableObjects_Dev";
13-
#else
14-
private static string _scriptableObjectsDataDirectory = "ScriptableObjects";
15-
#endif
16-
1711
[SerializeField]
1812
protected List<string> _fieldsToSerialize;
1913
public List<string> FieldsToSeralize => _fieldsToSerialize;
@@ -40,7 +34,7 @@ public void SaveToFile(string fileName = null, bool serializeAll = false)
4034
{
4135
string dirPath = Path.Combine(
4236
Application.persistentDataPath,
43-
_scriptableObjectsDataDirectory
37+
BinarySerializable.DATA_DIRECTORY
4438
);
4539
string filePath = Path.Combine(
4640
dirPath,
@@ -73,7 +67,7 @@ public void LoadFromFile(string fileName = null)
7367
{
7468
string filePath = Path.Combine(
7569
Application.persistentDataPath,
76-
_scriptableObjectsDataDirectory,
70+
BinarySerializable.DATA_DIRECTORY,
7771
$"{(fileName == null ? name : fileName)}.data"
7872
);
7973

0 commit comments

Comments
 (0)