Example #1
0
        public System.Object GetValue(string key, Type type, System.Object defaultValue = null)
        {
            //Segment --- Should be identical to a segment in GetUnityReferenceValue/GetValue
            if (!MoveToVariableAnchor(key))
            {
                Debug.Log("Couldn't find key '" + key + "' in the data, returning default (" + (defaultValue == null ? "null" : defaultValue.ToString()) + ")");
                return(defaultValue == null?GetDefaultValue(type) : defaultValue);
            }

            BinaryReader stream = readerStream;

            int magicNumber = (int)stream.ReadByte();

            if (magicNumber == 0)
            {
                return(defaultValue == null?GetDefaultValue(type) : defaultValue);                  //Null reference usually
            }
            else if (magicNumber == 2)
            {
                Debug.Log("The variable '" + key + "' was not serialized correctly and can therefore not be deserialized");
                return(defaultValue == null?GetDefaultValue(type) : defaultValue);
            }
            //Else - magic number is 1 - indicating correctly serialized data
            //Segment --- End

            System.Object ob = null;

            if (type == typeof(int))
            {
                ob = stream.ReadInt32();
            }
            else if (type == typeof(string))
            {
                ob = stream.ReadString();
            }
            else if (type == typeof(float))
            {
                ob = stream.ReadSingle();
            }
            else if (type == typeof(bool))
            {
                ob = stream.ReadBoolean();
            }
            else if (type == typeof(Vector3))
            {
                ob = new Vector3(stream.ReadSingle(), stream.ReadSingle(), stream.ReadSingle());
            }
            else if (type == typeof(Vector2))
            {
                ob = new Vector2(stream.ReadSingle(), stream.ReadSingle());
            }
            else if (type == typeof(Matrix4x4))
            {
                Matrix4x4 m = new Matrix4x4();
                for (int i = 0; i < 16; i++)
                {
                    m[i] = stream.ReadSingle();
                }
                ob = m;
            }
            else if (type == typeof(Bounds))
            {
                Bounds b = new Bounds();
                b.center  = new Vector3(stream.ReadSingle(), stream.ReadSingle(), stream.ReadSingle());
                b.extents = new Vector3(stream.ReadSingle(), stream.ReadSingle(), stream.ReadSingle());
                ob        = b;
            }
            else
            {
                if (type.GetConstructor(Type.EmptyTypes) != null)
                {
                    System.Object testOb = Activator.CreateInstance(type);

                    ISerializableObject sOb = (ISerializableObject)testOb;

                    if (sOb != null)
                    {
                        string prePrefix = sPrefix;
                        //Add to the prefix to avoid name collisions
                        sPrefix += key + ".";

                        sOb.DeSerializeSettings(this);
                        ob      = sOb;
                        sPrefix = prePrefix;
                    }
                }

                if (ob == null)
                {
                    Debug.LogError("Can't deSerialize type '" + type.Name + "'");
                    ob = defaultValue == null?GetDefaultValue(type) : defaultValue;
                }
            }

            return(ob);
        }