Skip to content

Commit 6407a24

Browse files
committed
feat(core): add tutorial 31 code
1 parent bd77a9f commit 6407a24

11 files changed

Lines changed: 189 additions & 17 deletions

Assets/Editor/GameParametersEditor.cs

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,17 @@
55
[CustomEditor(typeof(GameParameters), true)]
66
public class GameParametersEditor : Editor
77
{
8+
private GUIStyle _buttonsStyle;
9+
810
public override void OnInspectorGUI()
911
{
12+
if (_buttonsStyle == null)
13+
{
14+
_buttonsStyle = new GUIStyle(GUI.skin.button);
15+
_buttonsStyle.padding.right = 0;
16+
_buttonsStyle.padding.left = 0;
17+
}
18+
1019
serializedObject.Update();
1120

1221
GameParameters parameters = (GameParameters)target;
@@ -23,17 +32,39 @@ public override void OnInspectorGUI()
2332
continue;
2433

2534
EditorGUILayout.BeginHorizontal();
26-
EditorGUILayout.BeginVertical(GUILayout.Width(20f));
35+
EditorGUILayout.BeginVertical(GUILayout.Width(40f));
2736
//check for header attribute
2837
bool hasHeader = System.Attribute.IsDefined(field, typeof(HeaderAttribute), false);
2938
if (hasHeader)
3039
GUILayout.FlexibleSpace();
31-
if (GUILayout.Button(parameters.ShowsField(field.Name) ? "-" : "+", GUILayout.Width(20f)))
40+
EditorGUILayout.BeginHorizontal();
41+
if (GUILayout.Button(
42+
parameters.ShowsField(field.Name)
43+
? EditorGUIUtility.IconContent("animationvisibilitytoggleon@2x")
44+
: EditorGUIUtility.IconContent("d_animationvisibilitytoggleon@2x"),
45+
_buttonsStyle,
46+
GUILayout.Width(20f),
47+
GUILayout.Height(20f)
48+
))
3249
{
3350
parameters.ToggleShowField(field.Name);
3451
EditorUtility.SetDirty(parameters);
3552
AssetDatabase.SaveAssets();
3653
}
54+
if (GUILayout.Button(
55+
parameters.SerializesField(field.Name)
56+
? EditorGUIUtility.IconContent("SaveAs@2x")
57+
: EditorGUIUtility.IconContent("d_SaveAs@2x"),
58+
_buttonsStyle,
59+
GUILayout.Width(20f),
60+
GUILayout.Height(20f)
61+
))
62+
{
63+
parameters.ToggleSerializeField(field.Name);
64+
EditorUtility.SetDirty(parameters);
65+
AssetDatabase.SaveAssets();
66+
}
67+
EditorGUILayout.EndHorizontal();
3768
EditorGUILayout.EndVertical();
3869
GUILayout.Space(16);
3970
EditorGUILayout.PropertyField(serializedObject.FindProperty(field.Name), true);

Assets/Resources/ScriptableObjects/Parameters/00_Global Parameters.asset

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,12 @@ MonoBehaviour:
1212
m_Script: {fileID: 11500000, guid: e4aee701b24e74d11979d05a8285e154, type: 3}
1313
m_Name: 00_Global Parameters
1414
m_EditorClassIdentifier:
15-
_fieldsToShowInGame:
15+
_fieldsToSerialize:
1616
- enableDayAndNightCycle
1717
- enableFOV
18+
_fieldsToShowInGame:
19+
- enableFOV
20+
- enableDayAndNightCycle
1821
enableDayAndNightCycle: 0
1922
dayLengthInSeconds: 20
2023
dayInitialRatio: 0.2

Assets/Resources/ScriptableObjects/Parameters/01_Players Parameters.asset

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,13 @@ MonoBehaviour:
1212
m_Script: {fileID: 11500000, guid: 27f4a5d1a0bdf4ed7a3fd77b8ba10973, type: 3}
1313
m_Name: 01_Players Parameters
1414
m_EditorClassIdentifier:
15+
_fieldsToSerialize:
16+
- players
17+
- myPlayerId
1518
_fieldsToShowInGame: []
1619
players:
17-
- name: The Enemy
18-
color: {r: 0, g: 0.1465373, b: 1, a: 1}
20+
- name: Enemy
21+
color: {r: 0, g: 0.23097992, b: 1, a: 1}
1922
- name: Mina
20-
color: {r: 1, g: 0, b: 0.008962631, a: 1}
23+
color: {r: 1, g: 0, b: 0.038758278, a: 1}
2124
myPlayerId: 1

Assets/Resources/ScriptableObjects/Parameters/02_Sound Parameters.asset

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ MonoBehaviour:
1212
m_Script: {fileID: 11500000, guid: e35d547dcdbda48b4aa7680176196465, type: 3}
1313
m_Name: 02_Sound Parameters
1414
m_EditorClassIdentifier:
15+
_fieldsToSerialize:
16+
- musicVolume
17+
- sfxVolume
1518
_fieldsToShowInGame:
1619
- musicVolume
1720
- sfxVolume

Assets/Resources/ScriptableObjects/Parameters/03_Keyboard Mapping.asset

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ MonoBehaviour:
1212
m_Script: {fileID: 11500000, guid: 0b94adb5636ad4e7880fac54ffdd7ef8, type: 3}
1313
m_Name: 03_Keyboard Mapping
1414
m_EditorClassIdentifier:
15+
_fieldsToSerialize:
16+
- bindings
1517
_fieldsToShowInGame:
1618
- bindings
1719
bindings:

Assets/Scenes/SampleScene.unity

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1920,7 +1920,7 @@ MonoBehaviour:
19201920
m_TargetGraphic: {fileID: 300169161}
19211921
m_HandleRect: {fileID: 300169160}
19221922
m_Direction: 0
1923-
m_Value: 1
1923+
m_Value: 0
19241924
m_Size: 1
19251925
m_NumberOfSteps: 0
19261926
m_OnValueChanged:

Assets/Scripts/ScriptableObjects/Parameters/GamePlayersParameters.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1-
using UnityEngine;
1+
using System.Runtime.Serialization;
2+
using UnityEngine;
23

34
[System.Serializable]
4-
public struct PlayerData
5+
public class PlayerData : BinarySerializable
56
{
67
public string name;
78
public Color color;
9+
10+
protected PlayerData(SerializationInfo info, StreamingContext context)
11+
: base(info, context) { }
812
}
913

1014
[CreateAssetMenu(fileName = "Players Parameters", menuName = "Scriptable Objects/Game Players Parameters", order = 12)]
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
using System;
2+
using System.Collections;
3+
using System.Runtime.Serialization;
4+
using System.Security.Permissions;
5+
using System.Reflection;
6+
7+
[Serializable]
8+
public class BinarySerializable : ISerializable
9+
{
10+
public BinarySerializable() {}
11+
12+
protected BinarySerializable(SerializationInfo info, StreamingContext context)
13+
{
14+
Type T = GetType();
15+
foreach (FieldInfo field in T.GetFields())
16+
{
17+
Type serializedType = BinarySerializableData.GetSerializedType(field);
18+
if (serializedType.IsArray)
19+
{
20+
Type serializedElementType = serializedType.GetElementType();
21+
int nItems = info.GetInt32($"{field.Name}__count");
22+
Array deserializedValue = Array.CreateInstance(serializedElementType, nItems);
23+
for (int i = 0; i < nItems; i++)
24+
deserializedValue.SetValue(info.GetValue($"{field.Name}__{i}", serializedElementType), i);
25+
26+
object value;
27+
if (BinarySerializableData.Deserialize(field, deserializedValue, out value))
28+
field.SetValue(this, value);
29+
else
30+
UnityEngine.Debug.LogWarning($"Could not serialize field: {field.Name} - getting default values");
31+
}
32+
else
33+
{
34+
object deserializedValue = info.GetValue(field.Name, field.FieldType);
35+
object value;
36+
if (BinarySerializableData.Deserialize(field, deserializedValue, out value))
37+
field.SetValue(this, value);
38+
else
39+
UnityEngine.Debug.LogWarning($"Could not serialize field: {field.Name} - getting default values");
40+
}
41+
}
42+
}
43+
44+
[SecurityPermission(SecurityAction.Demand, SerializationFormatter = true)]
45+
public virtual void GetObjectData(SerializationInfo info, StreamingContext context)
46+
{
47+
Type T = GetType();
48+
foreach (FieldInfo field in T.GetFields())
49+
{
50+
object value;
51+
if (BinarySerializableData.Serialize(field, this, out value))
52+
{
53+
if (value.GetType().IsArray)
54+
{
55+
IEnumerator enm = ((IEnumerable)value).GetEnumerator();
56+
// write the items and get the count at the same time
57+
// (since SerializationInfo uses key-value pairs)
58+
int i = 0;
59+
while (enm.MoveNext())
60+
info.AddValue($"{field.Name}__{i++}", enm.Current);
61+
info.AddValue($"{field.Name}__count", i);
62+
}
63+
else
64+
{
65+
info.AddValue(field.Name, value);
66+
}
67+
}
68+
else
69+
{
70+
UnityEngine.Debug.LogWarning($"Could not serialize field: {field.Name} - ignoring");
71+
}
72+
}
73+
}
74+
}

Assets/Scripts/Tools/BinarySerializable.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/BinarySerializableData.cs

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,21 @@ public class BinarySerializableData
1313
typeof(bool),
1414
typeof(string),
1515
typeof(InputBinding),
16+
typeof(PlayerData),
1617
};
1718

1819
public Dictionary<string, object> properties;
1920

20-
public BinarySerializableData(ScriptableObject obj)
21+
public BinarySerializableData(ScriptableObject obj, List<string> fieldsToSerialize)
2122
{
2223
properties = new Dictionary<string, object>();
2324

2425
Type T = obj.GetType();
2526
foreach (FieldInfo field in T.GetFields())
2627
{
28+
if (!fieldsToSerialize.Contains(field.Name))
29+
continue;
30+
2731
object value;
2832
if (Serialize(field, obj, out value))
2933
properties[field.Name] = value;
@@ -44,25 +48,40 @@ private static bool _IsOfType(Type tested, Type reference)
4448
tested.IsArray && tested.GetElementType() == reference;
4549
}
4650

47-
public static bool Serialize(FieldInfo field, object obj, out object value)
51+
public static Type GetSerializedType(FieldInfo field)
4852
{
4953
Type T = field.FieldType;
54+
if (_IsTypeSerializable(T))
55+
return T;
56+
57+
object serialized;
58+
_SerializeValue(T, T.IsValueType ? Activator.CreateInstance(T) : null, out serialized);
59+
return serialized.GetType();
60+
}
61+
62+
private static bool _SerializeValue(Type T, object inValue, out object outValue)
63+
{
5064
if (_IsTypeSerializable(T))
5165
{
52-
value = field.GetValue(obj);
66+
outValue = inValue;
5367
return true;
5468
}
5569
else if (_IsOfType(T, typeof(Color)))
5670
{
57-
Color c = (Color) field.GetValue(obj);
58-
value = new float[] { c.r, c.g, c.b, c.a };
71+
Color c = (Color)inValue;
72+
outValue = new float[] { c.r, c.g, c.b, c.a };
5973
return true;
6074
}
6175

62-
value = null;
76+
outValue = null;
6377
return false;
6478
}
6579

80+
public static bool Serialize(FieldInfo field, object obj, out object value)
81+
{
82+
return _SerializeValue(field.FieldType, field.GetValue(obj), out value);
83+
}
84+
6685
public static bool Deserialize(FieldInfo field, object data, out object value)
6786
{
6887
Type T = field.FieldType;
@@ -73,7 +92,7 @@ public static bool Deserialize(FieldInfo field, object data, out object value)
7392
}
7493
else if (_IsOfType(T, typeof(Color)))
7594
{
76-
float[] c = (float[]) data;
95+
float[] c = (float[])data;
7796
value = new Color(c[0], c[1], c[2], c[3]);
7897
return true;
7998
}

0 commit comments

Comments
 (0)