feat(core): add tutorial 30 code · unitycoder/UnityTutorials-RTS@2b7f0df · GitHub
Skip to content

Commit 2b7f0df

Browse files
committed
feat(core): add tutorial 30 code
1 parent 2e9acc5 commit 2b7f0df

6 files changed

Lines changed: 201 additions & 4 deletions

File tree

‎Assets/Scenes/SampleScene.unity‎

Lines changed: 3 additions & 3 deletions

‎Assets/Scripts/ScriptableObjects/Parameters/GameParameters.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using System.Collections.Generic;
22
using UnityEngine;
33

4-
public abstract class GameParameters : JSONSerializableScriptableObject
4+
public abstract class GameParameters : BinarySerializableScriptableObject
55
{
66
public abstract string GetParametersName();
77

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Reflection;
4+
using UnityEngine;
5+
6+
[Serializable]
7+
public class BinarySerializableData
8+
{
9+
private static List<Type> _serializableTypes = new List<Type>()
10+
{
11+
typeof(int),
12+
typeof(float),
13+
typeof(bool),
14+
typeof(string),
15+
typeof(InputBinding),
16+
};
17+
18+
public Dictionary<string, object> properties;
19+
20+
public BinarySerializableData(ScriptableObject obj)
21+
{
22+
properties = new Dictionary<string, object>();
23+
24+
Type T = obj.GetType();
25+
foreach (FieldInfo field in T.GetFields())
26+
{
27+
object value;
28+
if (Serialize(field, obj, out value))
29+
properties[field.Name] = value;
30+
}
31+
}
32+
33+
private static bool _IsTypeSerializable(Type tested)
34+
{
35+
return
36+
_serializableTypes.Contains(tested) ||
37+
tested.IsArray && _serializableTypes.Contains(tested.GetElementType());
38+
}
39+
40+
private static bool _IsOfType(Type tested, Type reference)
41+
{
42+
return
43+
tested == reference ||
44+
tested.IsArray && tested.GetElementType() == reference;
45+
}
46+
47+
public static bool Serialize(FieldInfo field, object obj, out object value)
48+
{
49+
Type T = field.FieldType;
50+
if (_IsTypeSerializable(T))
51+
{
52+
value = field.GetValue(obj);
53+
return true;
54+
}
55+
else if (_IsOfType(T, typeof(Color)))
56+
{
57+
Color c = (Color) field.GetValue(obj);
58+
value = new float[] { c.r, c.g, c.b, c.a };
59+
return true;
60+
}
61+
62+
value = null;
63+
return false;
64+
}
65+
66+
public static bool Deserialize(FieldInfo field, object data, out object value)
67+
{
68+
Type T = field.FieldType;
69+
if (_IsTypeSerializable(T))
70+
{
71+
value = data;
72+
return true;
73+
}
74+
else if (_IsOfType(T, typeof(Color)))
75+
{
76+
float[] c = (float[]) data;
77+
value = new Color(c[0], c[1], c[2], c[3]);
78+
return true;
79+
}
80+
81+
value = null;
82+
return false;
83+
}
84+
85+
}

‎Assets/Scripts/Tools/BinarySerializableData.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.
Lines changed: 90 additions & 0 deletions

‎Assets/Scripts/Tools/BinarySerializableScriptableObject.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.

0 commit comments

Comments
 (0)