コード例 #1
0
        public static Type GetSystemTypeFromWrappedDataType(DataType dataType, VariableMetaData metaData)
        {
            Type elementType = null;

            if (dataType == DataType.UnityObjectReference || dataType == DataType.Enum)
            {
                elementType = metaData.GetTypeFromMetaData();
            }

            foreach (KeyValuePair <Type, DataType> mapping in mappings)
            {
                if (mapping.Value == dataType)
                {
                    elementType = mapping.Key;
                    break;
                }
            }

            if (elementType != null)
            {
                return(elementType);
            }
            else
            {
                // None matched
                return(null);
            }
        }
コード例 #2
0
        public static Type GetSystemTypeFromWrappedDataType(DataType dataType, VariableMetaData metaData)
        {
            if (dataType == DataType.UnityObjectReference)
            {
                return(metaData.GetTypeFromMetaData());
            }
            else if (dataType == DataType.Enum)
            {
                // Backing type, e.g. int, ushort
                return(metaData.GetTypeFromMetaData());
            }

            foreach (KeyValuePair <Type, DataType> mapping in mappings)
            {
                if (mapping.Value == dataType)
                {
                    return(mapping.Key);
                }
            }

            // None matched
            return(null);
        }
コード例 #3
0
        public static object ReadFromBinary(DataType dataType, BinaryReader br, VariableMetaData metaData)
        {
            object value = null;

            if (dataType == DataType.String)
            {
                value = br.ReadString();
            }
            else if (dataType == DataType.Char)
            {
                value = br.ReadChar();
            }
            else if (dataType == DataType.Boolean)
            {
                byte byteValue = br.ReadByte();
                value = (byteValue != 0);
            }
            else if (dataType == DataType.Integer)
            {
                value = br.ReadInt32();
            }
            else if (dataType == DataType.Long)
            {
                value = br.ReadInt64();
            }
            else if (dataType == DataType.Float)
            {
                value = br.ReadSingle();
            }
            else if (dataType == DataType.Double)
            {
                value = br.ReadDouble();
            }
            else if (dataType == DataType.Vector2)
            {
                value = new Vector2(br.ReadSingle(), br.ReadSingle());
            }
            else if (dataType == DataType.Vector3)
            {
                value = new Vector3(br.ReadSingle(), br.ReadSingle(), br.ReadSingle());
            }
            else if (dataType == DataType.Vector4)
            {
                value = new Vector4(br.ReadSingle(), br.ReadSingle(), br.ReadSingle(), br.ReadSingle());
            }
#if UNITY_2017_2_OR_NEWER
            else if (dataType == DataType.Vector2Int)
            {
                value = new Vector2Int(br.ReadInt32(), br.ReadInt32());
            }
            else if (dataType == DataType.Vector3Int)
            {
                value = new Vector3Int(br.ReadInt32(), br.ReadInt32(), br.ReadInt32());
            }
#endif
            else if (dataType == DataType.Bounds)
            {
                value = new Bounds(new Vector3(br.ReadSingle(), br.ReadSingle(), br.ReadSingle()), new Vector3(br.ReadSingle(), br.ReadSingle(), br.ReadSingle()));
            }
#if UNITY_2017_2_OR_NEWER
            else if (dataType == DataType.BoundsInt)
            {
                value = new BoundsInt(new Vector3Int(br.ReadInt32(), br.ReadInt32(), br.ReadInt32()), new Vector3Int(br.ReadInt32(), br.ReadInt32(), br.ReadInt32()));
            }
#endif
            else if (dataType == DataType.Quaternion)
            {
                value = new Quaternion(br.ReadSingle(), br.ReadSingle(), br.ReadSingle(), br.ReadSingle());
            }
            else if (dataType == DataType.Rect)
            {
                value = new Rect(br.ReadSingle(), br.ReadSingle(), br.ReadSingle(), br.ReadSingle());
            }
#if UNITY_2017_2_OR_NEWER
            else if (dataType == DataType.RectInt)
            {
                value = new RectInt(br.ReadInt32(), br.ReadInt32(), br.ReadInt32(), br.ReadInt32());
            }
#endif
            else if (dataType == DataType.Color)
            {
                value = new Color(br.ReadSingle(), br.ReadSingle(), br.ReadSingle(), br.ReadSingle());
            }
            else if (dataType == DataType.Color32)
            {
                value = new Color32(br.ReadByte(), br.ReadByte(), br.ReadByte(), br.ReadByte());
            }
            else if (dataType == DataType.AnimationCurve)
            {
                int        keyframeCount = br.ReadInt32();
                Keyframe[] keyframes     = new Keyframe[keyframeCount];
                for (int i = 0; i < keyframeCount; i++)
                {
                    keyframes[i] = new Keyframe(br.ReadSingle(), br.ReadSingle(), br.ReadSingle(), br.ReadSingle());
                }
                value = new AnimationCurve(keyframes);
            }
            else if (dataType == DataType.Gradient)
            {
                GradientMode       gradientMode = (GradientMode)br.ReadByte();
                GradientAlphaKey[] alphaKeys    = new GradientAlphaKey[br.ReadInt32()];
                for (int i = 0; i < alphaKeys.Length; i++)
                {
                    alphaKeys[i] = new GradientAlphaKey(br.ReadSingle(), br.ReadSingle());
                }
                GradientColorKey[] colorKeys = new GradientColorKey[br.ReadInt32()];

                for (int i = 0; i < colorKeys.Length; i++)
                {
                    colorKeys[i] = new GradientColorKey(new Color(br.ReadSingle(), br.ReadSingle(), br.ReadSingle(), br.ReadSingle()), br.ReadSingle());
                }
                value = new Gradient()
                {
                    mode = gradientMode, alphaKeys = alphaKeys, colorKeys = colorKeys
                };
            }
            else if (dataType == DataType.Enum)
            {
                value = ReadIntegerFromBinary(metaData.GetTypeFromMetaData(), br);
            }
            else if (dataType == DataType.UnityObjectReference)
            {
                string guidString = br.ReadString();
                if (string.IsNullOrEmpty(guidString))
                {
                    value = Guid.Empty;
                }
                else
                {
                    value = new Guid(guidString); // Read guid
                }
            }
            else if (dataType == DataType.Unknown)
            {
                value = br.ReadString(); // Read Type name
            }
            else if (dataType == DataType.Void)
            {
                // No need to read/write a value for a void type
            }
            else
            {
                Debug.LogWarning("Could not read " + dataType);
            }
            return(value);
        }