示例#1
0
        public static void WriteToBinary(DataType dataType, object value, BinaryWriter bw)
        {
            if (dataType == DataType.String)
            {
                if (value == null)
                {
                    // Treat null strings as empty as that's what our inspector will default them to
                    bw.Write("");
                }
                else
                {
                    bw.Write((string)value);
                }
            }
            else if (dataType == DataType.Char)
            {
                bw.Write((char)value);
            }
            else if (dataType == DataType.Boolean)
            {
                bool boolValue = (bool)value;
                bw.Write(boolValue ? (byte)1 : (byte)0);
            }
            else if (dataType == DataType.Integer)
            {
                bw.Write((int)value);
            }
            else if (dataType == DataType.Long)
            {
                bw.Write((long)value);
            }
            else if (dataType == DataType.Float)
            {
                bw.Write((float)value);
            }
            else if (dataType == DataType.Double)
            {
                bw.Write((double)value);
            }
            else if (dataType == DataType.Vector2)
            {
                Vector2 vector = (Vector2)value;
                bw.Write(vector.x);
                bw.Write(vector.y);
            }
            else if (dataType == DataType.Vector3)
            {
                Vector3 vector = (Vector3)value;
                bw.Write(vector.x);
                bw.Write(vector.y);
                bw.Write(vector.z);
            }
            else if (dataType == DataType.Vector4)
            {
                Vector4 vector = (Vector4)value;
                bw.Write(vector.x);
                bw.Write(vector.y);
                bw.Write(vector.z);
                bw.Write(vector.w);
            }
#if UNITY_2017_2_OR_NEWER
            else if (dataType == DataType.Vector2Int)
            {
                Vector2Int vector = (Vector2Int)value;
                bw.Write(vector.x);
                bw.Write(vector.y);
            }
            else if (dataType == DataType.Vector3Int)
            {
                Vector3Int vector = (Vector3Int)value;
                bw.Write(vector.x);
                bw.Write(vector.y);
                bw.Write(vector.z);
            }
#endif
            else if (dataType == DataType.Bounds)
            {
                Bounds  bounds = (Bounds)value;
                Vector3 center = bounds.center;
                bw.Write(center.x);
                bw.Write(center.y);
                bw.Write(center.z);

                Vector3 size = bounds.size;
                bw.Write(size.x);
                bw.Write(size.y);
                bw.Write(size.z);
            }
#if UNITY_2017_2_OR_NEWER
            else if (dataType == DataType.BoundsInt)
            {
                BoundsInt bounds = (BoundsInt)value;

                Vector3Int position = bounds.position;
                bw.Write(position.x);
                bw.Write(position.y);
                bw.Write(position.z);

                Vector3Int size = bounds.size;
                bw.Write(size.x);
                bw.Write(size.y);
                bw.Write(size.z);
            }
#endif
            else if (dataType == DataType.Quaternion)
            {
                Quaternion rotation = (Quaternion)value;
                bw.Write(rotation.x);
                bw.Write(rotation.y);
                bw.Write(rotation.z);
                bw.Write(rotation.w);
            }
            else if (dataType == DataType.Rect)
            {
                Rect rect = (Rect)value;
                bw.Write(rect.x);
                bw.Write(rect.y);
                bw.Write(rect.width);
                bw.Write(rect.height);
            }
#if UNITY_2017_2_OR_NEWER
            else if (dataType == DataType.RectInt)
            {
                RectInt rect = (RectInt)value;
                bw.Write(rect.x);
                bw.Write(rect.y);
                bw.Write(rect.width);
                bw.Write(rect.height);
            }
#endif
            else if (dataType == DataType.Color)
            {
                Color color = (Color)value;
                bw.Write(color.r);
                bw.Write(color.g);
                bw.Write(color.b);
                bw.Write(color.a);
            }
            else if (dataType == DataType.Color32)
            {
                Color32 color = (Color32)value;
                bw.Write(color.r);
                bw.Write(color.g);
                bw.Write(color.b);
                bw.Write(color.a);
            }
            else if (dataType == DataType.AnimationCurve)
            {
                AnimationCurve curve = (AnimationCurve)value;
                bw.Write(curve.keys.Length);
                for (int i = 0; i < curve.keys.Length; i++)
                {
                    Keyframe key = curve.keys[i];
                    bw.Write(key.time);
                    bw.Write(key.value);
                    bw.Write(key.inTangent);
                    bw.Write(key.outTangent);
                }
            }
            else if (dataType == DataType.Gradient)
            {
                Gradient gradient = (Gradient)value;
                bw.Write((byte)gradient.mode);
                bw.Write(gradient.alphaKeys.Length);
                for (int i = 0; i < gradient.alphaKeys.Length; i++)
                {
                    var key = gradient.alphaKeys[i];
                    bw.Write(key.alpha);
                    bw.Write(key.time);
                }
                bw.Write(gradient.colorKeys.Length);
                for (int i = 0; i < gradient.colorKeys.Length; i++)
                {
                    var key = gradient.colorKeys[i];
                    bw.Write(key.color.r);
                    bw.Write(key.color.g);
                    bw.Write(key.color.b);
                    bw.Write(key.color.a);
                    bw.Write(key.time);
                }
            }
            else if (dataType == DataType.Enum)
            {
                bw.Write((int)value);
            }
            else if (dataType == DataType.UnityObjectReference)
            {
                if (value is UnityEngine.Object || value == null)
                {
                    UnityEngine.Object unityObject = value as UnityEngine.Object;
                    if (unityObject != null)
                    {
                        bw.Write(ObjectMap.AddOrGetObject(unityObject).ToString());
                    }
                    else
                    {
                        bw.Write(Guid.Empty.ToString());
                    }
                }
                else
                {
                    bw.Write(((Guid)value).ToString());
                }
            }
            else if (dataType == DataType.Unknown)
            {
                bw.Write((string)value); // Write Type name
            }
            else if (dataType == DataType.Void)
            {
                // No need to read/write a value for a void type
            }
            else
            {
                Debug.LogWarning("Could not write " + dataType);
            }
        }