Ejemplo n.º 1
0
        public void AddValue(string key, System.Object value)
        {
            //Segment --- Should be identical to a segment in AddUnityReferenceValue/AddValue
            BinaryWriter stream = writerStream;

            AddVariableAnchor(key);

            if (value == null)
            {
                stream.Write((byte)0);                 //Magic number indicating a null reference
                return;
            }

            //Magic number indicating that the data is written and not null
            stream.Write((byte)1);
            //Segment --- End

            Type type = value.GetType();

            //stream.Write (type.Name);
            if (type == typeof(int))
            {
                stream.Write((int)value);
            }
            else if (type == typeof(string))
            {
                string st = (string)value;
                stream.Write(st);
            }
            else if (type == typeof(float))
            {
                stream.Write((float)value);
            }
            else if (type == typeof(bool))
            {
                stream.Write((bool)value);
            }
            else if (type == typeof(Vector3))
            {
                Vector3 d = (Vector3)value;
                stream.Write(d.x);
                stream.Write(d.y);
                stream.Write(d.z);
            }
            else if (type == typeof(Vector2))
            {
                Vector2 d = (Vector2)value;
                stream.Write(d.x);
                stream.Write(d.y);
            }
            else if (type == typeof(Matrix4x4))
            {
                Matrix4x4 m = (Matrix4x4)value;
                for (int i = 0; i < 16; i++)
                {
                    stream.Write(m[i]);
                }
            }
            else if (type == typeof(Bounds))
            {
                Bounds b = (Bounds)value;

                stream.Write(b.center.x);
                stream.Write(b.center.y);
                stream.Write(b.center.z);

                stream.Write(b.extents.x);
                stream.Write(b.extents.y);
                stream.Write(b.extents.z);
            }
            else
            {
                ISerializableObject sOb = value as ISerializableObject;

                if (sOb != null)
                {
                    string prePrefix = sPrefix;
                    //Add to the prefix to avoid name collisions
                    sPrefix += key + ".";
                    sOb.SerializeSettings(this);
                    sPrefix = prePrefix;
                }
                else
                {
                    UnityEngine.Object ueOb = value as UnityEngine.Object;

                    if (ueOb != null)
                    {
                        Debug.LogWarning("Unity Object References should be added using AddUnityReferenceValue");
                        WriteError();

                        stream.BaseStream.Position -= 1;               //Overwrite the previous magic number
                        stream.Write((byte)2);                         //Magic number indicating an error while serializing
                    }
                    else
                    {
                        Debug.LogError("Can't serialize type '" + type.Name + "'");
                        WriteError();

                        stream.BaseStream.Position -= 1;               //Overwrite the previous magic number
                        stream.Write((byte)2);                         //Magic number indicating an error while serializing
                    }
                }
            }
        }